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

时间:2023-01-17 07:08:00

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. I'll need to be able to click down the list and watch the textbox text update with each click.

我想在列表框中选择一个项目(winforms),然后在附近的文本框中显示与该项目相关的一些数据,但我不知道要使用哪个事件。我需要能够点击列表并在每次点击时观看文本框文本更新。

Thanks

6 个解决方案

#2


You will want to handle either SelectedIndexChanged or SelectedValueChanged.

您将需要处理SelectedIndexChanged或SelectedValueChanged。

(Note that the SelectedValueChanged MSDN article has an example that sounds like exactly what you are doing.)

(请注意,SelectedValueChanged MSDN文章的示例听起来就像您正在做的那样。)

#3


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

#4


I think this will help you out.

我想这会帮到你。

#5


Sorry, don't know the exact name of the event off the top of my head, but is something like SelectedItemChanged that you are looking for.

对不起,不知道我头顶的事件的确切名称,但是像您正在寻找的SelectedItemChanged。

#6


Is the SelectedIndexChanged event not working for you?

SelectedIndexChanged事件不适合您吗?

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

#1


#2


You will want to handle either SelectedIndexChanged or SelectedValueChanged.

您将需要处理SelectedIndexChanged或SelectedValueChanged。

(Note that the SelectedValueChanged MSDN article has an example that sounds like exactly what you are doing.)

(请注意,SelectedValueChanged MSDN文章的示例听起来就像您正在做的那样。)

#3


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

#4


I think this will help you out.

我想这会帮到你。

#5


Sorry, don't know the exact name of the event off the top of my head, but is something like SelectedItemChanged that you are looking for.

对不起,不知道我头顶的事件的确切名称,但是像您正在寻找的SelectedItemChanged。

#6


Is the SelectedIndexChanged event not working for you?

SelectedIndexChanged事件不适合您吗?

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