• ListBox控件的操作


    Items的相关使用方法:
    1、往listBox中添加一个数据  
                listBox.Items.Add("数据1");
    2、往listBox中添加多个数据集合 :
                string [] list = new string []{"admin","abcdefg","sqlserver"};
                listBox.Items.AddRange(list);
    3、在listBox指定位置插入一个新值
                listBox.Items.Insert(3,"New Add");
    4、获取listBox中的索引
                int index = listBox.Items.IndexOf("admin");//获取在listBox的索引
    5、判断是否存在
                bool bi = listBox.items.Contains("sqlserver");
    6、清除指定的数据
                listBox.Items.Remove("admin");
    7、清除指定索引位置的数据
                listBox.Items.RemoveAt("admin");
    
    当在listbox中要加载大量的数据时,最好使用以下两个方法:
    1、使用BeginUpdate可以防止控件闪烁
                listBox1.BeginUpdate();//开头加一个
                
                中间的数据使用 “for循环”或“foreach循环”
              {
                    List<UserInfo> list = new List<UserInfo>();
                    list.Add(new UserInfo()
                    {
                        ID = 1,
                        Name = "张三"
                    });
                    list.Add(new UserInfo()
                    {
                        ID = 2,
                        Name = "李四"
                    }); list.Add(new UserInfo()
                    {
                        ID = 3,
                        Name = "王五"
                    }); list.Add(new UserInfo()
                    {
                        ID = 4,
                        Name = "赵六"
                    });
                    //绑定数据 想的实际值一般来说 就会指定对应显示值的编号
                    listBox1.DataSource = list;//选项的来源
                    listBox1.DisplayMember = "Name";//想显示的文本对应属性名
                    listBox1.ValueMember = "ID";//想的实际值
                }
     2、      listBox1.EndUpdate();//结尾加一个
             注:往listbox中绑定数据加载  一定要使用 (DataSource、DisplayMember、ValueMember)三个属性,否则数据显示不出来
    

      

     

     var index = listBox1.SelectedIndices;
      foreach (int i in index)
     {
           textBox1.AppendText(i.ToString()+",");//AppendText:在textbox后面添加数据
     }
    效果图如下所示:

      

      

  • 相关阅读:
    poj 1840 简单哈希
    poj 2151概率dp
    poj 3349 简单hash
    poj3274 hash
    poj 1459 最大流 Dinic模板题
    poj 3436 最大流-拆点
    poj 3020 二分图最大匹配
    poj 1094 简单拓扑排序
    poj3687 反向建图拓扑排序
    poj 3267
  • 原文地址:https://www.cnblogs.com/leiminghui/p/13483714.html
Copyright © 2020-2023  润新知