在Winform应用程序中切换语句 [英] Switch statement in winform app

查看:81
本文介绍了在Winform应用程序中切换语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Windows窗体应用程序中使用switch语句如何通过按钮和文本框使用switch语句?
卡在此代码中,请有人帮我纠正它

I want to use switch statement in windows form application how to use switch statement through buttons and textboxes ?
am stuck in this code please somebody help me and correct it

namespace a
{

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            string s;
            s = textBox1(textBox1.Text);
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
              string s;
              textBox1("1","2");
              switch(s)
              {
                  case "1":
                      { 
                          Form2 f = new Form2();
                          f.Show();
                      }
                      break; // <<----- added by jsop
              }
        }
    }
}







但是看到我希望我提供不同的软件包,取决于用户他要选择哪个软件包,我必须应用switch语句.我该如何应用?







but see i want that i am offering different packages , and its upto the user that which package he want to choose , i have to apply the switch statement . how would i apply ?

推荐答案

就这么知道,除非您在文本框中验证地狱,否则这就是胡扯.除此之外,您的问题无法回答,因为您没有提供足够的信息.

如果您可以打开的表单数量有限,请考虑使用ComboBox而不是文本框.至少这样,您不必依靠用户的自我意识就可以在文本框中输入适当的文本.
Just so you know, that''s a crap design unless you''re validating the hell outa the text boxes. Beyond that,your question is unanswerable because you haven''t probvided enough info.

If you have a limited number of forms that could be opened, consider using a ComboBox instead of a textbox. At least that way, you don''t have to rely on the user being self-aware enough to enter the appropriate text in the textbox.


        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            string s;
            s = textBox1(textBox1.Text);
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
              string s;
              textBox1("1","2");
              switch(s)
...


您发现上述代码有什么错误(或正确)吗?


Do you see anything wrong (or right) with the above code?


首先删除此代码
First remove this
private void textBox1_TextChanged(object sender, EventArgs e)
        {
            string s;
            s = textBox1(textBox1.Text);
        }


因为您不需要这样做,而且也会产生编译错误,然后
试试这个:


as you don''t need this for what you are trying to do and will give compilation errors as well,then
Try this :

private void button1_Click(object sender, EventArgs e)
        {
              string s=textbox1.text;
             
              switch(s)
              {
                  case "1":
                      {
                          Form2 f = new Form2();
                          f.Show();
                      }
                    
                      break; 
                    case "2":
                      {
                          Form3 f3 = new Form3();
                          f3.Show();
                      }
                    
                      break; 

                     default:
                      MessageBox.Show("Please enter 1 or 2");
                      break;
              }
        }



但我建议您按照约翰的建议使用combobox,而不要使用文本框来消除用户在文本框中提供正确文本的开销.

希望对您有所帮助:)



But I would suggest you to go for combobox as suggested by John rather than using textbox to remove the overhead on user for giving the right text in textbox.

hope it helps :)


这篇关于在Winform应用程序中切换语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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