【问题标题】:Show value of combobox(item)显示组合框(项目)的值
【发布时间】:2013-03-18 22:49:28
【问题描述】:

我已经创建了一个名为 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);

现在我喜欢单击一个项目并单击一个按钮,会出现一个消息框,显示所选项目的值。

但问题是组合框只能显示“新桥街...”之类的文本,因为只有文本在我的组合框中,我喜欢显示它的值..

类似这样的:

Messagebox.show(combobox.selectedCombboxItem.Value);

我需要做什么?

谢谢!

【问题讨论】:

  • 如何将值与组合框中的文本相关联?
  • 你是对的,对此感到抱歉。

标签: c# combobox


【解决方案1】:

Combobox 将返回一个对象,您需要将其转换为 ComboBoxItem 才能访问 Value

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

【讨论】:

  • 正确的,假设我们说winforms应该是Messagebox.show(((ComboBoxItem)combobox.SelectedItem).Value);我刚刚使用了 selectedCombboxItem ,因为这是您在问题中使用的。
【解决方案2】:

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

您需要将 selectedCombboxItem 转换为您自己的类型

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多