天天看點

winform ComboBox基本操作

1.讓使用者隻能選擇項。

很簡單,設定ComboBox的屬性DropDownStyle為DropDownList即可。

winform ComboBox基本操作

------------------------------------------------------------------------------------------

2.往ComboBox中添加新項。

 private void Form1_Load(object sender, EventArgs e)
        {
            this.cmbTest.Items.Add("A");
            this.cmbTest.Items.Add("B");
            this.cmbTest.Items.Add("C");
            this.cmbTest.Items.Add("D");
        }
           

//效果截圖如下

winform ComboBox基本操作

------------------------------------------------------------------------------------

3.設定預設選擇的項。

this.cmbTest.SelectedIndex = 3; //選擇第四項,注意,編号從0開始

winform ComboBox基本操作

--------------------------------------------------------------------------------------------

4.擷取選擇的項的文本。

MessageBox.Show(this.cmbTest.SelectedItem.ToString());

winform ComboBox基本操作

----------------------------------------------------------------------------------------------

5.周遊ComboBox控件中每一項的文本:

//從最後一項開始周遊
            for (int i = this.cmbRoomId.Items.Count - 1; i >= 0;i-- )
            {
                this.cmbRoomId.SelectedIndex=i;//選擇第i項
                if (this.cmbRoomId.Text.Trim().Equals("Admin"))//判斷項的文本
                {
                    break;//找到就停止周遊
                }
            } 
           

----------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

6.資料綁定:

在SQL2005資料庫中,我有兩列renterID和renterName

//截圖如下

winform ComboBox基本操作

現在我想讓ComboBox控件顯示的是renterName,而ComboBox實際的值卻是renterID,

我們如何做呢?

先看下我們寫的SQL語句:select renterId,renterName from Renter

有了SQL語句,怎麼擷取資料集DataSet,這個不用我說了吧?

有了資料集後,我們就可以将其綁定到ComboBox中了。

代碼如下:

string sql="select renterId,renterName from Renter";
//SqlHelper.GetConnection()的作用是擷取一個資料庫連接配接對象,這是我自己封裝的方法,請靈活應變。	
using (SqlDataAdapter da = new SqlDataAdapter(sql,SqlHelper.GetConnection()))
               {
                    DataSet ds = new DataSet();
                    da.Fill(ds);//填充資料集,即擷取資料集
                    this.cmbRenter.DataSource = ds.Tables[0].DefaultView; //設定ComboBox的資料源
                    this.cmbRenter.DisplayMember = "renterName"; //讓ComboBox顯示renterName列
                    this.cmbRenter.ValueMember = "renterId";  //讓ComboBox實際的值為renterId列
                } 
           

資料綁定好後,我們如何擷取renterId的值呢?代碼如下:

MessageBox.Show(this.cmbRenter.SelectedValue.ToString()); 

//效果截圖如下:

winform ComboBox基本操作

7.觸發change事件

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //第一種方法擷取值
            string combobox1_value=this.comboBox1.Text;  
            //綁定後就會觸發   第一次觸發擷取不到值 應該做個判斷  比如 !combobox1_index.Contains("System")
            string combobox1_index=this.comboBox1.SelectedValue.ToString();
            Console.WriteLine("combobox1_value==="+combobox1_value);
            Console.WriteLine("combobox1_index==="+combobox1_index);
 
 
            Console.WriteLine("===============分割線==================");
 
            //第二種方法擷取值
           var Vcombobox1_value=this.comboBox1.SelectedItem as Region; 
            Console.WriteLine("name==="+Vcombobox1_value.name);
            Console.WriteLine("id==="+Vcombobox1_value.id);
 
        }