473,770 Members | 2,153 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MessageBox in Validating event cancels subsequent events

Hi,

It appears displaying a messagebox in a validating event will cancel the
subsequent event. In the program below, button 2's click event doesn't fire
if you open a dialog box in button 1's validating event. Am I doing
something wrong here?
Thanks
Al

Imports system
Imports system.windows. forms
' Create a form, add two buttons and event handlers.
' Click on Button 2 to receive Button 1 validating
' event but not button 2's click event. MessageBox
' appears to kill the click event.
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(B yVal sender As Object,
ByVal e As System.Componen tModel.CancelEv entArgs)
MessageBox.Show ("BtnValidat ing event")
End Sub

Private Sub BtnClick(ByVal sender As Object, ByVal e As
System.EventArg s)
MessageBox.Show ("BtnClick event")
End Sub
End Module

Aug 9 '06
16 5511
Hello, Rinze,

Since the user HAS clicked on button2 with the mouse, I think this
SHOULD be enough to cause its click event to fire. Sadly, it doesn't.

Cheers,
Randy
C-Services Holland b.v. wrote:
>
From the code posted I don't see why button2's click event would be
triggered at all. The click event will not fire simply because a button
get's the focus.
Aug 11 '06 #11
R. MacDonald wrote:
>
Since the user HAS clicked on button2 with the mouse, I think this
SHOULD be enough to cause its click event to fire. Sadly, it doesn't.
Now that I think about it, the button click event is not fired until
the button is *released*. Since the code is throwing up a message box,
focus is away from Button2 therefore the click is not registered.

Think about this: Suppose you had a textbox and a button on the form
and you had the validating event on the textbox. Suppose you entered
invalid data into the textbox and clicked the button, should the button
click event still be fired if the textbox was not validated?

I'm beginning to think this is by design, perhaps a bit unexpected, but
by design.

Aug 11 '06 #12
R. MacDonald wrote:
Since the user HAS clicked on button2 with the mouse, I think this
SHOULD be enough to cause its click event to fire. Sadly, it doesn't.
I'm not so sure. Look at this code below. If the textbox has the word
hello in it when Button2 is clicked, should Button2's event fire
anyway? I don't think so. I think the OP's problem is because of the
Message Box. It steals focus from Button2 so that when the mouse is
release, the message is not being sent to button 2. If you remove the
Message Box from the validating event, the click event should fire.

Public Class Form1

Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button2.Click
MsgBox("Button 2 Clicked")
End Sub

Private Sub TextBox1_Valida ting(ByVal sender As System.Object,
ByVal e As System.Componen tModel.CancelEv entArgs) Handles
TextBox1.Valida ting
If TextBox1.Text.T oUpper = "HELLO" Then
MsgBox("Invalid Value in textbox")
e.Cancel = True
End If
End Sub
End Class

Aug 11 '06 #13
Yes, if you remove the Message Box things work fine. But I'd like to use
this in the case where someone enters fills in some controls on a form and
then chooses to close the form. I want to give them a choice of canceling
the quit, proceeding without saving, or proceeding and saving. This seems
like a rather common behaviour.

I would expect that setting the cancel arguement to "true" would cancel the
subsequent click event and setting it to false would not.
"Chris Dunaway" <du******@gmail .comwrote in message
news:11******** **************@ 74g2000cwt.goog legroups.com...
R. MacDonald wrote:
>Since the user HAS clicked on button2 with the mouse, I think this
SHOULD be enough to cause its click event to fire. Sadly, it doesn't.

I'm not so sure. Look at this code below. If the textbox has the word
hello in it when Button2 is clicked, should Button2's event fire
anyway? I don't think so. I think the OP's problem is because of the
Message Box. It steals focus from Button2 so that when the mouse is
release, the message is not being sent to button 2. If you remove the
Message Box from the validating event, the click event should fire.

Public Class Form1

Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button2.Click
MsgBox("Button 2 Clicked")
End Sub

Private Sub TextBox1_Valida ting(ByVal sender As System.Object,
ByVal e As System.Componen tModel.CancelEv entArgs) Handles
TextBox1.Valida ting
If TextBox1.Text.T oUpper = "HELLO" Then
MsgBox("Invalid Value in textbox")
e.Cancel = True
End If
End Sub
End Class

Aug 12 '06 #14
Hello, Al,

Chris's idea that the event is being "absorbed" by the MessageBox may be
on the right track, but I suspect this is not "by design" but is more
likely due to lack of design. Either way, I can't see how to use this
idea to come up with a work-around.

