get control of senders functions

  • Thread starter Thread starter Pascal Schmidt-Volkmar
  • Start date Start date
P

Pascal Schmidt-Volkmar

Hi there, in my calculator app the number buttons have (of course) almost
the same functionality. Therefore, I would like to store their value in the
tag number and read that tag number when any of those buttons is pressed.
But how does this work??

private void button3_Click(object sender, EventArgs e)

{

if sender.GetType() is button

{

read out its tag value and add it to calcWindow;

}

}



Thanks,

Pascal
 
Pascal said:
Hi there, in my calculator app the number buttons have (of course) almost
the same functionality. Therefore, I would like to store their value in the
tag number and read that tag number when any of those buttons is pressed.
But how does this work??

private void button3_Click(object sender, EventArgs e)

{

if sender.GetType() is button

{

read out its tag value and add it to calcWindow;

}

}

Well, I would make sure that you only subscribe to the event with that
delegate when the sender *will* be a button - that way you can just
cast straight to button. After that, just use any of the button's
properties as normal (eg Text, Tag etc).

Jon
 
Hi Jon,

but how does this check and the cast work?

private void button1_Click(object sender, EventArgs e)

{

if ( sender.GetType() = System.Windows.Forms.Button )

{

System.Windows.Forms.Button(sender).text = edErgebnis.Text + "1";

}

}

Pascal
 
Well I'm pretty sure that it doesn't! You have an assignment operator in a
test, and are comparing directly to a class definition... and use of a
non-existant property...

I think this should be:

private void SharedClick(object sender, EventArgs e) {
Button btn = sender as Button;
if (btn != null && btn.Tag != null) {
// do something with btn.Tag, which will be cast as an object,
so we may need to recast it or call .ToString(), e.g.
myTextBox.Text += btn.Tag.ToString();
}
}

When any button is clicked, it fires the event sending itself (this) as the
sender; when we catch the event, we test it so see if it is a Button using
the "as" keyword (which returns null if you have incorrectly hooked the
event into another type of control). After this, btn is an instance of a
Button, so we can access btn.Tag (incidentally, you could actually replace
Button with Control in the above and it would work just fine, since Tag is
defined against a Control, not the actual Button class).

You'd also need to hook all of the Click events to use this *same* event
handler, which can be done either in the IDE (by selecting them in the
drop-down), or manually using something like (in .Net 2):

myButton1.Click += SharedClick;
myButton2.Click += SharedClick;
myButton3.Click += SharedClick;
....

In .Net 1 you would have to use the longer syntax, so I would probably go
for:

EventHandler sharedEvent = new EventHandler(SharedClick);
myButton1.Click += sharedEvent;
myButton2.Click += sharedEvent;
myButton3.Click += sharedEvent;
....

Does that help any?

Marc
 
Back
Top