Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
566 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...