But it sounds as if, rather than using the MessageBox as part of
validating the contents of a control, you are actually using it to
validate the closing of the form.

I know it doesn't address the problem of the missing click event, but
perhaps you can solve your problem by modifying your approach.

In a similar case when I wanted the user to validate or cancel
closing/saving, I have used the Form's Closing event for this purpose.
(Well actually, since it was a base for several derived forms, I
overrode the OnClosing method.) The main problem with this approach is
that it is still necessary to invoke the Validation code for the
currently ActiveControl. I haven't yet found a good generic way to do
this (in VB), and so have had to add code to each derived form to find
the active control and directly call its validation handler. (I think
it should be possible to locate and call the handler in a generic
fashion, and I'm still hoping to figure out how to do this.)

Cheers,
Randy
Al Santino wrote:
Yes, if you remove the Message Box things work fine. But I'd like to use
this in the case where someone enters fills in some controls on a form and
then chooses to close the form. I want to give them a choice of canceling
the quit, proceeding without saving, or proceeding and saving. This seems
like a rather common behaviour.

I would expect that setting the cancel arguement to "true" would cancel the
subsequent click event and setting it to false would not.

Aug 14 '06 #15
Hi Randy,

Would that it were. I have a SplitContainer with a list of items on the left
and a form on the right. When you click on an item in the list on the left
it displays the fields for the item on the right. You can edit the fields on
the right. When you click on an item in the left the form's validate event
fires to see whether you changed an item. If so, it displays a messagebox
asking whether you wish to save your changes.

I like your idea of using the form closing event but, unfortunately, the
form doesn't close each time. I do appreciate your giving this some thought.

Al
"R. MacDonald" <sc****@NO-SP-AMcips.cawrote in message
news:44******** **************@ news.wanadoo.nl ...
Hello, Al,

Chris's idea that the event is being "absorbed" by the MessageBox may be
on the right track, but I suspect this is not "by design" but is more
likely due to lack of design. Either way, I can't see how to use this
idea to come up with a work-around.

But it sounds as if, rather than using the MessageBox as part of
validating the contents of a control, you are actually using it to
validate the closing of the form.

I know it doesn't address the problem of the missing click event, but
perhaps you can solve your problem by modifying your approach.

In a similar case when I wanted the user to validate or cancel
closing/saving, I have used the Form's Closing event for this purpose.
(Well actually, since it was a base for several derived forms, I overrode
the OnClosing method.) The main problem with this approach is that it is
still necessary to invoke the Validation code for the currently
ActiveControl. I haven't yet found a good generic way to do this (in VB),
and so have had to add code to each derived form to find the active
control and directly call its validation handler. (I think it should be
possible to locate and call the handler in a generic fashion, and I'm
still hoping to figure out how to do this.)

Cheers,
Randy
Al Santino wrote:
>Yes, if you remove the Message Box things work fine. But I'd like to use
this in the case where someone enters fills in some controls on a form
and then chooses to close the form. I want to give them a choice of
canceling the quit, proceeding without saving, or proceeding and saving.
This seems like a rather common behaviour.

I would expect that setting the cancel arguement to "true" would cancel
the subsequent click event and setting it to false would not.
Aug 14 '06 #16
Hello, Al,

Yes, I can see that's a sticky problem. Too often in this sort of
situation we end up succumbing to non-elegant work-arounds that involve
"rememberin g" which control we were on, which control we were going to,
whether or not we were navigating by mouse-click, tab or code, etc. and
then programming around all the possibilities -- Very messy, because
there are usually so many possibilities.

I can't think of a good solution just now, but I'll let you know if I
have any late-night inspirations.

Cheers,
Randy
Al Santino wrote:
Hi Randy,

Would that it were. I have a SplitContainer with a list of items on the left
and a form on the right. When you click on an item in the list on the left
it displays the fields for the item on the right. You can edit the fields on
the right. When you click on an item in the left the form's validate event
fires to see whether you changed an item. If so, it displays a messagebox
asking whether you wish to save your changes.

I like your idea of using the form closing event but, unfortunately, the
form doesn't close each time. I do appreciate your giving this some thought.

Al
"R. MacDonald" <sc****@NO-SP-AMcips.cawrote in message
news:44******** **************@ news.wanadoo.nl ...
>>Hello, Al,

Chris's idea that the event is being "absorbed" by the MessageBox may be
on the right track, but I suspect this is not "by design" but is more
likely due to lack of design. Either way, I can't see how to use this
idea to come up with a work-around.

But it sounds as if, rather than using the MessageBox as part of
validating the contents of a control, you are actually using it to
validate the closing of the form.

I know it doesn't address the problem of the missing click event, but
perhaps you can solve your problem by modifying your approach.

In a similar case when I wanted the user to validate or cancel
closing/saving, I have used the Form's Closing event for this purpose.
(Well actually, since it was a base for several derived forms, I overrode
the OnClosing method.) The main problem with this approach is that it is
still necessary to invoke the Validation code for the currently
ActiveControl . I haven't yet found a good generic way to do this (in VB),
and so have had to add code to each derived form to find the active
control and directly call its validation handler. (I think it should be
possible to locate and call the handler in a generic fashion, and I'm
still hoping to figure out how to do this.)

Cheers,
Randy
Al Santino wrote:
>>>Yes, if you remove the Message Box things work fine. But I'd like to use
this in the case where someone enters fills in some controls on a form
and then chooses to close the form. I want to give them a choice of
canceling the quit, proceeding without saving, or proceeding and saving.
This seems like a rather common behaviour.

I would expect that setting the cancel arguement to "true" would cancel
the subsequent click event and setting it to false would not.

Aug 15 '06 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
9756
by: Paolo Mancini | last post by:
Hi all, I have a page with many different elements: <a href="...."> ... </a>, listboxes, radiobuttons, etc. Can I use javascript to prevent the user from interacting with the page? That is: can I intercept, alert the user, and cancel any click event on the page? Unfortunately, if the user clicks on a page element, the event bubbles
7
2517
by: MLH | last post by:
I tried the following code to prevent a checkbox from being updated (going from a value of Null to True, from True to False or from False to True). I was surprised it did not work. Can anyone offer a sensible, logical, insightful reason as to why Microsoft does not want to let programmers stop this event? Private Sub NoOwnerInfoChkBox_Click() If CurrentVehicleJobID = 0 Then
0
1609
by: Bradley Bossard via DotNetMonster.com | last post by:
I am having an issue with the .NET framework (or C#) and validating events. I have implemented several validating event handlers for textboxes on a form. When I run the app, the form works correctly the first time, but if I input some data in the form and click another control to change focus, the validator fires, but if I continue to hit 'ESC' enough times, it eventually lets me out of the validating loop and moves focus to the other...
0
1552
by: Joe | last post by:
Hi For a while now I have been finding postings of problems with the validating event not firing on controls properly. I too had this problem. The event would fire when clicking on another control which had it's causes validation property set to true however if I tabbed on to this control the event wouldn't fire. So after playing around with my code I figured out how to get it to work. I am not sure what the reason behind it is but it...
7
1759
by: Bruce HS | last post by:
I'd like to call my ancestor Validation Function every time any control on a Win Form generates a Validating or Validated event. I'm using VB. I've extended Textbox, for instance, to have its events do this for me, but my extended textbox doesn't get created by those wonderful form setup wizards. So, 1) Is there a way I can pick up these events without having to code for each control and without using custom extended controls, OR
21
9220
by: Darin | last post by:
I have a form w/ a textbox and Cancel button on it. I have a routine to handle textbox.validating, and I have the form setup so the Cancel button is the Cancel button. WHen the user clicks on the cancel button, the textbox.validating is being called. I don't want it to be since they are exiting the screen the validation doesn't have to be done. How can I do that.
4
3008
by: Academic | last post by:
Does it make sense to put this If e.Cancel Then Exit Sub at the beginning of form closing events so if the user cancels the app's exiting in one Closing routine he will not be asked again by another when its form Closing routine is run? I guess what I'm asking is will that work. If one form sets e.cancelled to true will e.cancel be true when the next form receives a closing event?
3
1975
by: Hamed | last post by:
Hello I have a Data Entry Form having some controls including a TextBox. When the user types an entry, I query a table and if it was entered before, I ask the user (using ShowModal method of a custom message form) if he wants to see his old entry in a new form. if he/she choose Yes, I create a new form and show the previously entered data in it. The problem is when I create and show the new form, the validating event is fired two...
2
2682
by: Peted | last post by:
Hi if i derive a reference to a control on a winform (ie Control activeControl = somecontrol on the form) how can i test if that control has a validating or validated event and more importantly how can i tell that those events have finished so that i can test for a new situation. My problem is i am modding some existing code, were the TAB key keypress is captured by a external c# module.
0
9591
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10228
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8883
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7415
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6676
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3970
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3575
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.