简体   繁体   English

C#当我在列表框中选择一个项目时,应该使用哪个事件在文本框中显示数据?

[英]C# Which Event should I use to display data in a textbox when I select an item in a listbox?

C# Which Event should I use to display data in a textbox when I select an item in a listbox? C#当我在列表框中选择一个项目时,应该使用哪个事件在文本框中显示数据?

I want to select an item in a list box (winforms) and then a textbox near by show some data related to that item but I don't know which event to use. 我想在列表框中选择一个项目(winforms),然后在附近的文本框中显示与该项目相关的一些数据,但是我不知道要使用哪个事件。 I'll need to be able to click down the list and watch the textbox text update with each click. 我需要能够单击列表,并单击每次观看文本框文本更新。

Thanks 谢谢

You will want to handle either SelectedIndexChanged or SelectedValueChanged . 您将要处理SelectedIndexChangedSelectedValueChanged

(Note that the SelectedValueChanged MSDN article has an example that sounds like exactly what you are doing.) (请注意, SelectedValueChanged MSDN文章中的示例听起来像您正在做的事情。)

Assuming you have a form with a TextBox and a ListBox. 假设您有一个带有TextBox和ListBox的表单。

    public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        textBox1.Text = listBox1.SelectedItem.ToString();
    }
}

我认为会帮助您。

抱歉,我不知道该事件的确切名称,但是您正在寻找的是SelectedItemChanged之类的东西。

Is the SelectedIndexChanged event not working for you? SelectedIndexChanged事件对您不起作用吗?

private void listBox1_SelectedIndexChanged(object sender, EventArgs e) {
    relatedTextbox.Text = listBox1.SelectedItem.ToString();
}

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

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