新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
經(jīng)典C#ListBox操作實況
C# ListBox操作實況

- protectedvoidButton1_Click(objectsender,EventArgse)
- {
- ListItemnewItem=newListItem();
- newItem.Text=this.TextBox3.Text;
- newItem.Value=this.TextBox4.Text;
- this.ListBox1.Items.Add(newItem);
- }
- //得到設置最大數(shù)量
- //原Capacity的容量為8,默認的情況下會隨著新增項(ListItem)的數(shù)量增多會成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);}//得到集合中項的數(shù)量protectedvoidButton6_Click(objectsender,EventArgse)
- {
- Response.Write(this.ListBox1.Items.Count.ToString());
- }
- //根據(jù)文本查找,如果找到則返回此項,如果找不到則返加null
- protectedvoidButton7_Click(objectsender,EventArgse)
- {
- ListItemitem=this.ListBox1.Items.FindByText(this.TextBox1.Text);
- if(item!=null)
- {
- Response.Write(item.Text+":"+item.Value);
- }
- }
- //根據(jù)值查找,如果找到則返回此項,如果找不到則返加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);
- }//刪除節(jié)點
- protectedvoidButton11_Click(objectsender,EventArgse)
- {
- ListItemitem=newListItem(this.TextBox1.Text,this.TextBox2.Text);
- this.ListBox1.Items.Remove(item);
- }//根據(jù)索引位置刪除
- 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操作實況,希望能給大家?guī)韼椭?/p>
分享題目:經(jīng)典C#ListBox操作實況
標題路徑:http://www.dlmjj.cn/article/coohcog.html


咨詢
建站咨詢
