简体   繁体   English

.NET C#Windows窗体,列表框控件问题

[英].Net C# windows forms, listbox control question

I have a pretty simple form with a listbox, a text box, and two buttons. 我有一个非常简单的表单,其中包含一个列表框,一个文本框和两个按钮。

The list box items are populated from a sql database table. 列表框项是从sql数据库表填充的。 The user may chose to select one or multiple items from the listbox. 用户可以选择从列表框中选择一项或多项。

The text box is used to write more details about the items in the listbox. 该文本框用于编写有关列表框中各项的更多详细信息。 One button can then be clicked to update another database table with these details. 然后可以单击一个按钮以使用这些详细信息更新另一数据库表。

I want to make it where if any items are selected from the listbox, those contents are automatically copied into the text box field on the fly as they are selected. 我要使其成为一个位置,如果从列表框中选择了任何项目,则这些内容将在选择时即时自动复制到文本框字段中。 Is this possible? 这可能吗?

I've been able to make this happen on the button click event - just not on the fly as they are selected. 我已经能够在按钮单击事件中做到这一点-只是在选择它们时不是动态进行的。 I want it to occur before the additional details are being sent to the database 我希望在其他详细信息发送到数据库之前发生

I've also tried using several different listbox events, but have been unable to obtain the results I am looking for. 我也尝试过使用几个不同的列表框事件,但是无法获得我想要的结果。

Any suggestions? 有什么建议么?

yes, the SelectedIndexChanged event fires on every selection change, and you can concatenate together the items in the listbox. 是的,在每次选择更改时都会触发SelectedIndexChanged事件,并且您可以将列表框中的项目连接在一起。 But if you are talking the description that's not visible too, you need to store the description in each listboxitem tag property, and in your code retrieve the description from there. 但是,如果您说的也是不可见的描述,则需要将描述存储在每个listboxitem标记属性中,并在代码中从那里检索描述。

try this out you will have to handle the SelectedIndexChanged event on the listbox. 试试看,您将必须处理列表框上的SelectedIndexChanged事件。 here is an example with example controls. 这是带有示例控件的示例。

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        textBox1.Text = "";
        foreach (string nextitem in listBox1.SelectedItems)
        {
            textBox1.Text += nextitem + " ";
        }
    }

im not too sure HOW you want the text to appear in the textbox so that would be up to you in the foreach loop. 我不太确定您希望文本如何出现在文本框中,以便在foreach循环中由您自己决定。

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

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