GVKun编程网logo

将StringCollection中的项目复制到列表框(string拷贝到char数组)

30

最近很多小伙伴都在问将StringCollection中的项目复制到列表框和string拷贝到char数组这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展.net–将StringC

最近很多小伙伴都在问将StringCollection中的项目复制到列表框string拷贝到char数组这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展.net – 将StringCollection转换为List、AbstractCollection 方法的toString()方法解析、c# – IIS 7.x,添加启用HTTPS的站点:SiteCollection.Add(string,string,string,byte [])overload、c# – WP7用MVVM将StrokeCollection转换为PNG?等相关知识,下面开始了哦!

本文目录一览:

将StringCollection中的项目复制到列表框(string拷贝到char数组)

将StringCollection中的项目复制到列表框(string拷贝到char数组)

如何解决将StringCollection中的项目复制到列表框?

我正在尝试将字符串集合变量中的所有字符串复制到列表框中。 我正在尝试这样的代码:

ListBox1.Items.AddRange(<strings stored in StringCollection1>)

那么如何使用AddRange方法将字符串从StringCollection复制到列表框?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

.net – 将StringCollection转换为List

.net – 将StringCollection转换为List

通常,我会选择List< String> [或者,在VB中,List(Of String)] over StringCollection尽可能:另见 Best string container。

然而,看起来,泛型 – 因此,List< String> – 显然不支持VS 2008的设置。因此,如果我想在我的用户设置中使用字符串列表,我不得不求助于使用StringCollection。

现在,因为我不想看到我的代码中的StringCollection,我需要将其转换为List< String&gt ;.我如何有效地这样做?或者,更好的是,我错了,有一种方法使用List< String>在设计设计师?

编辑:我必须使用.NET 2.0。

如果你必须使用.NET 2.0,我会想象最干净的选择是创建一个包装器StringCollection实现IEnumerable< string>和IEnumerator< string>分别用于StringCollection和StringEnumerator。 (注意:根据元数据,StringEnumerator不实现IEnumerator)。下面的示例。然而,在一天结束时,某人将在StringCollection上做一个foreach(),所以可以认为一个简单的foreach(stringCollection中的字符串项)并添加到List< string>就足够了;我怀疑这不会是足够满足您的需求。

你也可以实现IList< string>使用这种方法,保存你重复的底层字符串,但你会支付一个惩罚,“包装器”类型委托调用(在堆栈上多一个方法调用!)。我建议你处理在系统中的接口的事情无论如何IEnumberable< string>,IList< string>等等,而不是具体的List,它会引导你走一条更大的灵活性的道路。

static void Main(string[] args)
    {
        StringCollection stringCollection = new StringCollection();
        stringCollection.AddRange(new string[] { "hello","world" });

        // Wrap!
        List<string> listofStrings = new List<string>(new StringCollectionEnumerable(stringCollection));

        Debug.Assert(listofStrings.Count == stringCollection.Count);
        Debug.Assert(listofStrings[0] == stringCollection[0]); 

    }

    private class StringCollectionEnumerable : IEnumerable<string>
    {
        private StringCollection underlyingCollection; 

        public StringCollectionEnumerable(StringCollection underlyingCollection)
        {
            this.underlyingCollection = underlyingCollection; 
        }

        public IEnumerator<string> GetEnumerator()
        {
            return new StringEnumeratorWrapper(underlyingCollection.GetEnumerator()); 
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator(); 
        }
    }

    private class StringEnumeratorWrapper : IEnumerator<string>
    {
        private StringEnumerator underlyingEnumerator; 

        public StringEnumeratorWrapper(StringEnumerator underlyingEnumerator)
        {
            this.underlyingEnumerator = underlyingEnumerator;
        }

        public string Current
        {
            get
            {
                return this.underlyingEnumerator.Current; 
            }
        }

        public void dispose()
        {
            // No-op 
        }

        object System.Collections.IEnumerator.Current
        {
            get
            {
                return this.underlyingEnumerator.Current;
            }
        }

        public bool MoveNext()
        {
            return this.underlyingEnumerator.MoveNext(); 
        }

        public void Reset()
        {
            this.underlyingEnumerator.Reset(); 
        }
    }

AbstractCollection 方法的toString()方法解析

AbstractCollection 方法的toString()方法解析

