private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
combobox.items.add=("peter magdy");
if (combobox.selecteditems=("peter magdy")
textbox.text==("age 23, male, etc");
}
此代码可帮助您使用组合框中的值填充文本框
答案 0 :(得分:2)
考虑一下
// your person model where you hold person info
public class Person
{
public int Id {get; set;}
public string Name {get; set;}
public int Age {get; set;}
public string Sex {get; set;}
}
// You will hold not strings but real objects in combo
private void LoadCombo()
{
var john = new Pesron(){Id = 0, Name = "John", Age = 20, sex = "Male"};
var maria = new Pesron(){Id = 1, Name = "Maria", Age = 19, sex = "Female"};
var couple = new []{john, maria};
combobox.DataSourse = couple;
combobox.DisplayMember = "Name";
combobox.ValueMember = "Id";
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// Then you can have entire person information at your disposal
var p = (Person)combobox.SelectedItem;
textbox.text = string.Format("Name {0}, Age {1}", p.Name, p.Age);
}
答案 1 :(得分:1)
试试这个。
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox1.Items.Add("peter magdy");
if (comboBox1.SelectedItem == "peter magdy")
textBox.Text = "age 23, male, etc";
}
但是,您可能必须更改组件的名称(在代码或winform设计器中)。
答案 2 :(得分:0)
文本框具有Text
属性,可以设置/获取文本。
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
combobox.items.add=("peter magdy");
if (combobox.selecteditems=("peter magdy")
textbox.Text ="age 23, male, etc";
}
答案 3 :(得分:0)
尝试使用SelectionChangeComitted事件代替selectionIndexChange。
SelectionChangeComitted是选择的最后一个事件,它应该是将值设置为comboBox之前的位置。
当您在comboBox中使用向上和向下箭头但是comboBox的文本仍然更改时,可能不会发生SelectedIndex更改。