Prohibit Users From Accepting Data in VB and Vb.net

Author: Bruce A. Tucker
Category: Software RSS
Republish this article manually
Republish articles from Software category automatically

I have been developing software for quite sometime. I got my start about fifteen years ago developing small applications for in house use. If you are new to development, in house use means programs that are used within the company itself.

Once I got enough experience I was assigned to projects that were sold to clients around the world. Through my many years of developing software one thing holds true and that is the end user "will" break your software. Not because they do not know what they are doing and not because they are trying to break your software, and certainly not because you are a bad programmer, but because users will do things with software that make you scratch your head and go "huh?"

So a technique that I use that goes above just using simple error handling, like the "On Error" statement in Visual Basic or the "Try...Catch" statement in VB.net. What I like to do is limit the user's ability to actually enter any type of data or perform any type of actions before something else is completed first.

In my example here let's say the user clicks on a button. This click then loads a window where the user will enter some data to add to the previous screen.

In a module file we declare the form variable as public:

Public frmEnterNewCarrier As New frmNewCarrier

In the button's click routine our .Net could would look like this:

frmEnterNewCarrier = New frmNewCarrier

frmEnterNewCarrier.ShowDialog()

The first line creates a new instance of the window object we are going to use then the second line then displays the window object as a dialog window which in previous versions of VB it used to be called modal.

Now that the window is on the screen in front of the user, the user has to enter some type of data into a textbox. They also have the choice of clicking an "Ok" button to accept the data they enter then close the window, or click "Cancel" to close the window without doing anything.

When the screen loads the cancel button is always enabled. However I disable the "Ok" button because there is no data entered. Sure I could leave it enabled and allow the user to click it without entering anything but why put that extra check in there when you don't have to.

What I do is in the textbox's "KeyUp" event I enable the "Ok" button only if there is text in the textbox. The line of code would look like this:

btnOK.Enabled = (txtNewCarrier.Text.Trim.Length > 0)

What this line says is to set the ok button's enable property to true if there is text in the textbox. We check on the keyup event because it is the last key event that is executed which is exactly what we need.

You now have code that will prohibit the user from accepting non existent data. I think it is a very nice check. Give it a try and let me know what you think.

Resource Box:
Original Article URL: Prohibit Users From Accepting Data in VB and Vb.net

About the Author: Mr. Tucker is a freelance writer. You can read more of his articles by Clicking Here.


Keywords: net, programming, vb
View Count: 59
Date Submitted: 8/8/2008

Most recent articles in Software category:



Other related articles in Software category:



Recent articles by Bruce A. Tucker: