简体   繁体   English

在 C# 中获取组合框文本

[英]Get the combobox text in C#

I filled up a combobox with the values from an Enum.我用枚举中的值填充了一个组合框。

Now a combobox is text right?现在组合框是文本对吗? So I'm using a getter and a setter.所以我使用了一个getter和一个setter。 I'm having problems reading the text.我在阅读文本时遇到问题。

Here's the code:这是代码:

public BookType type
{
    get
    {
        return (BookType)Enum.Parse(typeof(BookType), this.typeComboBox.Text);
    }
    set
    {
        this.typeComboBox.Text = value.ToString();
    }
}

For some reason, this.typeComboBox.Text always returns an empty string when I select an item on the combobox.出于某种原因,当我在组合框中选择一个项目时, this.typeComboBox.Text总是返回一个空字符串。

Does someone see what I'm doing wrong?有人看到我做错了吗?

EDIT: I have come to the conclusion that the problem lies in timing.编辑:我得出的结论是问题出在时间上。 The point in time at which I summon the text is indeed after I changed the combobox, but still before that value is parsed as a value.我召唤文本的时间点确实是在我更改组合框之后,但仍然在该值被解析为值之前。 Problem fixed in a different way now, thanks for all the ideas.现在以不同的方式解决了问题,感谢所有想法。

Try this.尝试这个。 this worked for me.这对我有用。

string selectedText = this.ComboBox.GetItemText(this.ComboBox.SelectedItem);

The GetItemText method analyzes the item and returns the text of the bound to that item. GetItemText 方法分析项目并返回绑定到该项目的文本。

Set the DropDownStyle of the ComboBox to DropDownList .将 ComboBox 的DropDownStyle设置为DropDownList This will ensure that only the elements already in the list can be selected (no need to check that the text actually is a valid value).这将确保只能选择列表中已有的元素(无需检查文本实际上是否为有效值)。 Then if you use Enum.GetValues(typeof(BookType)) to fill the combobox then typeComboBox.SelectedItem property will be a value of BookType .然后,如果您使用Enum.GetValues(typeof(BookType))填充组合框,则typeComboBox.SelectedItem属性将是BookType的值。 So you can use this in the property getter and setter.所以你可以在属性 getter 和 setter 中使用它。

So to summarize.所以总结一下。 You don't have to bind the combobox to a list of text values as long as you use the DropDownList style.只要您使用 DropDownList 样式,就不必将组合框绑定到文本值列表。 Use the SelectedItem property to get an item of the wanted type instead of checking the Text property.使用 SelectedItem 属性来获取所需类型的项目,而不是检查 Text 属性。

Edit: You may have to check the SelectedItem property for null编辑:您可能必须检查 SelectedItem 属性是否为空

The combobox starts at index -1, which has no text, thus an empty string: ""组合框从索引 -1 开始,它没有文本,因此是一个空字符串:“”

I then change the index to a BookType that I need and then I get the wrong output...然后我将索引更改为我需要的 BookType,然后我得到错误的输出......

你应该试试 this.typeComboBox.SelectedItem.ToString()

您是否尝试过使用this.typeComboBox.SelectedText而不是typeComboBox.Text

I just created a simple windows form, and everything worked okay for me.我刚刚创建了一个简单的 Windows 窗体,对我来说一切正常。 Here is the code.这是代码。

public enum Test
{
    One, Two, Three
}

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        this.comboBox1.DataSource = Enum.GetNames(typeof(Test));
    }

    public Test Test
    {
        get 
        {
            return (Test)Enum.Parse(typeof(Test), this.comboBox1.Text);
        }
        set
        {
            this.comboBox1.Text = value.ToString();
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show(this.Test.ToString());

        this.Test = Test.Two;

        MessageBox.Show(this.Test.ToString());
    }
}
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var comboBox = (ComboBox)sender;
    ComboBoxItem comboAsTextblock = (ComboBoxItem)comboBox.SelectedItem;
    string comboBoxItemText = comboAsTextblock.Content.ToString();
    // comboBoxItemText is what you want :)
}

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

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