Monday 1 October 2012

Learn basics of Vb.Net in 10 Simple Posts - Part 2



Welcome back to Learn basics of VB.Net in ten Simple posts. This is the second post in the series and we shall see some more useful ways of using VB.Net and also see how we can improve our first project to do more.  In this post I will be showing how to add a TextBox, Label and also how a little bit coding.

Before we begin lets understand what is Text box and a Label. In simple terms we can say TextBox is a Object where you can Enter a value or Diplay a value and Label would act like a Tag for the Text box showing what data is represented by a TextBox or any other GUI object. For example in the below User Name and Password act as a Label for Textboxs. Where to use what is decided by the requirement.


Now that we know what a Label and a Text box is let us continue with our project.

1. Add two label and two text boxes from Toolbox and arrange them as shown below


2. Change the Text property of Label1 and Label2 to First Name: and Last Name: respectively. The labels might require a slight alignment. Now the form should like this.


3. Now let us read the value from the Labels and display Hellow message. To do this we will have to modify the code behind the Say !! Hellow Button. double click on the Button in design mode we should able to see the code behind it. Add the below line of code and remove any extra code that was created previously

MessageBox.Show("Hellow !!!! " & TextBox1.Text & vbTab _
                        & TextBox2.Text, "Welcome to TechLabs")

Complete code should look as below


By now you must be wondering previously we used MsgBox to display message and now we are using MessageBox.Show to display message box. MsgBox is VB6 kind of message box and MessageBox.Show is VB.Net method both are equally useful. 

The Method MessageBox.Show would take different parameters I will discuss a few of them, first parameter value is the display message and second is the Title of the message box. similarly there are other parameters which we can discuss as required.

So the display message is "Hellow !!!! " & TextBox1.Text & vbTab & TextBox2.Text, lets understand part by part. First part "Hellow !!! " is a static text and & is used to concatenate the string a very useful and most commonly used one. TextBox1.Text would give the value entered from the Text box and vbTab is to add a space between the Strings. Its simple until we try it.

once the code is added save it and debug. On clicking Say !!! Hellow button the bellow message should be displayed.


4. Congrats now you have successfully completed your second exercise. Tune in for more.



No comments:

Post a Comment