Solved

Validating a textbox...

Posted on 2009-05-08
4
344 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
Comment Utility
Can you show us your code?
0
 
LVL 15

Accepted Solution

by:
JackOfPH earned 500 total points
Comment Utility
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
Comment Utility
Do you need further assistance?
0
 

Author Closing Comment

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

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now