C# listbox控件操作看似很简单,但是总结起来还是很多的,以下就是C# listbox控件操作的一些总结,希望能给大家带来帮助。
- <%@ Page Language="C#" %>
- "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- "server">
- //增加新建项
- protected void Button1_Click(object sender, EventArgs e)
- {
- ListItem newItem = new ListItem();
- newItem.Text = this.TextBox3.Text;
- newItem.Value = this.TextBox4.Text;
- this.ListBox1.Items.Add(newItem);
- }
- //得到设置最大数量
- //原Capacity的容量为8,默认的情况下会随着新增项(ListItem)的数量增多会成2倍的增长8 16 32..
- //但如果设置了Capacity的大小,就不会自动增长了
- protected void Button2_Click(object sender, EventArgs e)
- {
- Response.Write("原大小:"+this.ListBox1.Items.Capacity.ToString());
- this.ListBox1.Items.Capacity = 6;
- Response.Write("更改后:"+this.ListBox1.Items.Capacity.ToString());
- }
- //清空items集合中所有的项
- protected void Button3_Click(object sender, EventArgs e)
- {
- this.ListBox1.Items.Clear();
- }
- //判断集合中是否包含指定项
- protected void Button4_Click(object sender, EventArgs e)
- {
- ListItem item = new ListItem(this.TextBox1.Text, this.TextBox2.Text);
- if (this.ListBox1.Items.Contains(item))
- {
- Response.Write("yes"); ;
- }
- else
- {
- Response.Write("no");
- }
- }
- //将items中的所有项复制到listItemCollection集合中
- protected void Button5_Click(object sender, EventArgs e)
- {
- ListItem[] items=new ListItem[this.ListBox1.Items.Count];
- this.ListBox1.Items.CopyTo(items, 0);
- ListBox listBox2 = new ListBox();
- listBox2.ID = "ListBox2";
- listBox2.Items.AddRange(items);
- //得到ListBox1的位置,并将listBox2放到ListBox1后面
- int position = 0;
- foreach (Control c in this.form1.Controls)
- {
- position++;
- if (c.ID == "ListBox1")
- {
- break;
- }
- }
- this.form1.Controls.AddAt(position,listBox2);
- }
- //得到集合中项的数量
- protected void Button6_Click(object sender, EventArgs e)
- {
- Response.Write(this.ListBox1.Items.Count.ToString());
- }
- //根据文本查找,如果找到则返回此项,如果找不到则返加null
- protected void Button7_Click(object sender, EventArgs e)
- {
- ListItem item= this.ListBox1.Items.FindByText(this.TextBox1.Text);
- if (item != null)
- {
- Response.Write(item.Text+":"+item.Value);
- }
- }
- //根据值查找,如果找到则返回此项,如果找不到则返加null
- protected void Button8_Click(object sender, EventArgs e)
- {
- ListItem item = this.ListBox1.Items.FindByText(this.TextBox2.Text);
- if (item != null)
- {
- Response.Write(item.Text + ":" + item.Value);
- }
- }
- //所查找项的索引
- protected void Button9_Click(object sender, EventArgs e)
- {
- ListItem item = this.ListBox1.Items.FindByText(this.TextBox1.Text);
- if (item != null)
- {
- int position = this.ListBox1.Items.IndexOf(item);
- Response.Write("所查找项的索引:"+position.ToString());
- }
- }
- //插入新建项
- protected void Button10_Click(object sender, EventArgs e)
- {
- ListItem newItem=new ListItem(this.TextBox1.Text,this.TextBox2.Text);
- this.ListBox1.Items.Insert(0, newItem);
- }
- //删除节点
- protected void Button11_Click(object sender, EventArgs e)
- {
- ListItem item = new ListItem(this.TextBox1.Text, this.TextBox2.Text);
- this.ListBox1.Items.Remove(item);
- }
- //根据索引位置删除
- protected void Button12_Click(object sender, EventArgs e)
- {
- int index = this.ListBox1.Items.IndexOf(new ListItem(this.TextBox1.Text, this.TextBox2.Text));
- this.ListBox1.Items.RemoveAt(index);
- }
- //选中项
- protected void Button13_Click(object sender, EventArgs e)
- {
- this.TextBox1.Text = this.ListBox1.SelectedItem.Text;
- this.TextBox2.Text = this.ListBox1.SelectedItem.Value;
- }
- //修改
- protected void Button14_Click(object sender, EventArgs e)
- {
- this.ListBox1.SelectedItem.Text = this.TextBox3.Text;
- this.ListBox1.SelectedItem.Value = this.TextBox4.Text;
- }
- "http://www.w3.org/1999/xhtml" >
- "server">
- "form1" runat="server">
- "ListBox1" runat="server">
- "0">张三
- "1">李四
- "True" Value="2">王五
- "3">赵六
- "4">王七
- oldText
- "TextBox1" runat="server">
- oldValue
- "TextBox2" runat="server">
- newText
- "TextBox3" runat="server">
- newValue "TextBox4" runat="server">
- "Button1" runat="server" Text="add" OnClick="Button1_Click" />
- "Button2" runat="server" Text="get(Set)Capacity" OnClick="Button2_Click" />
- "Button3" runat="server" Text="Clear" OnClick="Button3_Click" />
- "Button4" runat="server" Text="Contains" OnClick="Button4_Click" />
- "Button5" runat="server" Text="copyTo" OnClick="Button5_Click" />
- "Button6" runat="server" Text="count" OnClick="Button6_Click" />
- "Button7" runat="server" OnClick="Button7_Click" Text="findByText" />
- "Button8" runat="server" Text="findByValue" OnClick="Button8_Click" />
- "Button9" runat="server" Text="indexOf" OnClick="Button9_Click" />
- "Button10" runat="server" Text="insert" OnClick="Button10_Click" />
- "Button11" runat="server" Text="remove" OnClick="Button11_Click" />
- "Button12" runat="server" Text="removeAt" OnClick="Button12_Click" />
- "Button13" runat="server" Text="select" OnClick="Button13_Click" />
- "Button14" runat="server" Text="edit" OnClick="Button14_Click" />
以上就是笔者和大家分享的C# listbox控件操作总结。
本文题目:全面的C#listbox控件操作
当前链接:http://www.shufengxianlan.com/qtweb/news41/415541.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联