Solved

Validating a textbox...

Posted on 2009-05-08
4
351 Views
Last Modified: 2013-12-26
I am a little confused about how to use CausesValidation in a WinForms application....here is what I  want to accomplish:

The user is presented with a default value in a text box, but he/she can change it to anything except a blank or a value that has already been used.  (I have a function that will check that.)  
If the user attempts an invalid entry,  a message box explaining the problem pops up, then after the user dismisses the message box, the value in the text box reverts to whatever it was before the user edited it.

How would I use CausesValidation and the Validation and/or Focus events to accomplish the above?  I've been trying for hours now, but I think I may have coded myself into a corner....

Thanks in advance for your kind assistance.



0
Comment
Question by:codefinger
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24341560
Can you show us your code?
0
 
LVL 15

Accepted Solution

by:
JackOfPH earned 500 total points
ID: 24341835
You can use the undo property of the textbox to revert it to the original value:

example:

textbox1.undo

Here is a sample:

I hope you get the idea...


Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
        If Me.TextBox1.Text.Trim = String.Empty Then
            MsgBox("Value cannot be null.")
            e.Cancel = True
            TextBox1.Undo()
        End If
 End Sub

Open in new window

0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 24352401
Do you need further assistance?
0
 

Author Closing Comment

by:codefinger
ID: 31579687
Thanks.  I did not even suspect that the text box has an UNDO function.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question