例如ListItem控件中有几个人的名字:张三
李四
三个字
四个字的经过方法处理后,能得到这样的一个string(就是除最后一项外,每个ListItem项后加一个逗号) 
"张三,李四,三个字,四个字的"另外一个方法反相操作,既把这样一个字符串
"很多个字,字数不一样,个数也不一样,但是没标点符号,都是人名"
转换为一个ListItem[]集合,好直接绑到控件上。谢谢各位了……

解决方案 »

  1.   

    string [] temp = str.split(',');
    ListItem [] listItem = new ListItem[temp.length];
    for(int i = 0; i< temp.length;i++)
    {
      
      listitem[i].text = temp[i];
    }
      

  2.   

    方法
    public static ListItem[] conver(string str)
    {
      string [] temp = str.split(',');
      ListItem [] listItem = new ListItem[temp.length];
      for(int i = 0; i< temp.length;i++)
      {
      
      listitem[i].text = temp[i];
      }
    }出错。。
    string [] temp = str.split(',');错误 1 “string”并不包含“split”的定义
      

  3.   

    public static ListItem[] StringToListitem(string s)
        {
            string[] temp = s.Split(',');
            ListItem[] listItem = new ListItem[temp.Length];
            for (int i = 0; i < temp.Length; i++)
            {
                listItem[i].Text = temp[i];
            }
            return listItem;
        }
    这段代码可以正常编译。不过在实际运行到循环体的时候出错:未将对象引用设置到对象的实例。
    我已经有点晕了……谁能帮我看看啊。
      

  4.   

    public static ListItem[] StringToListitem(string s)
        {
            string[] temp = s.Split(',');
            ListItem[] listItem = new ListItem[temp.Length];
            for (int i = 0; i < temp.Length; i++)
            {
    listItem[i]=new ListItem();//你少这句,我是搞WinForm的,不知道ListItem构造函数如何定义
                listItem[i].Text = temp[i];
            }
            return listItem;
        }
      

  5.   

    谢谢你们两位,jointan我也发现了。下面是我最后写好的代码:    public static string ConverType(ListItemCollection l)  //把控件的Items属性编码成string后方便存储
        {
            string s = "";
            if (l.Count == 1)
            {
                s = l[0].Value;
                return s;
            }
            else
            {
                for (int i = 0; i < l.Count; i++)
                {
                    if (i == l.Count - 1)
                    {
                        s = s + l[i].Text;
                        return s;
                    }
                    s = s + l[i].Text + ",";
                }
                return s;
            }
        }    public static ListItem[] ConverType(string s) //把编码后的字符转换为ListItem[]集合供给控件。只适合这种格式的字符"汉字,字数不限,输入法下的逗号,最后一项没有逗号"
        {
            string[] temp = s.Split(',');
            ListItem[] listItem = new ListItem[temp.Length];
            for (int i = 0; i < temp.Length; i++)
            {
                listItem[i] = new ListItem();
                listItem[i].Text = temp[i];
                listItem[i].Value = temp[i];
            }
            return listItem;
        }