Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / September 2004

Tip: Looking for answers? Try searching our database.

Solution: Stop validating if Cancel button Clicked

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JB - 11 Sep 2004 01:27 GMT
OK, this was my setup/problem.  I had a form with initially 1 textbox
and a cancel button enabled.  If I entered any text into the text box
and then clicked on the cancel button or hit tab the validation event
would fire.  If a user entered tab I wanted to go ahead and
validate...if they clicked on Cancel, no matter what they had already
entered into the textbox, I wanted to Cancel and close.

My solution:

I added another button to my form and made it size 0,0 and made sure
the tab order was set with this basically hidden control to come
before the cancel button.

I then in my validating code (which was handling multiple textbox
validations) added the following two checks:

1) I then checked Me.ActiveControl.Name to see if it was equal to
btnCancel.  If it was then I just put focus on the cancel button and
exited the sub. (This is because this was the only control outside of
the sort of hidden control that the user could have clicked on).

2) If current textbox was empty I assumed user wanted to cancel and
just placed focus on the cancel button and exited the sub

Without having the extra hidden control to catch the tab the
Me.ActiveControl.Name would always be equal to btnCancel and
validation would never occur!

If someone has a better approach (without excluding the validating
event) please let me know...this was driving me crazy.

The only thing I settled on and did NOT resolve was when a user was
first on the first textbox if they did not enter anything and then
they hit the escape key it would close the form...but if they had
entered any text into the textbox then tried to hit escape validation
would occur.

JB
Jared - 11 Sep 2004 03:09 GMT
In the Textbox's validating event perform your validation, if it passes
great, if it fails close the form. Set the buttons CausesValidation property
to false and use its click event to close the form.

A text box and a button.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
   Me.Close()
End Sub
Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
   'Perform your validation checks here
   If Me.TextBox1.Text.TrimEnd.Length = 0 Then
       Me.Close()
   End If
End Sub
Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.LostFocus
   TextBox1_Validating(TextBox1, New System.ComponentModel.CancelEventArgs)
End Sub

> OK, this was my setup/problem.  I had a form with initially 1 textbox
> and a cancel button enabled.  If I entered any text into the text box
[quoted text clipped - 34 lines]
>
> JB
JB - 14 Sep 2004 18:12 GMT
Jared,

I think you missed the point.  If the user has entered
something/anything and then hits cancel running the validation event
by design will popup a message telling them what the error/problem
is...but I didn't want that if they did indeed hit the cancel button
then I just needed to cancel and if validation failed when a user was
really trying to get it right it would be rude to close the form.

JB

> In the Textbox's validating event perform your validation, if it passes
> great, if it fails close the form. Set the buttons CausesValidation property
[quoted text clipped - 56 lines]
> >
> > JB
Jared - 15 Sep 2004 01:00 GMT
JB,
In the English language I know this is considered rude, very rude --> "I
think you missed the point."
You need to remain courteous when asking for assistance, or get clever when
you write down your comments! ;-)

My reply to the comment:
Dim options As System.Text.RegularExpressions.RegexOptions = _
   System.Text.RegularExpressions.RegexOptions.None
Dim regex As System.Text.RegularExpressions.Regex = _
   New System.Text.RegularExpressions.Regex("\s(a(\w)\2).+((\scl).+(own))",
options)
Dim Matches As System.Text.RegularExpressions.MatchCollection _
   = regex.Matches(MyReply.Text)
Dim Result As String = Matches.Item(0).Groups(1).Value _
   & Matches.Item(0).Groups(4).Value _
   & Matches.Item(0).Groups(5).Value
System.Windows.Forms.MessageBox.Show(Result.ToUpper, "What I think of the
comment - I think you missed the point.")

My Reply to the post
First of all, I'm not writing your program for you, the example I presented,
albeit does not perform the desired results, does demonstrate the methods in
which you can achieve your results. Secondly, I don't know if you have
noticed, but, the event handler has an argument called sender, this
represents the object that raised the event, an object like a textbox, that
includes a name attribute. With this knowledge you no longer have to provide
a "workaround" with extra controls sucking up memory space. So, in this case
you have no reason to use Me.ActiveControl.Name! I have a simple form with
three textboxes and one button. Each of the Validation handlers is attached
to the same routine, when it fires it converts the object to a generic
control and gets the name. Use it in a select statement, use the gettype or
typeof to perform specific validation on your controls.

Private Sub MyTextboxes_Are_Validating(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) _
   Handles TextBox1.Validating, TextBox2.Validating, TextBox3.Validating
   Console.WriteLine("The control that is currently validating is: {0}",
CType(sender, Control).Name)
End Sub

You should be doing something SIMILAR to

> Jared,
>
[quoted text clipped - 69 lines]
>> >
>> > JB
mdb - 11 Sep 2004 05:00 GMT
> If someone has a better approach (without excluding the validating
> event) please let me know...this was driving me crazy.

Have you looked at the 'CausesValidation' property?  I've never tried it
myself, but the help description seems to indicate that you just need to
set it to false on the Cancel button, leaving everything else alone.

-mdb
JB - 14 Sep 2004 18:16 GMT
I did mess around a bit with the causesvalidation property and this
and/or e.cancel = true can be valid methods to control the whole
validation thing but in this case because the only other button was
cancel (by default the next textbox was disabled until textbox1 had
been validated) then having the cancel button cancel validation would
cause the validation to never occur!  That's what was driving me crazy
and why I came up with this work around.

Thanks.

JB

> > If someone has a better approach (without excluding the validating
> > event) please let me know...this was driving me crazy.
[quoted text clipped - 4 lines]
>
> -mdb

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.