繁体   English   中英

如何使用选定的组合框项目用数据填充文本框

[英]How to populate textbox with data , using selected combobox items

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    combobox.items.add=("peter magdy");

    if (combobox.selecteditems=("peter magdy")
    textbox.text==("age 23, male, etc");
}

此代码可帮助您使用组合框中的值填充文本框

考虑一下

// 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);
}

尝试这个。

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 Designer中)。

文本框具有可以设置/获取文本的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";
}

还可以尝试查看SelectionChangeComitted事件来代替selectionIndexChange。

SelectionChangeComitted是选择的最后一个事件,应该是在将值设置为comboBox之前的位置。

当您在comboBox中使用向上和向下箭头时,SelectedIndex更改可能不会出现,但是comboBox的文本仍然更改。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM