Wrox Programmer Forums
|
BOOK: Visual Basic 2010 Programmer's Reference
This is the forum to discuss the Wrox book Visual Basic 2010 Programmer's Reference by Rod Stephens; ISBN: 9780470499832
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Visual Basic 2010 Programmer's Reference section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 22nd, 2011, 09:28 AM
Registered User
 
Join Date: Mar 2011
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Textbox Leave Event

Hi - I am trying to write code that requires an end user to fill in a text box on a form. I have the event procedure coded as follows:

----------------
Private Sub hourlyRateTextBox_Leave(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles hourlyRateTextBox.Leave

If hourlyRateTextBox.Text = String.Empty Then
MessageBox.Show("Error - Field Required", "Field Required", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
hourlyRateTextBox.Focus()
End If

End Sub
------------

I have several of these coded and they work fine. However, I have a button on my UI that allows the end user to quit without saving so if they go into the form in add mode but don't enter anything in the text boxes (I have 4 set up like this), they get error messages.

Is there a way to circumvent the required fields if they simply want to quit the application without entering anything.

Any help would be greatly appreciated!

Jewel
 
Old April 22nd, 2011, 10:09 AM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

I would probably move the validation code into the button that closes the form normally. For example, suppose you have OK and Cancel buttons. Then do the validation in the OK button's event handler. When it finds a text box that isn't filled it, it displays a message and returns. That way the user only sees the first message not a whole bunch all at once, and the form doesn't close.

The Cancel button doesn't perform the checks so it closes the form without displaying error messages.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
 
Old April 22nd, 2011, 12:55 PM
Registered User
 
Join Date: Mar 2011
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you. I will move the code there. I was trying to make it so the user couldn't move past the text box without entering something but I guess in the grand scheme of things it won't work that way. Thanks again!!
 
Old April 22nd, 2011, 04:42 PM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

Ah. In that case you may want to look at the Validating event. A control's Validating event fires when the user tries to move off of it. Here's an example:

http://www.vb-helper.com/howto_net_use_validation.html

In this case, each TextBox would have its own Validating event.

I like to change the background color of an invalid value to light yellow so the user knows there's a problem, but not use e.Cancel to prevent the user from leaving the field. That way they can fix it when it's convenient, and it avoids the issue of what to do it they want to cancel. The OK button can validate again to make sure all of the fields are filled in correctly before accepting the form.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)





Similar Threads
Thread Thread Starter Forum Replies Last Post
ModelPopUpExtender on TextBox Keypress event swathi reddy ASP.NET 2.0 Basics 0 April 26th, 2010 04:52 AM
ToolStripMenuItem Enter & Leave Event angelboy C# 2005 6 November 21st, 2008 04:57 PM
TextBox Event davewoods Visual Basic 2005 Basics 3 October 10th, 2008 12:56 PM
TextBox - formattering input after event modelplane Visual Basic 2005 Basics 0 December 10th, 2006 03:06 PM
how to handle click event for TextBox gagansharma7 General .NET 4 June 26th, 2006 01:18 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.