ListBox控件
1.功能
ListBox控件顯示較長的選項列表,用戶可從中選擇一項或多項。如果項總數(shù)超出可以顯示的項數(shù),則自動向ListBox控件添加滾動條。ListBox控件列表中的每個元素稱為項。圖1所示為ListBox控件。
圖1 ListBox控件
2.屬性
ListBox控件常用屬性及說明如表1所示。
表1 ListBox控件常用屬性及說明
下面對比較重要的屬性進行詳細介紹。
(1)Items屬性。該屬性用于查看列表框中的項。
語法:
public ObjectCollection Items { get; } |
public Object SelectedItem { get; set; } |
SqlConnection con = new SqlConnection("server=ZHY\\zhy;uid=sa;pwd=;database=student"); con.Open(); SqlCommand com = new SqlCommand("select * from student",con); SqlDataReader dr = com.ExecuteReader(); this.listBox1.Items.Clear(); while (dr.Read()) { // this.listBox1.Items.Add(dr[0].ToString()); this.listBox1.Items.Add(dr[1].ToString()); // this.listBox1.Items.Add(dr[2].ToString()); } dr.Close(); con.Close(); |
using System; using System.Collections.Generic; using System.Windows.Forms; namespace _8_05 { static class Program { /// <summary> /// 應(yīng)用程序的主入口點。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new frmListBox()); } } } |
. 屬性列表:
SelectionMode 組件中條目的選擇類型,即多選(Multiple)、單選(Single)
Rows 列表框中顯示總共多少行
Selected 檢測條目是否被選中
SelectedItem 返回的類型是ListItem,獲得列表框中被選擇的條目
Count 列表框中條目的總數(shù)
SelectedIndex 列表框中被選擇項的索引值
Items 泛指列表框中的所有項,每一項的類型都是ListItem
2. 取列表框中被選中的值
ListBox.SelectedValue
3. 動態(tài)的添加列表框中的項:
ListBox.Items.Add("所要添加的項");
4. 移出指定項:
//首先判斷列表框中的項是否大于0
If(ListBox.Items.Count > 0 )
{
//移出選擇的項
ListBox.Items.Remove(ListBox.SelectedItem);
}
5. 清空所有項:
//首先判斷列表框中的項是否大于0
If(ListBox.Items.Count > 0 )
{
//清空所有項
ListBox.Items.Clear();
}
6. 列表框可以一次選擇多項:
只需設(shè)置列表框的屬性 SelectionMode="Multiple",按Ctrl可以多選
7. 兩個列表框聯(lián)動,即兩級聯(lián)動菜單
//判斷第一個列表框中被選中的值
switch(ListBox1.SelectValue)
{
//如果是"A",第二個列表框中就添加這些:
case "A"
ListBox2.Items.Clear();
ListBox2.Items.Add("A1");
ListBox2.Items.Add("A2");
ListBox2.Items.Add("A3");
//如果是"B",第二個列表框中就添加這些:
case "B"
ListBox2.Items.Clear();
ListBox2.Items.Add("B1");
ListBox2.Items.Add("B2");
ListBox2.Items.Add("B3");
}
8. 實現(xiàn)列表框中項的移位
即:向上移位、向下移位
具體的思路為:創(chuàng)建一個ListBox對象,并把要移位的項先暫放在這個對象中。
如果是向上移位,就是把當前選定項的的上一項的值賦給當前選定的項,然后
把剛才新加入的對象的值,再附給當前選定項的前一項。
具體代碼為:
//定義一個變量,作移位用
index = -1;
//將當前條目的文本以及值都保存到一個臨時變量里面
ListItem lt=new ListItem (ListBox.SelectedItem.Text,ListBox.SelectedValue);
//被選中的項的值等于上一條或下一條的值
ListBox.Items[ListBox.SelectedIndex].Text=ListBox.Items[ListBox.SelectedIndex + index].Text;
//被選中的項的值等于上一條或下一條的值
ListBox.Items[ListBox.SelectedIndex].Value=ListBox.Items[ListBox.SelectedIndex + index].Value;
//把被選中項的前一條或下一條的值用臨時變量中的取代
ListBox.Items[ListBox.SelectedIndex].Test=lt.Test;
//把被選中項的前一條或下一條的值用臨時變量中的取代
ListBox.Items[ListBox.SelectedIndex].Value=lt.Value;
//把鼠標指針放到移動后的那項上
ListBox.Items[ListBox.SelectedIndex].Value=lt.Value;
9. 移動指針到指定位置:
(1).移至首條
//將被選中項的索引設(shè)置為0就OK了
ListBox.SelectIndex=0;
(2).移至尾條
//將被選中項的索引設(shè)置為ListBox.Items.Count-1就OK了
ListBox.SelectIndex=ListBox.Items.Count-1;
(3).上一條
//用當前被選中的索引去減 1
ListBox.SelectIndex=ListBox.SelectIndex - 1;
(4).下一條
//用當前被選中的索引去加 1
ListBox.SelectIndex=ListBox.SelectIndex + 1;
this.ListBox1.Items.Insertat(3,new ListItem("插入在第3行之后項",""));
this.ListBox1.Items.Insertat(index,ListItem)
ListBox1.Items.Insert(0,new ListItem("text","value"));