C# 显示组合框(项目)的值

C# 显示组合框(项目)的值,c#,combobox,C#,Combobox,我自己创建了一个名为ComboBoxItem的类 public class ComboBoxItem { public string _value; public string _text; public ComboBoxItem(string val, string text) { _value = val; _text = text; } public override string ToString()

我自己创建了一个名为ComboBoxItem的类

public class ComboBoxItem
{
    public string _value;
    public string _text;

    public ComboBoxItem(string val, string text)
    {
        _value = val;
        _text = text;
    }

    public override string ToString()
    {
        return _text;
    }
}
我以这种方式在组合框中输入了一些带有值的文本:

busstops = new ComboBoxItem("410000015503", "New Bridge Street-St Dominics");
comboBox1.Items.Add(busstops);
busstops = new ComboBoxItem("410000015552", "Bothal Street (N-Bound), Byker ");
comboBox1.Items.Add(busstops);
现在我喜欢如果我点击一个项目,然后点击一个按钮,就会出现一个消息框,显示所选项目的值

但问题是组合框只能显示“New Bridge Street…”这样的文本,因为只有文本在我的组合框中,我喜欢显示它的值

大概是这样的:

Messagebox.show(combobox.selectedCombboxItem.Value);
我需要做什么


谢谢

SelectedComboxItem返回一个对象,
MessageBox.Show()
将调用
ToString()

您需要将SelectedComboxItem强制转换为您自己的类型

Messagebox.show(((ComboBoxItem)combobox.selectedCombboxItem).Value);

Combobox将返回一个对象,您需要将该对象强制转换为
ComboBoxItem
,以便访问

Messagebox.show(((ComboBoxItem)combobox.SelectedItem).Value);

如何将值关联到组合框中的文本?你是对的,对此表示抱歉。假设我们讨论的是winforms,那么正确的值应该是Messagebox.show(((ComboBoxItem)combobox.SelectedItem.value);我刚才使用了SelectedComboxItem,因为这是您在问题中使用的。