凑合看看 private void button2_Click(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.Add("性别"); dt.Columns.Add("1/0"); DataRow dr = dt.NewRow(); dr["性别"] = "男"; dr["1/0"] = "1"; dt.Rows.Add(dr); comboBox1.DataSource = dt; comboBox1.DisplayMember="性别"; comboBox1.ValueMember= "1/0"; comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged); } void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { //throw new Exception("The method or operation is not implemented."); MessageBox.Show(this.comboBox1.SelectedValue.ToString()); }
尽管 ComboBox 通常用于显示文本项,您仍可向 ComboBox 添加任何对象。 using System; using System.Windows.Forms;class Form1 : Form { Form1() { ComboBox combobox1 = new ComboBox(); combobox1.Parent = this; combobox1.Items.Add(new Sex(1)); // 值为1,显示为 "男" combobox1.Items.Add(new Sex(0)); // 值为0,显示为 "女" }
static void Main() { Application.Run(new Form1()); } }struct Sex { public int sex;
public Sex(int sex) { this.sex = sex; }
public override string ToString() { return sex == 0 ? "女" : "男"; } }
1、定义一个类Public Class cboTextValueItem Private _text As String Private _value As Integer Public Sub New(ByVal text As String, ByVal value As Integer) _text = text _value = value End Sub Public Property Text() As String Get Return _text End Get Set(ByVal value As String) _text = value End Set End Property Public Property Value() As Integer Get Return _value End Get Set(ByVal value As Integer) _value = value End Set End Property '重要 Public Overrides Function ToString() As String Return _text End Function End Class 2、填充ComboBoxdim tv as new cboTextValueItem("男",1) ComboBox1.items.add(tv) 3、获取选择项dim tv as cboTextValueItem=trycast(ComboBox1.SelectedItem,cboTextValueItem)if not isnothing(tv) '通过tv的Text属性和Value属项来取得选中的项目内容 endif
comboBox1.Items.Insert(0, new ComboBoxItem("0","请选择")); comboBox1.Items.Insert(1, new ComboBoxItem("1","根目录")); internal class ComboBoxItem:Object { private string value; private string text; public ComboBoxItem(string _value, string _text) { value = _value; text = _text; } public string Text { get { return text; } } public string Value { get { return value; } } public override string ToString() { return text; } }
using System; using System.Windows.Forms;class Form1 : Form { Form1() { ComboBox combobox1 = new ComboBox(); combobox1.Parent = this; combobox1.Items.Add(new Sex(1)); // 值为1,显示为 "男" combobox1.Items.Add(new Sex(0)); // 值为0,显示为 "女" combobox1.SelectedIndex = 0;
string[] wdList = new string[] {"北风","东北偏北风","东北风","东北偏东风","东风","东南偏东风","东南风","东南偏南风","南风", "西南偏南风","西南风","西南偏西风","西风","西北偏西风","西北风","西北偏北风"}; for (int i = 0; i < wdList.Length; i++) { comboBox1.Items.Insert(i, new ComboBoxItem(i + 1, wdList[i])); } 为什么我的程序提示错误: 错误1 找不到类型或命名空间名称“ComboBoxItem”(是否缺少 using 指令或程序集引用?)
ValueMember 属性 (值)
private void button2_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("性别");
dt.Columns.Add("1/0");
DataRow dr = dt.NewRow();
dr["性别"] = "男";
dr["1/0"] = "1";
dt.Rows.Add(dr);
comboBox1.DataSource = dt;
comboBox1.DisplayMember="性别";
comboBox1.ValueMember= "1/0";
comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
} void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//throw new Exception("The method or operation is not implemented.");
MessageBox.Show(this.comboBox1.SelectedValue.ToString());
}
using System.Windows.Forms;class Form1 : Form
{
Form1()
{
ComboBox combobox1 = new ComboBox();
combobox1.Parent = this;
combobox1.Items.Add(new Sex(1)); // 值为1,显示为 "男"
combobox1.Items.Add(new Sex(0)); // 值为0,显示为 "女"
}
static void Main()
{
Application.Run(new Form1());
}
}struct Sex
{
public int sex;
public Sex(int sex)
{
this.sex = sex;
}
public override string ToString()
{
return sex == 0 ? "女" : "男";
}
}
Private _text As String
Private _value As Integer Public Sub New(ByVal text As String, ByVal value As Integer)
_text = text
_value = value
End Sub Public Property Text() As String
Get
Return _text
End Get
Set(ByVal value As String)
_text = value
End Set
End Property Public Property Value() As Integer
Get
Return _value
End Get
Set(ByVal value As Integer)
_value = value
End Set
End Property
'重要
Public Overrides Function ToString() As String
Return _text
End Function
End Class
2、填充ComboBoxdim tv as new cboTextValueItem("男",1)
ComboBox1.items.add(tv)
3、获取选择项dim tv as cboTextValueItem=trycast(ComboBox1.SelectedItem,cboTextValueItem)if not isnothing(tv)
'通过tv的Text属性和Value属项来取得选中的项目内容
endif
comboBox1.Items.Insert(0, new ComboBoxItem("0","请选择"));
comboBox1.Items.Insert(1, new ComboBoxItem("1","根目录")); internal class ComboBoxItem:Object
{
private string value;
private string text;
public ComboBoxItem(string _value, string _text)
{
value = _value;
text = _text;
} public string Text
{
get { return text; }
} public string Value
{
get { return value; }
} public override string ToString()
{
return text;
} }
using System.Windows.Forms;class Form1 : Form
{
Form1()
{
ComboBox combobox1 = new ComboBox();
combobox1.Parent = this;
combobox1.Items.Add(new Sex(1)); // 值为1,显示为 "男"
combobox1.Items.Add(new Sex(0)); // 值为0,显示为 "女"
combobox1.SelectedIndex = 0;
Button button1 = new Button();
button1.Parent = this;
button1.Text = "看看选择了什么";
button1.Top = 50;
button1.Width = 120;
button1.Click += delegate
{
Sex selected = (Sex)combobox1.SelectedItem;
MessageBox.Show(string.Format("选择的值是:{0}\r\n显示为:{1}", selected.sex, selected));
};
}
static void Main()
{
Application.Run(new Form1());
}
}struct Sex
{
public int sex;
public Sex(int sex)
{
this.sex = sex;
}
public override string ToString()
{
return sex == 0 ? "女" : "男";
}
}
itm.Columns.Add(New DataColumn("text", GetType(String)))
itm.Columns.Add(New DataColumn("value", GetType(Integer)))
itm.Rows.Add("a", 1)
itm.Rows.Add("b", 2)
CombPZ.DisplayMember = "text"
CombPZ.ValueMember = "value"
CombPZ.DataSource = itm
你的方法绑上去可以,可是如果我有从数据库里读出了性别,在怎么显示出对应的名字啊?
lz应该是想要知道这一句吧,,
您这个绑定能执行吗??我的代码跟你的差不多啊,但是每次执行绑定操作,就跳到 Indexchange函数中了??
我只是把绑定操作放在了
form_load 函数中了。
"西南偏南风","西南风","西南偏西风","西风","西北偏西风","西北风","西北偏北风"};
for (int i = 0; i < wdList.Length; i++)
{
comboBox1.Items.Insert(i, new ComboBoxItem(i + 1, wdList[i]));
}
为什么我的程序提示错误:
错误1 找不到类型或命名空间名称“ComboBoxItem”(是否缺少 using 指令或程序集引用?)
for (int i = 0; i < wdList.Length; i++)
{
comboBox1.Items.Insert(i, new ComboBoxItem((i+1).ToString(), wdList[i]));
}