Showing posts with label VB.Net Basics. Show all posts
Showing posts with label VB.Net Basics. Show all posts

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.



Saturday, 29 September 2012

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

Hi, this is the first post where I am going to give a brief introduction on VB.Net and how to start learning it. If you know any of the programming basics or good at logic and make use of existing resources then you are at the right place to gain all the knowledge.
All the posts in the series would use Microsoft Visual Studio 2010 (VS 2010) to demonstrate the VB.Net.

Creating First Project in VS 2010
1.       Open Microsoft VS 2010
2.       Goto File->New Project (Ctrl + N shortcut)
3.       Select Windows on the left pane and Project type as Windows Forms Application from middle pane. At the bottom of the Window Name: WindowsApplication1 is given by default change it to MYFirstProject and click on Ok.




4.       Now the screen should be showing the below the Form Form1.vb is created by default by VS and the solution Explorer shows the project files such as all the VB files, forms etc


5.       Now let us Debug the project created. Go to Debug -> Start Debugging (F5 shortcut).  And the below window should pop up.

6.       Next would be changing the Form name and Form Title. To do this we need to change the property of the Form.                The Property of any object in VB.Net can be changed in two ways one is by changing the property from properties window and other by changing the property from code. Both are equally helpful and would need understanding on how to use and when to use.



The Properties window shows the various properties that an object supports. Let us understand few of them

a. Name: specifies the name of the Object.
b. Text:  this property is to give the display value for the object. For a form it would be the Title and for a Button it would be a Button name and so on.
c.  Font :  this property is to change the font type.
d. ForeColor: this property is to change the color of the text.
e. BackColor: this property is to change the background color of the object.
f.  Enabled: this property is used to Enable or Disable the Object
g. StartPosition: this property is used to set the start position of the Form. Like the form should at the center of the screen, bottom right etc.

There are still a lot of properties that we will be using let use discuss on them when we come across.




7.       Let us change the Form name from Form1 to MyFirstForm and Form Text from Form1 to Hellow Form.
8.       Let us add the first control on the Form MyFirstForm. From the ToolBox click on Button and drag the Button on the Form and place it somewhere on the form.

9.       Let us rename the Button name and Button text. Select the Button on the Form and go to Properties window. And change the Button Name property to HellowButton and Button Text to Say !! Hellow . The form should as below now


Note: The button size might not be sufficient to hold the Button Text as “Say !! Hellow” increase the button width and should work. Also for the form can be decreased.
10.   Now that we have added a button on the form let us assign functionality to the form. When we click on the Button then a popup should come up saying “Hellow”. To do this double click on the Button and it should take to you to the code screen of Form1



The above code is generated by default. We shall discuss on each of them in details in later posts. As of now let us just add the required functionality.
Add the below lines of code before End Sub.

MsgBox (“Hellow !!!” )




11.   Now let us debug the code by pressing F5 and click on Say !! Hellow Button

12.   Congrats we just completed our first VB.Net Project. Stay tuned for more.