C# WinForm ComboBox 枚举 选定值
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 Form2 : Form
{
public Form2()
{
InitializeComponent();
this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
}
[Flags]
[Serializable]
enum Kind
{
现金支付 = 0x0001,
银行支付 = 0x0002,
}
private void Form2_Load(object sender, EventArgs e)
{
var items = new System.Collections.ArrayList(Enum.GetValues(typeof(Kind))).ToArray();
//Items 加载 默认选择为空
//DataSource 加载 默认选择为首项
this.comboBox1.Items.Clear();
this.comboBox1.Items.AddRange(items);
//this.comboBox1.DataSource = items;
}
private void button1_Click(object sender, EventArgs e)
{
if (this.comboBox1.SelectedItem != null)
{
var item = (Kind)this.comboBox1.SelectedItem;
int value = item.GetHashCode(); //value = 1
}
//设置 combobox 的值
//Items DataSource 都适用
this.comboBox1.SelectedItem = Kind.银行支付;
}
}
}