Solved

Validating a textbox...

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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
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
Course of the Month6 days, 15 hours left to enroll

622 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