C#(WinForm)ComboBox和ListBox添加項及設定預設選擇項

來源:互聯網
上載者:User

Web控制項DropDownList和WinForm控制項ComboBox機制不一樣。
ComboBox沒有對應的ListItem需要自己寫一個:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WinListItem
{
    /// <summary>
    /// 選擇項類,用於ComboBox或者ListBox添加項
    /// </summary>
    public class ListItem
    {
        private string id = string.Empty;
        private string name = string.Empty;
        public ListItem(string sid, string sname)
        {
            id = sid;
            name = sname;
        }
        public override string ToString()
        {
            return this.name;
        }
        public string ID
        {
            get
            {
                return this.id;
            }
            set
            {
                this.id = value;
            }
        }
        public string Name
        {
            get
            {
                return this.name;
            }
            set
            {
                this.name = value;
            }
        }
    }
}

然後可以類似DropDownList添加項:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WinListItem
{
    public partial class MainFrm : Form
    {
        public MainFrm()
        {
            InitializeComponent();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            ListItem listItem = comboBox1.SelectedItem as ListItem;
            MessageBox.Show(listItem.ID + "," + listItem.Name);
        }

        private void MainFrm_Load(object sender, EventArgs e)
        {
            //添加項,Web控制項DropDownList有對應的ListItem
            ListItem listItem0 = new ListItem("0", "選項零");
            ListItem listItem1 = new ListItem("1", "選項一");
            ListItem listItem2 = new ListItem("2", "選項二");
            comboBox1.Items.Add(listItem0);
            comboBox1.Items.Add(listItem1);
            comboBox1.Items.Add(listItem2);
            //設定預設選擇項,DropDownList會預設選擇第一項。
            comboBox1.SelectedIndex = 0;//設定第一項為預設選擇項。
            comboBox1.SelectedItem = listItem1;//設定指定的項為預設選擇項
        }
    }
}

運行

源碼:http://revit.5d6d.com/thread-960-1-1.html

參考:c#(winform)中ComboBox和ListBox添加項完全解決

剛開始用.net 的winform開發,發現好些控制項都很難用,可能是不熟悉的原因吧,這不,一個給ComboBox添加項的問題就搞的我很頭疼,我要同時給一個項添加名字和值,怎麼都沒法加,查了查資料,又自己匯總測試了下,終於全部搞定了,現把完整的方案寫下。

用comboBox的資料繫結的方法很簡單,建一個資料來源,綁定到ComboBox上,然後指定DisplayMember和 ValueMember就可以了。但是感覺好不靈活哦,如果我要在ComboBox上再添加一項,那怎麼辦?Web裡面有ListItem, winform裡面怎麼沒有了?感覺真是不爽,網上找了個方法,自己添加一個ListItem類,然後add到items裡面,感覺還不錯,有點象web 裡面的用法了,可是問題又來了,添加的第一項怎麼變成類名了?不是我給它賦的名字,其他項又都沒有問題。於是又查到說,“因為combobox的 Item.ADD(一個任意類型的變數),而顯示的時候調用的是這個變數的ToString()方法,如果這個類沒有重載ToString(),那麼顯示的結果就是命名空間   +   類名”,於是加上重載的ToString()方法,好了,至此,我終於可以很方便的來給ComboBox和ListBox添加項了。

ListItem item = new ListItem("我是值", "我是名字");
this.lbChoiceRoom.Items.Add(item);
this.lbChoiceRoom.DisplayMember = "Name";
this.lbChoiceRoom.ValueMember = "ID";

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.