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操作实况,希望能给大家带来帮助。
分享文章:经典C#ListBox操作实况
当前路径:http://www.mswzjz.cn/qtweb/news45/364695.html
攀枝花网站建设、攀枝花网站运维推广公司-贝锐智能,是专注品牌与效果的网络营销公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 贝锐智能