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