Talk to Webmaster Tony John
|
Resources » .NET programming » .NET Framework
Building Windows Application (Address Book) - Part VI
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!
|
|
|
No responses found. Be the first to respond...
|
 Follow us on Twitter: https://twitter.com/dotnetspider
|
Active MembersTodayLast 7 Daysmore...
|