C# WinForm ComboBox 自定义数据项 (ComboBoxItem )

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

namespace WindowsFormsApplication3
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}

public struct ComboBoxItem<TKey, TValue>
{
private TKey key;
private TValue value;

public ComboBoxItem(TKey key, TValue value)
{
this.key = key;
this.value = value;
}

public TKey Key
{
get { return key; }
}

public TValue Value
{
get { return value; }
}

public override string ToString()
{
return Value.ToString();
}
}

private void Form3_Load(object sender, EventArgs e)
{
//KeyValuePair<int, string> keys = new KeyValuePair<int,string>();
this.comboBox1.Items.Add(new ComboBoxItem<int, string>(1, "Lin"));
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
var item
= (ComboBoxItem<int, string>)this.comboBox1.SelectedItem;

Text
= item.Value;
}
}
}

  

一个 1月 到12 月的下拉单

            for (int i = 1; i <= 12; i++)

            {

                this.comboBox1.Items.Add(

                   new ComboBoxItem<int, string>(i, 

                          String.Concat(i.ToString().PadLeft(2, '0'), "月")));

            }

  

你可能感兴趣的:(combobox)