Solved

Validating a textbox...

Posted on 2009-05-08
4
348 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 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…

785 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