Subscribe to Subscribers
Talk to Webmaster Tony John

Resources » .NET programming » .NET Framework

Building Windows Application (Address Book) - Part II


Posted Date:     Category: .NET Framework    
Author: Member Level: Gold    Points: 10


This is part 2 of my article series which explains the steps involved in developing a WIndows Application. In the Part-I of this article we created a login form and placed all the required controls in the form. In this article, we will see mor eimportant features like input validation, event handling etc.



 


Building Windows Application (Address Book) - Part II


If you haven't read the Part-I of this article, please read it first.


Building the User Interfaces

In the Part-I of this article we created a login form and placed all the required controls in the form. We also saw how to add the tool tip text for the controls and some properties of the forms object.

Apart from the development and developmental issues I will be telling you more and more useful and interesting properties in VB.NET.

Before getting into our main stream Development of Address Book. Lets see some new properties in the Form (which were not there in VB6.0). In the last part, I explained you about the MaximumSize and MinimumSize of the form, which are not there in the VB6.0 Form.

Now, lets see some more properties of the forms class now.

First lets see the Locked property.

Locked Property

For all the controls which can be placed in the form and form itself, a new property called Locked is added. Of course, this property may not be useful in the run time or it doesn't have any meaning in the run time as such.

But very very useful in the Designing part. If the Locked property is set to True, then that property will be locked for editing ie., changing the size or moving the control are blocked.

We have many more new properties like IsMdiContainer, AutoScroll and Opacity.

We will see them in our next section of the article. Now lets getting into of normal track (Development of Address Book)

Designing the Login Form contd............

In the last section of the article we have designed the login form as shown below.



Now lets see how to validate the fields.

Validations to be done.


  • User name and login name should not be empty


  • These validation is to be checked, when the user comes out the corresponding button or Click the Login button.


  • On click of the cancel button the application should not check for any validation, but the application should be closed.



Now, lets do the validation.

The validations can be done in two ways in VB6.0.


  • Using the LostFocus event, check the validation in the LostFocus event of the text box and if the validation fails using the function SetFocus we can set the focus back in the text box.


  • Using the Validate event write the logic for the validation in the Validate event of the text and if the validation fails, using the Cancel property of the textbox we will be setting the focus back to the same textbox



In the above two methods, the second one is the best, since this cancel method can be blocked for some controls. For example, the validation should not occur for the Cancel button in our login form. This can be done by writing the logic for validation in the Validate event and setting the CausesValidation property to False.

Similarly, we can use both the methods. But the events are changed here.



  • The LostFocus event is replaced by Leave event in the VB.NET. Which is very similar to the LostFocus event of the VB6.0


  • The Validate of VB6.0 is replaced with Validating event in the VB.NET.



Now lets code for the Validation of the Textboxes txtLogin and txtPass.

Private Sub txtLogin_Validating(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) _
Handles txtLogin.Validating
If txtLogin.Text = "" Then
'Code when the login name is empty
e.Cancel = True
Else
'Code when the login name is not empty
End If
End Sub
Private Sub txtPass_Validating(ByVal sender As Object, ByVal e As _
System.ComponentModel.CancelEventArgs) _
Handles txtPass.Validating
If txtPass.Text = "" Then
' Code to be executed when the password is left blank.
e.Cancel = True
Else
' Code to be executed when the password is not blank.
End If
End Sub


Now, lets see what can we do, if the login name or the password is left blank. We show a popup message using the MsgBox function. But that is a old style of doing. Let see what .Net is providing us for displaying the Warning messages. We have component called ErrorProvider, which is going to look into some control and display the error message.

If you could notice I have added one ErrorProvider control erpLogin. We are going to display all the error using that control. Lets see the full code of the Error provider.

Private Sub txtLogin_Validating(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) _
Handles txtLogin.Validating
If txtLogin.Text = "" Then
erpLogin.BlinkRate = 1000
erpLogin.BlinkStyle = ErrorBlinkStyle.AlwaysBlink
erpLogin.SetError(txtLogin, "Login name should not be empty")
e.Cancel = True
Else
erpLogin.SetError(txtLogin, "")
End If
End Sub
Private Sub txtPass_Validating(ByVal sender As Object, ByVal e As _
System.ComponentModel.CancelEventArgs) _
Handles txtPass.Validating
If txtPass.Text = "" Then
erpLogin.BlinkRate = 1000
erpLogin.BlinkStyle = ErrorBlinkStyle.AlwaysBlink
erpLogin.SetError(txtPass, "Password should not be empty")
e.Cancel = True
Else
erpLogin.SetError(txtPass, "")
End If
End Sub


If you can have a close look at the above code. In the txtLogin_Validating event's If block i have used teh BlinkRate, BlinkStyle and SetError Method.

