获得在C#中的组合框的文本 [英] Get the combobox text in C#

查看:114
本文介绍了获得在C#中的组合框的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我充满了值的组合框从枚举。

I filled up a combobox with the values from an Enum.

现在的组合框的文字吗?所以我用一个getter和一个setter。我有问题,阅读的文本。

Now a combobox is text right? So I'm using a getter and a setter. I'm having problems reading the text.

这里的code:

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

由于某种原因, this.typeComboBox.Text 总是返回一个空字符串,当我在组合框中选择一个项目。

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?

编辑:我是来,问题就在于时机的结论。 在时间点上,我鼓足了文本确实后,我改变了组合框,但是仍然在此之前,值将被解析为一个值。 固定的问题以不同的方式,现在,感谢所有的想法。

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.

推荐答案

我刚刚创建了一个简单的Windows窗体,一切对我来说效果不错。这里是code。

I just created a simple windows form, and everything worked okay for me. 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());
    }
}

这篇关于获得在C#中的组合框的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