3 buttons, only one fires event

M

mp

I have 3 buttons on a form (btnRead, btnSave, btnUpdate)
3 identical _click events (see below)
the read button fires it's click event the other two don't
any thoughts of what could cause that?
i'm trying to do a mvps pattern
so the buttons are in the form in the view layer
setting aside for a moment why i'm not getting the events caught by the
presentation layer yet,
I don't see why only one button appears to be event firing it's own click
event
the other buttons appear to work on the ui (they take focus and change color
when clicked)

private void btnRead_Click(object sender, EventArgs e)
{
Debug.Print("btnclick " + sender.ToString());
MessageBox.Show("btnclick " + sender.ToString());

}
private void btnSave_Click(object sender, EventArgs e)
{
Debug.Print("btnclick " + sender.ToString());
MessageBox.Show("btnclick " + sender.ToString());

}
private void btnUpdate_Click(object sender, EventArgs e)
{
Debug.Print("btnclick " + sender.ToString());
MessageBox.Show("btnclick " + sender.ToString());

}

I'm mystified (as usual)
thanks
mark
 
M

mp

mp said:
I have 3 buttons on a form (btnRead, btnSave, btnUpdate)
3 identical _click events (see below)

so this is interesting(to me)
I made a new form with 3 buttons
i named the buttons btnRead, btnSave, btnUpdate
I copied and pasted 3 click event code sections from another cs into this
one
private void btnRead_Click(object sender, EventArgs e)
{
try
{
Debug.Print("button Read");
}
catch (Exception ex)
{
Debug.Print(ex.Message);
}
}

etc...
none of them fire the event

so i go back to the designer and dbl click the button object to get to the
click event in code
it creates a new click event btn<Name>_Click_1
private void btnRead_Click_1(object sender, EventArgs e)
{
}
if i put the same code in that event as in the original _Click event it
works!?!?!

then I erase all events and go back to the designer and dbl click the button
to get to the event code and it produces
private void btnRead_Click(object sender, EventArgs e)
{
}

which is the exact same signature as the one i had originally pasted in
which didn't work
now this one works (when I put the same code in the event as shown above)

is this normal, I can't just paste in click event code which matches the
name of the control object, i have to dbl click the object itself to create
the event code???
thanks
mark
 
M

mp

Peter Duniho said:
[...]
is this normal, I can't just paste in click event code which matches the
name of the control object, i have to dbl click the object itself to
create
the event code???

I forgot: are you coming from VB or VB.NET? Your apparent
misunderstanding would not be surprising if that's where your experience
is from.

yep, from vb6
Simply declaring an event handler is not sufficient. The handler needs to
be subscribed to the event. In VB and VB.NET, the language provides
mechanisms for the method to be declared in a way that automatically
causes it to be subscribed (e.g. "Handles"), but in C# the subscription
has to be explicit.
ah ha!
When you double-click a control, the VS Designer will do a couple of
things:

. Auto-generate a method with the appropriate signature and a name that
relates the method to the control and event being handled, where the event
is the default event for that control (e.g. "Click" is the default event
for buttons).

. Add a line in the *.Designer.cs file that actually subscribes the
handler to the event.

that must be this line?
this.btnRead.Click += new System.EventHandler(this.btnRead_Click);
The easiest way to have three separate event handlers for three different
buttons is to double-click each button in turn, causing the above to occur
for all three.

Often, it is possible for a single method to handle events from several
control instances, where the control-specific behavior is controlled in
some way by the "sender" argument passed to the method. In this case, you
can create the method once, and then rather than double-clicking the
control, just select the method for the other controls using the
Properties window in the IDE:

. Select the control
. In the Properties window, switch to the list of events (click the
button with the lightning bolt on it)
. Find the event you want to subscribe, and click _once_ in the edit
box. A drop-down will appear with a list of methods that have a matching
signature.
. Select the method you want.

Finally note that for other events, you can still get the auto-generate
behavior, by double-clicking the event's edit box in the Properties window
rather than the control itself.

In all three scenarios - double-clicking the control, double-clicking the
event edit box, or single-clicking the edit box and selecting the method
to subscribe as the handler - the Designer will add the necessary line of
code to the *.Designer.cs file to actually subscribe the event handler to
the event.

Pete

thanks for the tips
Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top