C#ListBox用法

ListBox控件顯示較長的選項列表,用戶可從中選擇一項或多項。如果項總數超出可以顯示的項數,則自動向ListBox控件添加滾動條。ListBox控件列表中的每個元素稱爲項。圖1所示爲ListBox控件

ListBox控件
1.功能
ListBox控件顯示較長的選項列表,用戶可從中選擇一項或多項。如果項總數超出可以顯示的項數,則自動向ListBox控件添加滾動條。ListBox控件列表中的每個元素稱爲項。圖1所示爲ListBox控件。

圖1 ListBox控件
2.屬性
ListBox控件常用屬性及說明如表1所示。

表1 ListBox控件常用屬性及說明
下面對比較重要的屬性進行詳細介紹。
(1)Items屬性。該屬性用於查看列表框中的項。
語法:

public ObjectCollection Items { get; }
屬性值:ListBox.ObjectCollection表示ListBox中的項。
說明:
① 該屬性使用戶可以獲取對當前存儲在 ListBox 中的項列表的引用。通過此引用,可以在集合中添加項、移除項和獲得項的計數。
② 可以使用DataSource屬性來操控ListBox的項。如果使用DataSource屬性向ListBox添加項,則可以使用Items屬性查看ListBox中的項,但不能使用ListBox.ObjectCollection的方法向該列表添加項或從中移除項。
(2)SelectedItem屬性。該屬性表示當前選中的項。
語法:
 public Object SelectedItem { get; set; }
屬性值:表示控件中當前選定內容的對象。
說明:對於標準 ListBox,可以使用此屬性確定在ListBox中選定了哪個項。如果 ListBox的SelectionMode屬性設置爲SelectionMode.MultiSimple或SelectionMode.MultiExtended(它指示多重選擇ListBox),並在該列表中選定了多個項,則此屬性可返回任何選定的項。
示例
把左邊的文本框中的內容移到右邊的文本框中
本示例主要使用Items屬性向ListBox1控件添加項,然後使用SelectedItem屬性將ListBox1控件中的選中項添加到ListBox2控件中。示例運行結果如圖2和圖3所示。

圖2  ListBox1中項移動前

圖3  ListBox1中項移動後
程序主要代碼如下:
 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.Data.SqlClient。
完整程序代碼如下:
★★★★★主程序文件完整程序代碼★★★★★
 using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace _8_05
{
static class Program
{
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmListBox());
}
}
}
★★★★★Form1窗體設計文件完整程序代碼★★★★★(責任編輯:admin)

. 屬性列表:

    SelectionMode    組件中條目的選擇類型,即多選(Multiple)、單選(Single)
    Rows             列表框中顯示總共多少行
    Selected         檢測條目是否被選中
    SelectedItem     返回的類型是ListItem,獲得列表框中被選擇的條目
    Count            列表框中條目的總數
    SelectedIndex    列表框中被選擇項的索引值
    Items            泛指列表框中的所有項,每一項的類型都是ListItem

2. 取列表框中被選中的值

     ListBox.SelectedValue

3. 動態的添加列表框中的項:

     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. 列表框可以一次選擇多項:
   
     只需設置列表框的屬性 SelectionMode="Multiple",按Ctrl可以多選

7. 兩個列表框聯動,即兩級聯動菜單

     //判斷第一個列表框中被選中的值
     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. 實現列表框中項的移位
     即:向上移位、向下移位
     具體的思路爲:創建一個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).移至首條
          //將被選中項的索引設置爲0就OK了
          ListBox.SelectIndex=0;
      (2).移至尾條
          //將被選中項的索引設置爲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"));

發佈了1 篇原創文章 · 獲贊 12 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章