欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#(WinForm) ComboBox和ListBox添加項(xiàng)及設(shè)置默認(rèn)選擇項(xiàng)

 更新時(shí)間:2014年07月23日 16:14:59   投稿:mdxy-dxy  
這篇文章主要介紹了C#(WinForm) ComboBox和ListBox添加項(xiàng)及設(shè)置默認(rèn)選擇項(xiàng)的的相關(guān)資料,需要的朋友可以參考下

Web控件DropDownList和WinForm控件ComboBox機(jī)制不一樣。
ComboBox沒(méi)有對(duì)應(yīng)的ListItem需要自己寫一個(gè):

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

namespace WinListItem
{
  /// <summary>
  /// 選擇項(xiàng)類,用于ComboBox或者ListBox添加項(xiàng)
  /// </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添加項(xiàng):

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)
    {
      //添加項(xiàng),Web控件DropDownList有對(duì)應(yīng)的ListItem
      ListItem listItem0 = new ListItem("0", "選項(xiàng)零");
      ListItem listItem1 = new ListItem("1", "選項(xiàng)一");
      ListItem listItem2 = new ListItem("2", "選項(xiàng)二");
      comboBox1.Items.Add(listItem0);
      comboBox1.Items.Add(listItem1);
      comboBox1.Items.Add(listItem2);
      //設(shè)置默認(rèn)選擇項(xiàng),DropDownList會(huì)默認(rèn)選擇第一項(xiàng)。
      comboBox1.SelectedIndex = 0;//設(shè)置第一項(xiàng)為默認(rèn)選擇項(xiàng)。
      comboBox1.SelectedItem = listItem1;//設(shè)置指定的項(xiàng)為默認(rèn)選擇項(xiàng)
    }
  }
}

運(yùn)行如圖:

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

剛開始用.net 的winform開發(fā),發(fā)現(xiàn)好些控件都很難用,可能是不熟悉的原因吧,這不,一個(gè)給ComboBox添加項(xiàng)的問(wèn)題就搞的我很頭疼,我要同時(shí)給一個(gè)項(xiàng)添加名字和值,怎么都沒(méi)法加,查了查資料,又自己匯總測(cè)試了下,終于全部搞定了,現(xiàn)把完整的方案寫下。

用comboBox的數(shù)據(jù)綁定的方法很簡(jiǎn)單,建一個(gè)數(shù)據(jù)源,綁定到ComboBox上,然后指定DisplayMember和 ValueMember就可以了。但是感覺(jué)好不靈活哦,如果我要在ComboBox上再添加一項(xiàng),那怎么辦?Web里面有ListItem, winform里面怎么沒(méi)有了?感覺(jué)真是不爽,網(wǎng)上找了個(gè)方法,自己添加一個(gè)ListItem類,然后add到items里面,感覺(jué)還不錯(cuò),有點(diǎn)象web 里面的用法了,可是問(wèn)題又來(lái)了,添加的第一項(xiàng)怎么變成類名了?不是我給它賦的名字,其他項(xiàng)又都沒(méi)有問(wèn)題。于是又查到說(shuō),“因?yàn)閏ombobox的 Item.ADD(一個(gè)任意類型的變量),而顯示的時(shí)候調(diào)用的是這個(gè)變量的ToString()方法,如果這個(gè)類沒(méi)有重載ToString(),那么顯示的結(jié)果就是命名空間 + 類名”,于是加上重載的ToString()方法,好了,至此,我終于可以很方便的來(lái)給ComboBox和ListBox添加項(xiàng)了。

復(fù)制代碼 代碼如下:

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

相關(guān)文章

最新評(píng)論