Click here to Skip to main content
15,908,013 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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
C#
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
              }
        }
    }
}






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 ?
Posted
Updated 7-Nov-11 7:52am
v5
Comments
Uday P.Singh 7-Nov-11 13:33pm    
what are you looking for?
zahra1991 7-Nov-11 13:34pm    
i am looking for the correct syntax , this code is giving some errors . kindly correct it. i have tried alot
Uday P.Singh 7-Nov-11 13:36pm    
can you tell me what are trying to do?
#realJSOP 7-Nov-11 13:39pm    
If it's a copiler error (and it probably is), try adding a break statement after the closing curly brace in the case part of the switch statement. Also, what is textbox1("1", "2"); doing?
zahra1991 7-Nov-11 13:38pm    
i actually want that whatever user enters in the textbox and when click on the enter buttn , i must open the next forms , let suppose if the user enters "1" it must open the form 2, similarly if he enters "2" it should open form 3

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.
 
Share this answer
 
v2
Comments
zahra1991 7-Nov-11 13:48pm    
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 ?
Uday P.Singh 7-Nov-11 14:08pm    
5ed
C#
        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?
 
Share this answer
 
Comments
zahra1991 7-Nov-11 13:54pm    
it is wrong dats y it is giving errors
Richard MacCutchan 7-Nov-11 14:03pm    
Brilliant; go to the top of the class. Come on, you are a developer and you cannot step through this code (with your eyes) and see that it cannot possibly work? Where does the value for your switch statement come from?
#realJSOP 7-Nov-11 14:50pm    
If you guessed that it came from his ass, you'd be right on the money.
Richard MacCutchan 7-Nov-11 15:59pm    
LOL!
PJ du Preez 7-Nov-11 13:59pm    
get rid of the TextChanged event
First remove this
C#
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 :

C#
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;
              }
        }


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 :)
 
Share this answer
 
v4
Comments
zahra1991 7-Nov-11 14:05pm    
thankyou so much , now its working , u know i spent more than 3 hours while fixing it , but i cant use default statement why ? and how to use combo box ? what is its purpose ?
zahra1991 7-Nov-11 14:20pm    
for combo box i should write combobox.text ?
PJ du Preez 7-Nov-11 14:30pm    
combobox.selecteditem
zahra1991 7-Nov-11 14:59pm    
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string s;
s=comboBox1.SelectedItem;
switch(s)
{
case "1":
Form2 f=new Form2();
f.Show();
break;
case"2":
Form3 a=new Form3();
a.Show();
break;
}
}



}
}


it is giving me some error :(
PJ du Preez 7-Nov-11 14:29pm    
You can load the options you want available into the combobox list using combobox.items.add() and then switch on the combobox.selecteditem. it is easier and you do not have to worry about the default
correct syntax for switch is:

C#
switch (variable)
{
case possible_value_1:
//code whatever should happen here
break;

case possible_value_2:
//code whatever should happen here
break;

case possible_value_3:
//code whatever should happen here
break;

default: //always have a default!!
//code whatever should happen here

}


for example:

C#
private void button1_Click(object sender, EventArgs e)
{
            string s;
            s = textbox1.text;

            switch (s)
            {
                 case "2":
                      form3 = new Form();
                      form3.show();
                      break;
                 case "3":
                      form4 = new Form();
                      form4.show();
                      break;
                 //case default:
                     default:
                      MessageBox.Show("You have entered a wrong value!");
                      
            }
}


hope this helps!
 
Share this answer
 
v5
Comments
PJ du Preez 7-Nov-11 14:01pm    
if you are using Visual Studio, just type "switch" and press the tab button twice. the IDE will generate a switch structure for you, just add the rest.
PJ du Preez 7-Nov-11 14:20pm    
agreed, but the question was not about design, so I gave an answer in the context of the question
Uday P.Singh 7-Nov-11 14:28pm    
Sorry but I downvoted as your code will not compile, first you don't need to use case with default and added break statement to avoid fall through. But still you have not created the object of form3 and from4.
PJ du Preez 7-Nov-11 14:34pm    
Oops, thanx for the break; Force of habbit. Default is good to have
Uday P.Singh 7-Nov-11 14:36pm    
Please review your code again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900