Subscribe to Subscribers
Talk to Webmaster Tony John


Resources » .NET programming » .NET Framework

Building Windows Application (Address Book) - Part VI


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


In this section of the article we will be seeing "How to load a control in runtime"



 


Building Windows Application (Address Book) - Part VI

Building the User Interfaces

Today, we are going to look at the development of Administrator Main window, we should design a window as shown below for the administrator of the Address Book Application.


First, lets see the tip in VB.NET.

Tip in VB.NET

Today, I am going to give a tip which will help in writing your code better.

Now, have a close look at the following class

Class TestClass
Public str As String = ""
End Class


I have declared a class called TestClass with a string member str. Now, observe the followind code and tell me what will be the output of the following code


Dim o1 As TestClass
If o1 Is Nothing Or o1.str = "" Then
MessageBox.Show("Please initialise the string ")
Else
MessageBox.Show("The string is " & o1.str)
End If


If your answer is "Message box showing the string 'Please initialise the String'", then you are wrong.

Even if you say, it displays a "Message box showing the string 'The string is'", you are wrong.

Actually, it raises a "NullReferenceException". Why is that so. Because, conditional expression are all evaluated and then the logical operator are applied to them.

ie., In the statement
If o1 is Nothing or o1.str = "" then
the expression o1 is Nothing is evaluated,

 If True or o1.str = "" Then 


We all know that if any one expression results in true the if block will be evaluated. This expressions results in True,

Then it gets into the second expression o1.str = "" , Here if you see o1 is not initialised. So, it raises the Exception called NullReferenceException.

We should avoid this kind of situation. The code should be modified as follows.


Dim o1 As TestClass
If o1 Is Nothing Then
MessageBox.Show("Please initialise the string ")
ElseIf o1.str = "" Then
MessageBox.Show("Please initialise the string ")
Else
MessageBox.Show("The string is " & o1.str)
End If


I hope that this could help you in writing better code.

Building the Administrator Main Window.

First, let me explain the different section of the Administrator Main Window, then we will develop each section.

The Administrator Main window is split into three section as shown below.


In the section #1 We should have a button representing each user of the Address Book. ie., If there are 3 users, user 1, user 2 and user 3. We should have 3 buttons User 1, User 2 and User 3.

So, we should load the buttons at run time depending upon the available users.

So, first lets see how to create a button at run time. We should clearly look into the following steps for loading any controls.
  • Object of that control should be created and initialised First.

  • We should define the Size coordinates of the controls

  • We should specify the location of the control

  • We should add the control to the container where the control needs to be added

  • We should define the procedure for handling the events of the control

  • We should associate that procedure to the control using AddHandler



Now, Lets create a demo application, where in I am going to create and load a control and handle the click event for the same. Click here to down load the demo application.

Unzip the file, open the project called RunTimeButton. In the project, I am having a Form called frmRunTimeButton.

Actually, I am loading a Button control in the Form at run time. I am creating an object called but, assigning all the necessary properties and then finally I am adding the control to the form using the following code.


Dim but As New Button()
but.Text = "Show Message"
but.Left = 10
but.Top = 10
but.Height = 30
but.Width = 100
Me.Controls.Add(but)
but.Show()



Then, have defined a procedure called ButClick for controlling the Click event of the button as follows.


Private Sub ButClick(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
MsgBox("Button clicked")
End Sub


Now, that I have defined the procedure for handling the event, then I need to link this procedure with the button click event.

So, I have added the following code in the From Load event.


AddHandler but.Click, AddressOf ButClick



For more information on Events & Event Handling see Events in VB.NET Part I & Part II and for delegate vist Delegates in VB.NET Part I and Part II .

We will see the remaining development process in our next section of the article.



If you have any queries, doubt or suggestion to improve, please drop me a mail at
asksadha@yahoo.com and asksadha@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 VI"

No responses found. Be the first to respond...

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 V
    Previous Resource: Building Windows Application (Address Book) - Part VII
    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.)



    Follow us on Twitter: https://twitter.com/dotnetspider

    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.