473,327 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Switch for selected control in common event ?

Hi !

I have several buttons and a common click event for all of them. How can I
get the reference to the clicked control in a switch?

This does not work:

private void Button_Click(object sender, System.EventArgs e)
{
switch ((Button)sender)
{
case Button1:
...
break;
case Button2:
....
break;
default:
break;
}
}
Nov 17 '05 #1
3 1624
Hi Frank,

Since the Button1 reference may change you can't use it in a switch, only const variables allowed.

You need to use if/else if/else blocks.
On Thu, 26 May 2005 10:07:42 +0200, Frank Esser <Mi*****@nurfuerspam.de> wrote:
Hi !

I have several buttons and a common click event for all of them. How can I
get the reference to the clicked control in a switch?

This does not work:

private void Button_Click(object sender, System.EventArgs e)
{
switch ((Button)sender)
{
case Button1:
...
break;
case Button2:
....
break;
default:
break;
}
}


--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #2
you can give ur buttons Tag property different values and switch.
ur code would be like :
private void Button_Click(object sender, System.EventArgs e)
{
switch ( (int)(((Button)sender).Tag) )
{
case 1:
...
break;
case 2:
....
break;
default:
break;
}
}

Nov 17 '05 #3
You can use the Name property:

switch( ((Button)sender).Name)
{
case "Button1":
// :
break;
case "Button2" :
// :
break;
}

But the real answer is.... If you want to do something different for
each button, you should be using separate Click events. Factor the common
part into a separate method, and call it from each of the Button[n]_Click
events.
--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"Frank Esser" <Mi*****@nurfuerspam.de> wrote in message
news:OJ**************@TK2MSFTNGP10.phx.gbl...
Hi !

I have several buttons and a common click event for all of them. How can I
get the reference to the clicked control in a switch?

This does not work:

private void Button_Click(object sender, System.EventArgs e)
{
switch ((Button)sender)
{
case Button1:
...
break;
case Button2:
....
break;
default:
break;
}
}

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Zlatko Matiæ | last post by:
There is a form (single form) and a combobox. I want that current record of the form is adjusted according to selected value in the combobox. Cuurrent record should be the same as the value in the...
5
by: Dick | last post by:
I have a GridView bound to an ObjectDataSource. I have a Button that calls GridView.DataBind. I want the row that is selected before the DataBind to still be selected afterwards. This happens...
12
by: | last post by:
Is it fine to call another method from Switch? Eg. Switch (stringVar) { case ("a"): somVar = "whatever"; Another_Method(); //call another method return;
6
by: SAL | last post by:
hello, I'm using a radiobuttonlist in an updatepanel in an item template in a Gridview control. I'm populating the radiobuttonlist in the RowDataBound event. I have the control toolkit registered...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.