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

C# WinForm ComboBox 枚举 选定值

2011-07-21 14:15 441 查看
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.银行支付;
}
}
}


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