BlinkRate - Property used to Get or Set the Blink rate at which the error icon flashes.
BlinkStyle - Property used to set the Blink Style. It takes three values ErrorBlinkStyle.AlwaysBlink, ErrorBlinkStyle.BlinkIfDifferentError & ErrorBlinkStyle.NeverBlink. I hope the name itself explain the nature of these values.

The SetError function plays a dual roles, ie., both for setting and removing the errors. If you could see I have used the setError function both in the If as well as the Else block.

The SetError function takes two parameter one the control for which the error is to be set and the string parameter (representing the error), if the string is empty, then it removes the error for that control. If the passed string is not empty, it sets that string as the error message and also sets the Error icon as shown in the figure below. The message will be shown as a tool tip text when the mouse is moved over the error icon.



You could see an icon blinking on the right side of the txtLogin control. The position of the icon displayed can also be changed using the SetIconAlignment function of the Error Provider.

It takes two parameter, first one the control to which the ErrorProvider is to be associated and then the Icon alignement.

For example.

erpLogin.SetIconAlignment(txtPass, ErrorIconAlignment.BottomLeft)


The above line of code set the icon to the left of the txtPass textbox.

The icon displayed can be changed by assigning some ico file to the Icon property.

Now, run the application which you have built and see, you will be not be allowed to close the application when the error is occured. This is because you have written a validation for a control and that the validation is not valid.

When you click the close button, actually the validation event once again fires and block the other events. In order to avoid that we must use the CausesValidation which when set to false will block all the validations, only when the control gets the focus.

In our case, let us assume that the control is in the Login name text boxes and the user as not entered any thing in the text box but tries to close the form or clicks the Cancel button. What actually happens is the Validating event once again fires and block all the other events. In order to avoid that we need to set the CausesValidations property to False for the control btnCancel and the form.


Now, that we have added all the required controls, tool tips and also we have done the validations, what else can we do in order to make our application more user friendly. Can we provide a dynamic help for the fields and buttons in the Login Screen. ie., some help to be displayed on pressing the F1 key.

This can be done every simple in VB.NET through a control called HelpProvider. We can set some text message as the Help text or also we can give some external file like HTM, CHM or Hlp files as the help file associated with that control.

In the login screen lets see only displaying the Text help message. We will see displaying the external file in our upcoming section of this article.

I have added a HelpProvide control called HlpLogin to our login form. Upon adding the Help Provider we will get a new property in the Property window called "HelpString on Help provider name", we need to assing the string which should be displayed as the help for that control.

The help will be displayed as a Tool Tip Text to the user on pressing F1 as shown in the following figure.



Now we need to set the Tab order for the control. We should set the tab order properly, which will help the user to traverse through the fields properly.

In VB6.0 we had a Property called TabIndex which is used to assign the tab order for the control.

But in VB.NET that property is removed. The VB.NET IDE has provided a method to set of Tab order visually.

Select the Tab Order in the View Menu. On selecting that menu item, Our desig window will change as shown the figure below, a rectalgulare box representing the tab order will be added to all the control placed in the form.



We can change the tab order of the controls, by clicking the rectangular box. Try that out and mail me if you have any problem in setting them up.



We will stop our development of "Address book" up to this now. In the next section of article we will see the development of the Splash screen and other things.

If you have any queries, doubt or suggestion to improve, please drop me a mail at sadhasivam1981@yahoo.com or sadhasivam1981@hotmail.com.

Visit http://sadhasivam.t35.com to know more about me.





Did you like this resource? Share it with your friends and show your love!


Responses to "Building Windows Application (Address Book) - Part II"
Author: Robert John Hamill    09 May 2004Member Level: Bronze   Points : 0
I am moving from vb6 to vb.net and this has been an excellent view of the changes that have been made. 1st Class


Author: David Mercer    22 Jun 2004Member Level: Bronze   Points : 0
I am just beginning with vb.net and am really enjoying your tutorial. I can't seem to get the error message tooltip to display the error message. The icon appears and blinks, but no error message... any suggestions... keep up the good work...


Author: Robert    09 Jul 2004Member Level: Bronze   Points : 0

I too am a newbie and this tutorial is great! Like David, I originally didn't see the error message until I reset the blinkrate to a higher count. Looks like there is a delay on it appearing.

The problem I encountered is that when I set the form and the Cancel control to False for CausesValidation, I still cannot cancel out without entering some text in the name/password field. Can you set the default focus on the cancel button and would it then close ok?

Looking forward to the rest of the tutorial set on this!!....



Author: Vidya Dharan    07 Sep 2004Member Level: Bronze   Points : 0
The article is very helpful for starters (.NET programmers). Gives a quick understanding of textbox validations in VB.NET.

Thanks a lot...
Geeta



Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Building Windows Application (Address Book) - Part I
    Previous Resource: Building Windows Application (Address Book) - Part III
    Return to Resources
    Post New Resource
    Category: .NET Framework


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    (No tags found.)
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.