您的位置:首页 > 编程语言 > C#

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

2011-07-22 09:29 239 查看
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'), "月")));
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: