全面的C# listbox控件操作

开发 后端
C# listbox控件操作看似很简单,但是总结起来还是很多的,以下就是C# listbox控件操作的一些总结,希望能给大家带来帮助。

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" />
   
           
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.

以上就是笔者和大家分享的C# listbox控件操作总结。

【编辑推荐】

  1. C#委托基础:谈委托和接口
  2. 简单实现C# CheckBox单选的相关功能
  3. C# ServiceController类剖析
  4. C# HttpWebRequest提交数据方式浅析
  5. C#计算素数序列浅谈
责任编辑:阡陌 来源: 网络转载
相关推荐

2009-08-19 11:21:02

C# ListBox控

2009-08-14 14:19:50

Enhanced LiC#构建

2009-09-08 15:39:00

2009-09-08 14:54:40

C# listBox控

2009-09-08 16:10:03

C# ListBox

2009-09-27 10:46:30

C#控件数组

2009-08-12 15:50:40

C# ListBox

2009-08-03 17:12:40

C#指针操作

2009-09-18 10:45:31

C#数组操作

2009-09-16 10:56:22

C#开发ActiveX

2009-08-06 16:05:09

GridView控件

2009-08-18 14:36:36

C# 操作Excel

2009-09-08 16:34:49

C# Listbox

2009-09-10 12:00:09

C# listbox

2009-09-08 15:12:07

C# ListBox

2011-05-20 16:07:29

C#

2009-09-08 16:22:27

c# listBox

2024-06-05 08:17:37

C#算法数据科学

2009-08-27 13:55:08

C#子线程

2009-08-18 17:41:22

C# ListView
点赞
收藏

51CTO技术栈公众号