471,337 Members | 850 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

get control of senders functions

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
Dec 7 '05 #1
4 1638
Pascal Schmidt-Volkmar wrote:
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

Dec 7 '05 #2
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
Dec 7 '05 #3
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

"Pascal Schmidt-Volkmar" <pa***********@tauth.com> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
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

Dec 7 '05 #4
Marc,

that was exactly what I was looking for! Thanks a lot!

Pascal
Dec 7 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Simon | last post: by
reply views Thread by Matt Clepper | last post: by
3 posts views Thread by Filips Benoit | last post: by
8 posts views Thread by Prince Mathew | last post: by
5 posts views Thread by Galen Somerville | last post: by
4 posts views Thread by SAL | last post: by
11 posts views Thread by Brad Baker | last post: by
reply views Thread by rosydwin | last post: by

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.