我试图将一个组合框值和文本框值与枚举值匹配,并将结果放在文本框中。但无法做同样的事情 [英] i am trying to match one combobox value and textbox value with enum values and putting result in textbox. but unable to do the same

查看:112
本文介绍了我试图将一个组合框值和文本框值与枚举值匹配,并将结果放在文本框中。但无法做同样的事情的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过生成随机数并将结果放入文本框来将一个组合框值和文本框值与枚举值匹配。但无法做同样的事情。这里em1和em是枚举值

i am trying to match one combobox value and textbox value with enum values by generating random number and putting result in textbox. but unable to do the same. here em1 and em is enum values

enum emotion { verysad, sad, normal, happy, veryhappy }
        emotion em = emotion.verysad;
        emotion m = emotion.sad;
        emotion o = emotion.normal;
        emotion t = emotion.happy;
        emotion i = emotion.veryhappy;
        emotion em1 = emotion.verysad;
        emotion m1 = emotion.sad;
        emotion o1 = emotion.normal;
        emotion t1 = emotion.happy;
        emotion i1 = emotion.veryhappy;
                   if (em1.ToString().Equals(val1))
                   {

                       textBox4.Text = em1.ToString();
                   }
                   else if (m1.ToString().Equals(val1))
                   {
                       --m1;
                       textBox4.Text = m1.ToString();
                   }
                   else if (o1.ToString().Equals(val1))
                   {
                       --o1;
                       textBox4.Text = o1.ToString();

                   }
                   else if (t1.ToString().Equals(val1))
                   {
                       --t1;
                       textBox4.Text = t1.ToString();
                   }
                   else if (i1.ToString().Equals(val1))
                   {
                       --i1;
                       textBox4.Text = i1.ToString();
                   }
                   if (em.ToString().Equals(val))
                   {
                       ++em;
                       textBox3.Text = em.ToString();
                   }
                   else if (m.ToString().Equals(val))
                   {
                       ++m;
                       textBox3.Text = m.ToString();
                   }
                   else if (o.ToString().Equals(val))
                   {
                       ++o;
                       textBox3.Text = o.ToString();

                   }
                   else if (t.ToString().Equals(val))
                   {
                       ++t;
                       textBox3.Text = t.ToString();
                   }
                   else if (i.ToString().Equals(val))
                   {

                       textBox3.Text = i.ToString();
                   }

推荐答案

我正在学习自我,但坚持到这里,请帮助



这就是为什么我总是说:不要试着通过查看代码,从书中学习或从课程中学习更好来学习(因为导师可以解释当你不理解时,一本书不能用不同的方式。



他们都以结构化的方式解释事物,确保以合理的顺序满足语言中的所有内容。因为你甚至不知道它们存在而完全学习你自己学习的麻烦,因为你甚至不知道它们存在 - 而且当你完全吠叫错误的树时,没有人检查你的工作并告诉你...... br />


就像你在这种情况一样。

我想到两件事:

1)不要使用如果..其他如果......还有这样的事情:有一个名为开关的控制区分是专门为它设计的。

2 )这是一种非常糟糕的做事方式:使用枚举几乎是我保留的唯一一点......



文本框。为什么?您希望用户输入随机文本吗?或者从列表中选择?如果后者 - 你确实想要一个列表,请相信我 - 为什么不使用ComboBox设置为列表?您可以使用枚举值填充它:

"i am learning by doing self but stuck here , please help"

This is why I always say: don't try to learn by looking at code, learn from a book, or better from a course (because a tutor can explain things in a different way when you don't understand, which a book can't).

They both explain things in a structured way, making sure you meet everything in the language in a sensible order. The trouble with truing to learn on your own ifs that you miss things out, because you don't even know they exist - and there is none to check your work and tell you when you are completely barking up the wrong tree...

As you are in this case.
Two things spring to mind:
1) don't use if.. Else if... Else for this kind of thing: there is a control divide called switch which is designed for it.
2) that is a very bad way to do things: using an enum is pretty much the only bit I'd keep...

The text box. Why? Do you want the user to enter random text? Or select from a list? If the latter - and you do want a list, trust me - why not use a ComboBox set as a list? You can populate that with the enum values:
cbStatus.DataSource = Enum.GetValues(typeof(emotion));

直接得到这个值:



And get the value directly:

emotion value = Enum.Parse<emotion>(cbStatus.SelectedValue.ToString());





或者只是设置你的其他文本框的值...



Or just set your other text box to the value...


这篇关于我试图将一个组合框值和文本框值与枚举值匹配,并将结果放在文本框中。但无法做同样的事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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