Monday, February 24, 2014

VB.NET coding tutorial using "Buttons"

______________________________________________________

Buttons contains a lot of events that you can create instruction that the button will do after those events has been established. Such buttons events are the mouse hover, mouse click,  double click, gotfocus and so on.

Let's try the:

"Click" event
         - This tutorial will guide you to create a simple Message Box after clicking a specific button inside the form.

1. Open your visual studio 2013 and create a project.

2. On the default form shown, just try do drag a button for the toolbox menu.

3. Double click the button.

4. You will be located on the source code of your program on where you will type your codes.

5. This time, you will see the code like :

                Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
                         
                End Sub
6. Between those 2 lines of codes you will enter the code that generate a simple message box.

7. Declare a string variable that will handle the message text that will be put on our Message Box. To declare a variable in visual basic, you need to state the word "Dim" before the variable and its datatype. For example:

                 Dim messageVar as String

7. The word "messageVar" is our variable. You can use different words as your variable depending on what you prefer to use. The word "String" is our variable's data type. Data type can be integer, date, byte, long, double, but now ,we will use string for the reason that we need to create a message within a message box.

8. After declaring a variable, we need to assign value that our variable will handle. We declare a string variable so we need to assign a simple string/text message. We will do it like this:

                 Dim messageVar as String = "Hello World"
                                            or
                 Dim messageVar as String
                 messageVar = "Hello Word"

We can assign a value before or after the main declaration of the varialble.

9. To create a Message Box, we used the syntax "MsgBox()" like this:
                 MsgBox(messageVar)

10. As you will notice we put our variable inside the parenthesis because that will be the message that our messagebox will show. Then your done, your final source should be like this:
                 Dim messageVar as String
                 messageVar = "Hello Word"
                 MsgBox(messageVar)


11. Run the program by hitting F5 and push the button inside the form.



______________________________________________________

       
                                           

No comments:

Post a Comment