Building Windows Application (Address Book) - Part IV
Building the User Interfaces
Now, we will see some more development process of our address book application. In the pervious sections of articles we saw the development process of the Login form and Splash Screen. We will be seeing some tip as usual in the beginning, and then I will be telling you the various methods of sharing data between the forms.
Now, lets see the tips quickly.
Tips in VB.NET
Let me tell you some tip on Button. As I have already told you, the caption property for all the control is removed and replaced with Text property and so with the Button also.
First Tip
Setting up the BackColor property for the button
In VB6.0, we will not be able to set the BackColor property, we need to set the Style property to Graphical. Same with the Picture property of the Button.
But it is not the same in VB.NET, the BackColor and Image (replacement for Picture property of the Button) can be changed directly.
In VB6.0, changing the fore color of the Button is not allowed, but it can be done using some API Call in VB6.0. In VB.NET they can be changed directly by using the ForeColor property.
Setting up the Style for the Button
In VB.NET we can build four types of buttons,

Each style has it own characteristics. They can be chosen by assigning any one of the above type to the property called FlatStyle.
Now, lets get back to the actual development process.
We are only going to discuss the "How datas can be shared between in the forms" and what way we are going to choose for data sharing in the development of Address Book Application.
Different methods of Sharing data between forms
- Sharing the data globally
- Sharing only between the two forms
- Using the Relationship
Lets see one by one with examples in Detail.
First lets see, how the data can be stored globally through out the form. I hope every one knows that we can have Public Modules in VB.NET. We can use the Modules to help us to share the data globally. What we can do is, have some Public variables/constants, these public constants/variables can be accessed globally, with out creating any instance. Let me give explain this with an example, Click here to download the demo application.
Extract the zip file to some folder, you will be getting the VB.NET Project called ShareGlobally. Open that project, you will be having three forms called FrmFirstForm, FrmSecondForm and FrmThirdForm. Also I have one public module called PublicModule which is going to help me in sharing the data.
What I am actually I am collecting some Student information in the form one and form two and I am going to display that in Form Three.
In the first form, we collect the student name, age, and parent name. On the Next button click event we are assigning the values to the public variable in the modules. And we are doing the same with the second form also.
And in the third form, we are reading the Variables stored and displaying the same in that form.

Download the code from the above link and try the project. If you have any doubts or clarifications, feel free to contact me. So I feel, it won't be tough to understand the example; I have added all possible comments to the code.
Sharing only between the two forms
The data between the two forms can be shared in any of the following way (There are many ways to share the data, but I am going to tell only these two methods)
- Using the constructor
- Having some public variables or properties in the second form, where the data is
required.
We will see both the methods. Click Here to download the demo application for this method.
In the demo application I have three forms frmInput, frmUsingConst and frmUsingVar. I have designed the frmInput form as shown in the following figure.

Then I have two buttons which is going to call the frmUsingConst and frmUsingVar form and pass the data.
In the frmUsingConst I have two private variables A and B. And I have a Label control lblResult, I am adding the two values and assigning the data to the label control I have placed the following code in that form.
Dim A, B As Integer Public Sub New(ByVal val1 As Integer, _ ByVal val2 As Integer) MyBase.New() InitializeComponent() A = val1 B = val2 End Sub Private Sub FrmUsingConst_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load lblResult.Text = "The result is " & (A + B) End Sub
In the button click event I have done like this (for the button which is going to called the form which takes the data through the constructor)
Private Sub btnOne_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnOne.Click Dim frmFUC As New FrmUsingConst(CInt(txtA.Text), CInt(txtb.Text)) frmFUC.Show() End Sub
Now, lets see how we are going to share the data using the public variables in the frmUsingVar.
I have placed the following code in the frmUsingVar form.
Public A, B As Integer Private Sub frmUsingVar_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load lblResult.Text = "The result is " & (A + B) End Sub
I hope the above code doesn't need any explanation. In the Button click event (btnTwo) write the following code.
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles Button1.Click Dim frmvar As New frmUsingVar() frmvar.A = CInt(txtA.Text) frmvar.B = CInt(txtB.Text) frmvar.Show() End Sub
What actually I am doing in the above code is I am creating an instance of the form FrmUsingVar and assigning the value of A and B, which I entered in the text boxes, and activating the form. Where in the form load event I have written the code and sum them up and display in the label.
Restricted Sharing
I don't know how to call this method of sharing the data. Because here we are going to share the values using the relationship between the forms. That is, say for example if we have the mdiform and child form relation ship defined.
Then we can use the MdiParent property defined in the child form to get the instance of the MDIParent form running.
Similarly we can get the instances of the child form through the property called MdiChildren actually it returns a collection. Which can be used to get the instance of the child forms running.
We will see more on this method, in the development, we will be using the first and third type of data sharing in our development.
There are many other methods available and even a lot of round about methods are also available.
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.
|
No responses found. Be the first to respond and make money from revenue sharing program.
|