|
This
will be your first Visual Basic Application. I will try to explain everything
in as much detail as I can.
This
Simple program features a button. When you click on the button, a MessageBox
pop's up and displays, "Hello World." Any Questions about this
tutorial, please visit vbProgramming Forums.
Starting
Up
When you open up Visual Studio. NET, there will be a "Start Page."
This displays either your most recent programs, or your "profile."
In the start page, select, 'My Profile' (to your left). Make the first list
box, "Visual Basic Developer." Now, VS. NET is now arranges itself to
make it more resourceful for VB.NET developers. The next thing you have to do is create a new
project, go to File | New | Project.
Select Windows Application and name the project "Hello World".
You will see a blank window, this is known as the form. You can drag and drop
components from the toolbox (look to the left of the form). Once you do that,
you will set their properties such as Name, Height, Width, Position, and Text.
Creating
Objects
First, create a new button on the form, drag and drop the button control from
the toolbox (or double-click it) and place it on the form. The button's
default text should be "Button 1".
Modifying
Properties
Click the button on the form, go to the properties window (bottom-left) change
the Text property to "Click Me". The button will now say,
"Click Me" instead of "Button1."Set the name property to
MyButton. The name property is important, because when you code, you must
specify what object you are referring to (like MyButton and MyTextBox). Its just
like creating Robots. They all look alike, you just need to name them in order
for them to know that you are talking to them. (OK, maybe this is a
far-fetched but.. anyway, a name is a name.)
Coding and
Creating Events
Coding is
obviously an important aspect of programming! Our program doesn't do anything
yet, we need to code in order to tell the button what to do. If we don't code,
our button will not do anything. So, open up your code (View |Code or press
F7). You will notice (up at the top), 2 drop down menus. The first one is the
object Menu and the one next to it is the events menu. In the objects menu,
select: MyButton. In the events Menu select Click. For future references, I
will say "MyButton.Click" from now on. When you selected MyButton
and Click, VB.NET generated some code for you. This is to save a LOT of time
writing code! All you need to worry about are the first 2 words (Sub
MyButton_Click)
and the last 2 words (End
Sub).
Well, that should be self explanatory, lets take a look at what it generated
for us. The area in green is what I have typed in, read it thoroughly.
---------------------------------------------------------------------------------------------------------------------------------------------------
Sub
MyButton_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles
MyButton.Click
'In Between these 2 lines are the code that
will be executed when MyButton is clicked.
'This line is a comment, its not real code, its just there so that people can
understand the code
'A comment is preceded by an apostrophe ( ' ). It is a really helpful way of
making sure you don't get confused by your code
End
Sub
---------------------------------------------------------------------------------------------------------------------------------------------------
In between both lines, type in:
MessageBox.Show("Hello
World!")
Running your
Program
Everything in between MyButton.Click and End Sub
Executes when you click MyButton. In this case, when you click MyButton, a 'MessageBox'
appears, saying Hello World. Run your program. Try it out! Press F5 to run
your program. An alternative way to run your program is to go to Debug |
Start. This is your first Visual Basic program (I think)!
The Source Code
for this tutorial is located here:
You can also
locate this by logging in to vbProgramming Forums and going to:
Tutorials > Tutorial Source Code > Source Code
|
|