简体   繁体   English

选择项目后如何使组合框输出到文本框

[英]How to get a Combo box to output into text boxes after item selection

What I'd like to do is once the user has selected an item from the combo box, for it to then populate the text boxes with the corresponding data. 我想做的是一旦用户从组合框中选择了一个项目,然后用相应的数据填充文本框。

The error's I'm getting are: 我得到的错误是:

The best overloaded method match for 'System.Collections.Generic.List.this[int]' has some invalid arguments “ System.Collections.Generic.List.this [int]”的最佳重载方法匹配具有一些无效的参数

and

Argument '1': cannot convert from 'object' to 'int' 参数“ 1”:无法从“对象”转换为“ int”

Here is a section of my code: 这是我的代码的一部分:

List<Venue> Ven = new List<Venue>();

    private void cboVenue_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            txtVenue.Text = Ven[cboVenue.SelectedItem].m_VenName;

        }
        catch
        {
        }
    }

Please, any help would really be appreciated. 请,任何帮助将不胜感激。 Thanks 谢谢

If you are using databinding (or even if you are populating the combobox manually), just use databinding anyways... 如果您正在使用数据绑定(或者即使您是手动填充组合框),则无论如何都要使用数据绑定...

<ComboBox x:Name="cmbBox" ItemsSource="{Binding Path=Ven}" />
<TextBox Text="{Binding Path=SelectedValue, ElementName=cmbBox}" />

Note that you want to grab the SelectedValue, not the SelectedIndex or SelectedItem. 请注意,您要获取SelectedValue,而不是SelectedIndex或SelectedItem。 Though, depending on how you setup your combobox the SelectedItem might be equivalent to the SelectedValue... still, use SelectedValue. 但是,取决于设置组合框的方式,SelectedItem可能等同于SelectedValue ...,仍然使用SelectedValue。

Try this: 尝试这个:

    txtVenue.Text = Ven[cboVenue.SelectedIndex].m_VenName;

You also have to check that the Index is >= 0, an Index of -1 is for "nothing selected" 您还必须检查索引> = 0,索引-1表示“未选择任何内容”

try to replace: 尝试替换:

txtVenue.Text = Ven[cboVenue.SelectedItem].m_VenName;

with: 与:

txtVenue.Text = Ven[cboVenue.SelectedIndex].m_VenName;

A combobox's SelectedItem property is an object not an int . 组合框的SelectedItem属性是一个对象,而不是一个int So when you try to access an item in your list you're getting the error. 因此,当您尝试访问列表中的项目时,会收到错误消息。

If you have bound the data to the combobox such that SelectedItem does contain the list index value (but just as an object) all you need to do is cast it to an int and then use it to find the value in your list. 如果您已将数据绑定到组合框,使得SelectedItem确实包含列表索引值(但只是作为一个对象),则您需要做的所有事情就是将其强制转换为int, 然后使用它在列表中查找值。

Eg 例如

int index = Convert.ToInt32(cboVenue.SelectedItem)  

Then Ven[index] will contain what you need. 然后,Ven [index]将包含您需要的内容。

Alternatively you may need to look at the SelectedText, SelectedValue or Selectedindex properties of the combobox to work back to the value you require. 或者,您可能需要查看组合框的SelectedText,SelectedValue或Selectedindex属性,以重新获得所需的值。

Try SelectedIndex instead of SelectedItem. 尝试使用SelectedIndex而不是SelectedItem。 it's integer. 它是整数。

List<Venue> Ven = new List<Venue>();

private void cboVenue_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        txtVenue.Text = Ven[cboVenue.SelectedIndex].m_VenName;
    }
    catch
    {
    }
}

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

相关问题 如何从combo_box选择中自动填充单选按钮,一些combo_box,许多text_box和日历 - How to autofill a radio_button, a few combo_boxes, many text_boxes, and a calendar FROM a combo_box selection 从组合框中的选定项目填充文本框 - populating text boxes from selected item in combo box 如何获取组合框中所选项目的内容? - how to get the content of selected item in combo box? 如何在应用程序重新启动之间保持组合框中的项目选择 - How to persist the selection of an item in a combo box between application restarts C#使用仅1个选择的txt文件中的值填充2个组合框和1个文本框 - C# populating 2 combo boxes and 1 text box with values from a txt file with only 1 selection 具有共享绑定的多个组合框-首次从框中选择后显示错误 - Multiple Combo Boxes with shared Binding - Display error after first selection from box 组合框选择项返回错误 - Combo box selection item returning an error 从数据库获取多个值,并在索引更改C#时显示在组合框和不同的文本框中 - get multiple values form the database and show in combo box and different text boxes when the index change c# 如何在其他组合框的基础上填充组合框 - How do I fill a combo box bases on other combo boxes 如何从组合框中获取选定的项目值 - How to get the selected Item value from combo box
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM