5 Replies - 559 Views - Last Post: 09 March 2013 - 03:23 PM Rate Topic: -----

#1 gig  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 18-October 12

a button switch case

Posted 09 March 2013 - 01:29 PM

private void btnEnter_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            

            switch (btn)
            {
                case 1: if (Ggchoice = "Get Group" & txtgroupID != null)
                        dataGridViewGroups.DataSource = gr.GetGroup(int.Parse(txtgroupID.Text));
                    else 
                {
                    MessageBox.Show("please insert corrent GroupID");
                }
                    break;


Can someone help me in defining the BTN in the SWITCH? I want any time that pressing the BUTTON the SWITCH CASE will activated

This post has been edited by tlhIn`toq: 09 March 2013 - 02:29 PM
Reason for edit:: code tags


Is This A Good Question/Topic? 0
  • +

Replies To: a button switch case

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5628
  • View blog
  • Posts: 12,073
  • Joined: 02-June 10

Re: a button switch case

Posted 09 March 2013 - 02:30 PM

case btnEnter:
   // blah blah
   break;
case btnSend:
   // blah
   break;

Was This Post Helpful? 0
  • +
  • -

#3 gig  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 18-October 12

Re: a button switch case

Posted 09 March 2013 - 02:58 PM

the problem is in line switch(btn)
the cases are case 1:, case 2:, .....and they are ok...
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5628
  • View blog
  • Posts: 12,073
  • Joined: 02-June 10

Re: a button switch case

Posted 09 March 2013 - 03:03 PM

Then you have a problem.

You are switching based on btn defined in line 3.
And that is being filled as whichever button is being passed in.
It looks like you are planning to subscribe several buttons to this one handler. So btnEnter, btnSend, btnWidget etc are all going to call this method. Is that right? If not, then there is no explanation for lines 3 and 6.

In what way do you think the case conditions of 1, 2, 3, 4 ... are right? Where are these conditions coming from or what do they represent?

This post has been edited by tlhIn`toq: 09 March 2013 - 03:04 PM

Was This Post Helpful? 0
  • +
  • -

#5 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: a button switch case

Posted 09 March 2013 - 03:05 PM

You can only use integral or string types in a switch statement.

Therefore, the easiest option available to you is probably to switch on the Name of the button, rather than the button itself. Alternatively, I guess you could assign each button an integer in the Tag property, and extract, parse/cast and switch on that in the click event handler method.

Alternatively, you could use an if-else construct to compare the sender reference to the buttons you have on your form:

if (sender == btn1) { ... }
else if (sender == btn2) { ... }
etc...



However, none of those options are particularly good. Can't you just avoid all this by creating a separate click event handler method for each button? Let the delegate backing the Click event handle all the command routing for you :)

This post has been edited by CodingSup3rnatur@l-360: 09 March 2013 - 03:17 PM
Reason for edit:: Grammar + Added note about if-else

Was This Post Helpful? 1
  • +
  • -

#6 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5628
  • View blog
  • Posts: 12,073
  • Joined: 02-June 10

Re: a button switch case

Posted 09 March 2013 - 03:23 PM

View PostCodingSup3rnatur@l-360, on 09 March 2013 - 04:05 PM, said:

You can only use integral or string types in a switch statement.


Good catch. I guess it just goes to show we see what we *expect* to see sometimes.
I'm so used to do this sort of thing
string btn = ((Button) sender).Name.ToLower();
switch (btn)
{
   case "btnexit":
      //blah
   break;
}

I just saw what I expected to see: Taking the name and switching on that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1