Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / June 2006

Tip: Looking for answers? Try searching our database.

TextBox validation.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Kevin Burton - 29 Jun 2006 15:47 GMT
I have a text box (several actually) that I need to validate. Some of the
conditions is that it can't be empty, it can only contain numeric characters
and optionally dashes, etc. One of the problems is that once a user has
started entering text in this text box, unless it is valid input focus stays
on that control. I would like for there to be a way for the user to "get out"
of the control. Typically I would expect that hitting <ESC> would do that but
I don't see an event like for DataGridView where there is a CellEndEdit event
for a text box. Another problem is that if the user hits <ENTER>, notihing
happens. Again I would expect for the validating event to fire but it
doesn't. Is there a more robust way of validating text box input?

Thank you.

Kevin
Joris Zwaenepoel - 30 Jun 2006 07:20 GMT
The focus is not "forced" to stay in the textbox, if you do not set e.Cancel
= True in its Validating event handler.  So, leave that away from the event
handler and it will
be possible to move the focus from one control to another, even with invalid
data.  

You can still use an errorprovider to inform the user of the bad data.

As for the Esc and Enter key behaviours, try the foloowing:

   Private _prevTextBox1Value As String

   Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.Enter
       _prevTextBox1Value = TextBox1.Text
   End Sub

   Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
       If e.KeyCode = Keys.Escape Then
           TextBox1.Text = _prevTextBox1Value
       End If
   End Sub

   Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
       If e.KeyChar = vbCr Then
           SendKeys.Send("{Tab}")
           e.Handled = True
       End If
   End Sub

Hope this helps,

Joris

> I have a text box (several actually) that I need to validate. Some of the
> conditions is that it can't be empty, it can only contain numeric characters
[quoted text clipped - 10 lines]
>
> Kevin
Stoitcho Goutsev (100) - 30 Jun 2006 14:22 GMT
Kevin,

I understand your are using .NET2.0 because you mentioned DataGridView
controls. In this case don't you use the MaskedTextBox control? This way you
can prevent users of making mistakes instead of showing them error messages
for wrong input.

Signature

HTH
Stoitcho Goutsev (100)

>I have a text box (several actually) that I need to validate. Some of the
> conditions is that it can't be empty, it can only contain numeric
[quoted text clipped - 15 lines]
>
> Kevin

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.