Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Validating a textbox...

Posted on 2009-05-08
4
Medium Priority
?
354 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
  • 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

971 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