Sunday, June 14, 2009

How to Use the Error Provider in VB.NET


Error Provider ComponentError Provider is one of the many components in VB.NET that lets your application display error messages once a control triggers its validating or validated event. To understand, here's a step-by-step procedure on how to use the Error Provider:


1. Double click or drag the error provider button into the form. The error provider named ErrorProvider1 appears at the bottom of the design area as shown below:
Error Provider
2. Create a form exactly as shown below and change the name of TextBox1 and TextBox2 into txtTelNo1 and txtTelNo2 in the Properties Window.
Error Provider 3
3. Type the following in the validating event of txtTelNo1 and txtTelNo2:

Private Sub txtTelNo1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtTelNo1.Validating
ValidateTelNo(DirectCast(sender, TextBox))
End Sub


Private Sub txtTelNo2_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtTelNo2.Validating
ValidateTelNo(DirectCast(sender, TextBox))
End Sub

4. Next, type the following subroutine that takes the txtTelNo1 and txtTelNo2 as its parameter and sets the error in the ErrorProvider1 for textbox that doesn't contain exactly 7 digits.

Private Sub ValidateTelNo(ByVal txtTelNo As TextBox)
If (txtTelNo.Text.Length <> 0) And Not (txtTelNo.Text Like "#######") Then
ErrorProvider1.SetError(txtTelNo, txtTelNo.Name & " must contain exactly 7 digits")
Else
ErrorProvider1.SetError(txtTelNo, "")
End If
End Sub

5. For the Button1 Click Event, type the following:

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
If IsNotValidTelNo(txtTelNo1) Then Exit Sub
If IsNotValidTelNo(txtTelNo2) Then Exit Sub

MessageBox.Show("Success!")
End Sub

6. Finally, type the following function used in Button1.Click event that takes a textbox as its parameter and returns True or False:

Private Function IsNotValidTelNo(ByVal txtTelNo As TextBox) As Boolean
If ErrorProvider1.GetError(txtTelNo).Length = 0 Then
Return False
Else
MessageBox.Show(ErrorProvider1.GetError(txtTelNo))
txtTelNo.Focus()
Return True
End If
End Function

The program checks for the number of digits contained in the textbox and prompts the user for any error once the button is clicked.
Error Provider
Read more...

Wednesday, June 3, 2009

How To Customize Visual Basic.NET IDE

The default Microsoft Visual Basic.NET IDE including its Windows, Menubar, and Toolbar among others are very useful in developing applications. However, there are situations that you do not need all of the tools included in the IDE or you need some tools that are not included in the default setup. Here are some tips on how to customize the Visual Basic.NET IDE.


Customization options are found in Tools in the Menu Bar
Tools Menu

Go to Tools >> Customize or right click the menu bar or toolbar and select Customize from the pop-up menu and a dialog box like this one will appear:
Customize Dialog Box

Under Toolbars tab, you can check the items you want to be added on your toolbar. Now click the Commands tab and you will see the Categories list box vis-à-vis its Commands. The picture below shows the Commands under Debug Category. Click a Command and drag it to the toolbar.
Customize Dialog Command Tab

While the Customize Dialog Box is open, you can select items in the menu bar and toolbar. Right click the item and a pop-up menu will allow you to reset, delete, rename or modify it as shown below.
Pop-up Menu

Read more...

Testing

Hello World! Read more...