AbstractCollection 抽象类,是Java util下的Collection的骨干类,提供了很多方法。很多方法,都被具体的实现类重写了,但是toString方法好像保留了,这个方法的源码如下:

 public String toString() {
        Iterator<E> i = iterator();
    if (! i.hasNext())
        return "[]";

    StringBuilder sb = new StringBuilder();
    sb.append(''['');
    for (;;) {
        E e = i.next();
        sb.append(e == this ? "(this Collection)" : e);
        if (! i.hasNext())
        return sb.append('']'').toString();
        sb.append(", ");
    }
    }

里面有一句 

sb.append(e == this ? "(this Collection)" : e);

这句为啥要加这个判断呢,看下如下代码

ArrayList a = new ArrayList();
a.add("1");
a.add("2");
a.add(a);//加本身
System.out.println(a.toString());

   a.add(a),这句本身没有语法错误的,虽然一般人不会这么加,但是真这样加了,上面那句判断不加,当前e是this的话,被append调用,又会调用自身的toString(),无线循环了,就会 overstackflow 了。jdk源码严谨值得学习啊。


c# – IIS 7.x,添加启用HTTPS的站点:SiteCollection.Add(string,string,string,byte [])overload

c# – IIS 7.x,添加启用HTTPS的站点:SiteCollection.Add(string,string,string,byte [])overload

我需要以编程方式添加一个IIS 7.x站点,当我们默认使用HTTPS / SSL绑定来创建这个应用程序时,我将陷入困境,usig SiteCollection.Add(string,string,byte[]) overload.

给予https:*:80:test.localhost https:*:443:test.localhost作为bindinginformation抛出一个带有此消息的ArgumentException:指定的HTTPS绑定无效.

这个绑定信息有什么问题?

谢谢.

编辑:我正在使用Microsoft.Web.Administration程序集.

解决方法

这是我做了什么来创建https网站,它的工作.当然,我在这里跳过一些代码.
using Microsoft.Web.Administration
...
using(var manager = new ServerManager())
{
    // variables are set in advance...
    var site = manager.Sites.Add(siteName,siteFolder,siteConfig.Port);

    var store = new X509Store(StoreName.AuthRoot,StoreLocation.LocalMachine);
    store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadWrite);

    // certHash is my certificate's hash,byte[]
    var binding = site.Bindings.Add("*:443:",certHash,store.Name);
    binding.Protocol = "https";

    store.Close();

    site.ApplicationDefaults.EnabledProtocols = "http,https";

    manager.CommitChanges();
}

UPD:证书是通过以下方式从pfx文件创建的:

// get certificate from the file
string pfx = Directory.GetFiles(folder,"*.pfx",SearchOption.AllDirectories).FirstOrDefault();
var store = new X509Store(StoreName.Root,StoreLocation.LocalMachine);
store.Open(OpenFlags.OpenExistingOnly | OpenFlags.ReadWrite);

var certificate = new X509Certificate2(pfx,certPassword,X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);
store.Add(certificate);
store.Close();
certHash = certificate.GetCertHash();

c# – WP7用MVVM将StrokeCollection转换为PNG?

c# – WP7用MVVM将StrokeCollection转换为PNG?

我有一个InkPresenter绑定到我用于签名面板的MVVM模型中的strokeCollection.在我将数据发送回服务器之前,我想将strokeCollection转换为PNG数据,这就是我所拥有的(我正在使用 ImageTools库):

// Signature is a strokesCollection
var bounds = Signature.GetBounds();
var inkSignature = new InkPresenter {Height = bounds.Height,Width = bounds.Width,strokes = Signature};
var wbBitmap = new WriteableBitmap(inkSignature,null);
var myImage = wbBitmap.ToImage();
byte[] by = null;
MemoryStream stream = null;
using (stream = new MemoryStream())
{
    PngEncoder png = new PngEncoder();
    png.Encode(myImage,stream);
}

流总是只用0填充,我觉得我错过了一些我没想过的非常简单的东西.有任何想法吗?

解决方法

我认为问题在于渲染器在您获取UI之前没有时间更新UI.尝试将位图创建包装到dispatcher.BeginInvoke调用.

关于将StringCollection中的项目复制到列表框string拷贝到char数组的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于.net – 将StringCollection转换为List、AbstractCollection 方法的toString()方法解析、c# – IIS 7.x,添加启用HTTPS的站点:SiteCollection.Add(string,string,string,byte [])overload、c# – WP7用MVVM将StrokeCollection转换为PNG?等相关知识的信息别忘了在本站进行查找喔。

本文标签: