Re: Messagebox in Validating cancels subsequent events
- From: "Kevin Spencer" <uce@xxxxxxx>
- Date: Thu, 10 Aug 2006 15:49:54 -0400
Hi Al,
I tend to avoid the term "bug," unless behavior is not expected by the
developer. I would think that Microsoft is well aware of this behavior, and
does not consider it to be errant.
--
HTH,
Kevin Spencer
Microsoft MVP
Chicken Salad Surgery
Accept the Unexpected.
"Al Santino" <asantino@xxxxxxxxxx> wrote in message
news:OvS%23ZLKvGHA.560@xxxxxxxxxxxxxxxxxxxxxxx
Hi Kevin,
Thanks for looking at it. I guess I'll give up on using the validating
event and try something else. Would you say this is a bug?
Al
"Kevin Spencer" <uce@xxxxxxx> wrote in message
news:OQjkp8JvGHA.4872@xxxxxxxxxxxxxxxxxxxxxxx
Hi Al,
I'm not completely sure what's happening. I tried out the code, and mixed
it around a bit, and then read the documentation on the
Control.Validating event. Control focus events happen in the following
order:
1. Enter
2. GotFocus
3. Leave
4. Validating
5. Validated
6. LostFocus
Note that the Validating event occurs prior to the LostFocus event. So,
when the Validating event occurs, the Control still has the focus. My
guess is that because of the event handler, the focus is not lost until
the MessageBox appears, and this causes the second button to not receive
the Click event unless clicked a second time.
--
HTH,
Kevin Spencer
Microsoft MVP
Chicken Salad Surgery
Accept the Unexpected.
"Al Santino" <asantino@xxxxxxxxxx> wrote in message
news:OB$yU5IvGHA.4972@xxxxxxxxxxxxxxxxxxxxxxx
Hi,
It appears displaying a messagebox in a validating event will cancel
subsequent events. I'm including a short program demonstrating the
behaviour in VB and C#.
The program creates a form, adds two buttons, a validating handler for
button one and a click handler for button two. Clicking on button two
when button one has the focus should cause button one's validating to
fire followed by button two's click handler. However, button two's
click handler doesn't fire.
Can someone explain what I'm doing wrong or suggest a workaround?
Thanks
Al
-----------------------------------------------
*** VB version:
Imports system
Imports system.windows.forms
Module ValidateTest
Sub Main()
Dim frm As New Form()
Dim btn As New Button
btn.Text = "One"
btn.Parent = frm
AddHandler btn.Validating, AddressOf BtnValidating
btn = New Button
btn.Left = btn.Width + 3
btn.Text = "Two"
btn.Parent = frm
AddHandler btn.Click, AddressOf BtnClick
Application.Run(frm)
End Sub
Private Sub BtnValidating(ByVal sender As Object,
ByVal e As System.ComponentModel.CancelEventArgs)
MessageBox.Show("BtnValidating event")
End Sub
Private Sub BtnClick(ByVal sender As Object, ByVal e As
System.EventArgs)
MessageBox.Show("BtnClick event")
End Sub
End Module
-----------------------------------------------
*** C# version:
using System;
using System.Windows.Forms;
class NoClickEvent: Form
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new NoClickEvent());
}
public NoClickEvent()
{
Button btn = new Button();
btn.Text = "One";
btn.Parent = this;
btn.Validating += BtnValidating;
btn = new Button();
btn.Text = "Two";
btn.Left = btn.Width + 3;
btn.Parent = this;
btn.Click += BtnClick;
}
void BtnValidating(Object sender,
System.ComponentModel.CancelEventArgs e)
{
MessageBox.Show("BtnValidating event");
}
void BtnClick(Object sender, System.EventArgs e)
{
MessageBox.Show("BtnClick event");
}
}
.
- Follow-Ups:
- Re: Messagebox in Validating cancels subsequent events
- From: Al Santino
- Re: Messagebox in Validating cancels subsequent events
- References:
- Messagebox in Validating cancels subsequent events
- From: Al Santino
- Re: Messagebox in Validating cancels subsequent events
- From: Kevin Spencer
- Re: Messagebox in Validating cancels subsequent events
- From: Al Santino
- Messagebox in Validating cancels subsequent events
- Prev by Date: ToolStripControlHost & DateTimePicker
- Next by Date: RE: FK Constraint Raised for no reason I can find?
- Previous by thread: Re: Messagebox in Validating cancels subsequent events
- Next by thread: Re: Messagebox in Validating cancels subsequent events
- Index(es):