在 C# 中获取组合框文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/315237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 22:52:31  来源:igfitidea点击:

Get the combobox text in C#

c#.netwinformscombobox

提问by Vordreller

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. I'm having problems reading the text.

现在组合框是文本吗?所以我使用了一个 getter 和一个 setter。我在阅读文本时遇到问题。

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.Textalways 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.

编辑:我得出的结论是问题出在时间上。我调用文本的时间点确实是在我更改组合框之后,但仍然在该值被解析为值之前。问题现在以不同的方式解决,感谢所有的想法。

采纳答案by Rohan West

I just created a simple windows form, and everything worked okay for me. Here is the code.

我刚刚创建了一个简单的 windows 窗体,一切正常。这是代码。

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());
    }
}

回答by Vordreller

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,然后我得到错误的输出......

回答by Howler

You should try this.typeComboBox.SelectedItem.ToString()

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

回答by Dylan Beattie

Have you tried using this.typeComboBox.SelectedTextinstead of typeComboBox.Text?

您是否尝试过使用this.typeComboBox.SelectedText代替typeComboBox.Text

回答by Rune Grimstad

Set the DropDownStyleof the ComboBox to 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.SelectedItemproperty will be a value of BookType. So you can use this in the property getter and setter.

DropDownStyleComboBox 的设置为DropDownList。这将确保只能选择列表中已有的元素(无需检查文本实际上是有效值)。然后,如果您Enum.GetValues(typeof(BookType))用来填充组合框,则typeComboBox.SelectedItem属性将为BookType. 所以你可以在属性 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. Use the SelectedItem property to get an item of the wanted type instead of checking the Text property.

所以总结一下。只要使用 DropDownList 样式,就不必将组合框绑定到文本值列表。使用 SelectedItem 属性获取所需类型的项目,而不是检查 Text 属性。

Edit: You may have to check the SelectedItem property for null

编辑:您可能需要检查 SelectedItem 属性是否为空

回答by Muhammedh

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 方法分析项目并返回绑定到该项目的文本。