C# ListBox操作实况
- protectedvoidButton1_Click(objectsender,EventArgse)
- {
- ListItemnewItem=newListItem();
- newItem.Text=this.TextBox3.Text;
- newItem.Value=this.TextBox4.Text;
- this.ListBox1.Items.Add(newItem);
- }
- //得到设置最大数量
- //原Capacity的容量为8,默认的情况下会随着新增项(ListItem)的数量增多会成2倍的增长81632..
- //但如果设置了Capacity的大小,就不会自动增长了
- protectedvoidButton2_Click(objectsender,EventArgse)
- {
- Response.Write("原大小:"+this.ListBox1.Items.Capacity.ToString());
- this.ListBox1.Items.Capacity=6;
- Response.Write("更改后:"+this.ListBox1.Items.Capacity.ToString());}//清空items集合中所有的项
- protectedvoidButton3_Click(objectsender,EventArgse)
- {
- this.ListBox1.Items.Clear();
- }//判断集合中是否包含指定项
- protectedvoidButton4_Click(objectsender,EventArgse)
- {
- ListItemitem=newListItem(this.TextBox1.Text,this.TextBox2.Text);
- if(this.ListBox1.Items.Contains(item))
- {
- Response.Write("yes");;
- }
- else
- {
- Response.Write("no");
- }}//将items中的所有项复制到listItemCollection集合中
- protectedvoidButton5_Click(objectsender,EventArgse)
- {
- ListItem[]items=newListItem[this.ListBox1.Items.Count];
- this.ListBox1.Items.CopyTo(items,0);
- ListBoxlistBox2=newListBox();
- listBox2.ID="ListBox2";
- listBox2.Items.AddRange(items);
- //得到ListBox1的位置,并将listBox2放到ListBox1后面
- intposition=0;
- foreach(Controlcinthis.form1.Controls)
- {
- position++;
- if(c.ID=="ListBox1")
- {
- break;
- }
- }
- this.form1.Controls.AddAt(position,listBox2);}//得到集合中项的数量protectedvoidButton6_Click(objectsender,EventArgse)
- {
- Response.Write(this.ListBox1.Items.Count.ToString());
- }
- //根据文本查找,如果找到则返回此项,如果找不到则返加null
- protectedvoidButton7_Click(objectsender,EventArgse)
- {
- ListItemitem=this.ListBox1.Items.FindByText(this.TextBox1.Text);
- if(item!=null)
- {
- Response.Write(item.Text+":"+item.Value);
- }
- }
- //根据值查找,如果找到则返回此项,如果找不到则返加null
- protectedvoidButton8_Click(objectsender,EventArgse)
- {
- ListItemitem=this.ListBox1.Items.FindByText(this.TextBox2.Text);
- if(item!=null)
- {
- Response.Write(item.Text+":"+item.Value);
- }
- }
- //所查找项的索引
- protectedvoidButton9_Click(objectsender,EventArgse)
- {
- ListItemitem=this.ListBox1.Items.FindByText(this.TextBox1.Text);
- if(item!=null)
- {
- intposition=this.ListBox1.Items.IndexOf(item);
- Response.Write("所查找项的索引:"+position.ToString());
- }
- }//插入新建项
- protectedvoidButton10_Click(objectsender,EventArgse)
- {
- ListItemnewItem=newListItem(this.TextBox1.Text,this.TextBox2.Text);
- this.ListBox1.Items.Insert(0,newItem);
- }//删除节点
- protectedvoidButton11_Click(objectsender,EventArgse)
- {
- ListItemitem=newListItem(this.TextBox1.Text,this.TextBox2.Text);
- this.ListBox1.Items.Remove(item);
- }//根据索引位置删除
- protectedvoidButton12_Click(objectsender,EventArgse)
- {
- intindex=this.ListBox1.Items.IndexOf(newListItem(this.TextBox1.Text,this.TextBox2.Text));
- this.ListBox1.Items.RemoveAt(index);
- }//选中项
- protectedvoidButton13_Click(objectsender,EventArgse)
- {
- this.TextBox1.Text=this.ListBox1.SelectedItem.Text;
- this.TextBox2.Text=this.ListBox1.SelectedItem.Value;
- }//修改
- protectedvoidButton14_Click(objectsender,EventArgse)
- {
- this.ListBox1.SelectedItem.Text=this.TextBox3.Text;
- this.ListBox1.SelectedItem.Value=this.TextBox4.Text;
- }
以上介绍了C# ListBox的操作方法,可谓是C# ListBox操作实况,希望能给大家带来帮助。
【编辑推荐】