KEMBAR78
Constructing Windows Applications Notes | PDF | Computer File | Typefaces
0% found this document useful (0 votes)
33 views37 pages

Constructing Windows Applications Notes

The document provides a step-by-step guide for beginners on how to create a simple Windows application using Visual Basic .NET. It covers launching the software, creating a new project, adding controls like textboxes and labels, and modifying their properties. Additionally, it explains the design and debug environments, as well as how to customize the appearance of the form and its controls.

Uploaded by

oimarigobert
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views37 pages

Constructing Windows Applications Notes

The document provides a step-by-step guide for beginners on how to create a simple Windows application using Visual Basic .NET. It covers launching the software, creating a new project, adding controls like textboxes and labels, and modifying their properties. Additionally, it explains the design and debug environments, as well as how to customize the appearance of the form and its controls.

Uploaded by

oimarigobert
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 37

Launch your Visual Basic .NET or Visual Studio software.

When the software first loads,


you'll see a screen something like this one:

Click here to see the image in a new popup window (86K)

There's a lot happening on the start page. But basically, this is where you can start a new
project, or open an existing one. The first Tab, Projects, is selected. At the moment, the
area labelled "Open an Existing Project" is blank. This is what you'll see when you run
the software for the first time (because you haven't created a project yet). When you
create a project, the Name you gave it will be displayed on this page, as a hyperlink.
Clicking the link will open the project.

At the bottom of the screen, there are two buttons: "New Project" and "Open Project". To
get started, click the "New Project" button. When you do, you'll see this dialogue box
appear:

As a beginner, you'll normally want the option selected: "Windows Application", in the
"Visual Basic Projects" folder. This means that you're going to be designing a programme
to run on a computer running the Microsoft Windows operating system.
If you look in the Name textbox at the bottom, you'll see it says "WindowsApplication1".
This is the default name for your projects. It's not a good idea to keep this name. After all,
you don't want all of your projects to be called "WindowsApplication1",
"WindowsApplication2", etc. So click inside this textbox and change this Name to the
following:

My First Project

Keep the Location the same as the default. This is a folder inside of your "My
Documents" folder called "Visual Studio Projects". A new folder will then be created for
you, and its name will be the one you typed in the "Name" textbox. All of your files for
your first project are then saved in this folder.

Click the OK button, and the Visual Basic NET design time environment will open. It
will look like the following (the 2008 edition is just the same):

The Default Form


In the Visual Basic NET design time environment, the first thing to concentrate on is that
strange, big square in the top left. That's called a form. It's actually the pretty bit of your
programme, the part that others will see when they launch your masterpiece. Granted, it
doesn't look too attractive at the moment, but you'll soon discover ways to lick it into
shape.

To run the form, try this:

 From the menu bar, click Debug


 From the drop down menu, click Start
 Alternatively, press the F5 key on your keyboard
 Your programme is launched

Congratulations! You have now created your very first programme. It should look like
this:
Click the Red X on the form to stop it from running. You will then be returned to the
software environment.

If you compare the first form with the one above, you'll see that they look very similar.
But the one above is actually a real programme, something you could package and sell to
unsuspecting village idiots

So what's going on? Why the two different views? Well, Visual Basic has two distinct
environments, a Design environment and a Debug environment. Design Time is where
you get to play about with the form, spruce it up, add textboxes, and buttons, and labels
(and code, of course ); Debug is where you can test your programme and see how well it
performs. Or doesn't perform, as is usually the case.

But don't worry about the terminology, for the time being. Just be aware that there's a two
step process to VB programming: designing and debugging.

Adding a Control to a Form


Things like buttons, textboxes, and labels are all things that you can add to your Forms.
They are know as Controls, and are kept in the Toolbox for ease of use.

The Toolbox can be found on the left of the screen. In the picture below, you can see the
toolbox icon next to Form1:

To display all the tools, move your mouse over the toolbox icon. You'll see the following
automatically appear:
There are seven categories of tools available. The toolbox you'll be working with first is
the Common Controls toolbox. To see the tools, click on the plus symbol next to
Common Controls. You'll see a long list of tools:

As you can see, there are an awful lot of tools to choose from! For this first section, we'll
only be using the Button, the TextBox and the Label.
How to Add a Control to your VB .NET Forms
Let's start by adding a textbox to our form. With the tools displayed, do the following:

 Locate the TextBox tool


 Double click the icon
 A textbox is added to your form

The textbox gets added to the top left position of your form. To move it down, hold your
mouse over the textbox and drag to a new position:

Notice the small squares around the textbox. These are sizing handles. Move your mouse
over one of them. The mouse pointer turns into an extended line with arrowheads. Hold
your left mouse button down and drag outwards. The textbox is resized. Play around with
the sizing handles until you're happy with the size of your textbox.

One thing you will notice is that you can't make the size any higher, but you can make it
wider. The reason why you can't make it any higher is because the default action of a
textbox is to have it contain only a single line of text. If it's only going to contain one line
of text, Microsoft reasoned, there's no reason why you should be able to change its
height. A textbox can only be made higher if it's set to contain multiple lines of text.
You'll see how to do this soon.

 Create two more textboxes by double clicking on the textbox icon in the toolbar
(Or Right-click on the selected textbox and choose Copy. Then Right-click on the
Form and choose Paste.)
 Resize them to the same size as your first one
 Line them up one below the other with space in between
 Try to create something that looks like the one below
No more reading these lessons online - get the eBook here!

Adding a Label to your Form

Let's add some labels near the textboxes so that your users will know what they are for.

 Locate the label control in the toolbox


 Double click the label icon
 A new label is added to your form
 It should look like the one below
Click on the label to select it. Now hold your left mouse button down on the label. Keep
it held down and drag it to the left of the textbox.

Create two more labels, and position them to the left of the textboxes. You should now
have a form like this one:

To see what your Form looks like as a programme, click Debug > Start from the menu
bar. Or press F5 on your keyboard:

To stop the programme from running, you can do one of the following:

1. Click the Red X at the top right of your Form


2. Click Debug > Stop Debugging from the menu bar
3. Press Shift + F5 on your keyboard
You can also click the Stop button on the VB toolbars at the top, as in the image below:

All right, we're getting somewhere. We now have a form with textboxes and labels,
something that looks like a form people can fill in. But those labels are not exactly
descriptive, and our textboxes have the default text in them. So how can we enter our
own text for the labels, and get rid of that default text for the textboxes?

An Introduction to VB .NET Properties


You've probably noticed the area to the right of the design environment, the area with all
the textboxes in a grid, the one that has those daunting names like
"AccessibleDescription", "AccessibleName", "AccessibleRole". That's the Properties
box.

Click anywhere on the form that is not a label or a textbox, somewhere on the form's grey
areas. The form should have the little sizing handles now, indicating that the form is
selected.

On the right of the design environment there should be the following Properties box:
If your Properties box says "Textbox1 Textbox" or "Label1 Label" then you haven't yet
selected the Form. Click away from the textbox or label until the Properties box reads
"Form1 Form"

What you are looking at is a list of the properties that a form has: Name , BackColor,
Font, Image, Text, etc. Just to the right of these properties are the values for them. These
values are the default values, and can be changed. We're going to change the value of the
Text property.

First, you might want to display the list of Properties in a more accessible form. You can
display the list properties alphabetically. To do that, click the Alphabetic icon at the top
of the Properties box, as in the image below:
This will make the properties easier to find.

Before we change any in the Properties box, let's clear up what we mean by "Property".

No more reading these lessons online - get the eBook here!

What is a Property?
Those controls you added to the form (textboxes and labels), and the form itself, are
called control objects. You can think of controls as things, something solid that you can
pick up and move about. Controls (things) have properties. If your television were a
control, it too would have properties: an On/Off button property, a colour property, a
volume property, and a ... well, what other properties would your television have? Think
about it.

The properties of your television will have values. The On/Off button would have just
two values - On or Off. The volume property could have a range of values, from zero to
ten, for example. If the value of the volume property was set to ten, the loudest value,
then you'd probably have some very angry neighbours!

In VB.NET, you can change a property of a control from the Properties Box. (You can
also change a property using code, which you'll do quite a lot.) If we go back to our Form
object, and the properties and values it has, we can see how to change them using the
Properties Box. We'll change only one of these values for now - the value of the Text
property . So, do this:

 Locate the word "Text" in the Property box, as in the image below

"Text" is a Property of Form1. Don't be confused by the word "Form1" next to the word
"Text". All this means is that the current value of the Text property is set to the word
"Form1". This is the default.
To change this to something of your own, do this:

 Click inside the area next to "Text", and delete the word "Form1" by hitting the
backspace key on your keyboard
 When "Form1" has been deleted, type the words "My First Form"

 Click back on the form itself (the one with the labels and textboxes), or hit the
return key on your keyboard
 The words "My First Form" will appear as white text on a blue background at the
top of the form

When you've correctly changed the Text property, your Form will then look like this one:

As you can see, your new text has gone at the top of the form, in white on the blue
background.

So the Text Property of a form is for setting the caption you want to display in the title
bar at the top.

In the next part, we'll take a look at how to change the text property of labels and
textboxes.

The Text Property of a Control


Changing the values of some properties is fairly easy. We'll now change the Text
properties of our labels, and the Text properties of our Textboxes.

Click on Label1 so that it has the sizing handles, and is therefore selected. Examine the
Property box for the Label:
You can see that the Label control has quite a few different properties to the Form
control. Think back to your television as an control. It will have different buttons and
knobs to your DVD Player control. A label has different "buttons and knobs" to the
Form's "buttons and knobs".

But the Label has a lot of properties that are the same. The Text property of a Label does
what you'd expect it to do: adds text to your label. We'll do that now:

 With label1 selected, click inside the area next to "Text", and delete the word
"Label1" by hitting the backspace key on your keyboard
 Type in the words "First Name"
 Click back onto the grey form, or hit the return key on your keyboard
 Label1 has now changed its text caption to read "First Name"
 If you've made a typing error, go back to the first step above and try again
 Your form should now look like this:
Now, change the Text property of the other two labels. Change them to these values:

Label2: Last Name


Label3: Telephone Number

What you should notice is that the labels resize themselves, after you press the enter key
to commit the changes. You may need to drag your labels to the left a bit. But when
you're finished, your form should look like ours below:

The form might look a little squashed, though. Is there anything we can do to make it
bigger? Well, it just so happens there is.
The Form can be resized just like the Label and the textboxes. Click anywhere on the
form that is not a textbox or a label. If you look closely around the Form's edges, you'll
notice our old friends the sizing handles. To make the form bigger, just stretch them like
you did the labels and the textboxes. Play around with the size of the form until you're
happy with it. You can now reposition and resize the textboxes and labels so that things
don't look too squashed. Your form might look like this one:

Click on Debug > Start to have a look at your programme. Or Press F5 on your
keyboard. Click Debug > Stop Debugging to get back to the design environment. (Or
press Shift + F5, or just click the red X at the top right of the form.)

Adding a Splash of Colour


At the moment, our form looks a little bland. Time to liven it up with a splash of colour.

Changing the colour of the Form means we have to change one of its properties - the
BackColor property.

So click anywhere on the form that is not a textbox or a label. If you do it right, you
should see the sizing handles around the edges of the grey form. The Property Box on the
right will read "Form1", and that indicates that you have indeed selected the form. When
the Form is selected you can change its properties.

To change the colour of the Form, click the word "BackColor" in the Property Box. Next,
click the black down-pointing arrow to the right. A drop-down box will appear.
The default colour is the one selected - Control. This is on the System Tab. The System
colours are to set whatever colour scheme the user has opted for when setting up their
computers. For example, you can use the Display Properties dialogue box in Windows
XP to change how things like menus and buttons look. Someone who is colour-blind
might have changed his or her settings in order to see things better on the computer
screen. If you stick with the System colours then a colour-blind user of your programme
would not have any problems seeing your master work.

As you can see in the image above, you can choose the colour of the Active Caption. The
Active Caption is the one you set earlier when you changed the text to "My First Form".
The Active Caption is blue on my computer, and the Active Caption Text is white. It
might be different on yours.

If you want to choose a colour that is not a System colour, click the Custom Tab. You'll
then see this:
Click on any of the Colours in the colour palette and the background colour of your form
will change.

You can also select the Web Tab. When you do, you'll see a list of Web-Safe colours to
choose from. A Web-Safe colour is one that displays correctly in a web browser,
regardless of which computer being used (that's the theory, anyway). You might want to
use a Web-Safe colour if you're designing a project for the internet. But you can choose
one even if you're not.

No more reading these lessons online - get the eBook here!

To change the colour of the labels, click on a label to select it. Look in the Property box
to see if it reads Label. If so, you can now go ahead and change the BackColor property
of the Label in exactly the same way that we changed the BackColor property for our
Form.

Change the colour of the other two labels to anything you like. To change the colour of
more than one Label at a time, click on one Label to select it. Now, hold down the "Ctrl"
key on your keyboard and click another Label. You'll see that two Labels now have
sizing handles around them. Click the third Label with the "Ctrl" key held down, and all
three Labels will be selected. You can change the BackColor property of all three at once.
If you want to change the Font size of the Labels and Textboxes, select a control. Let's
start with Label1.

 So click on Label 1
 Scroll down the Property Box until you see Font
 Click on the word "Font" to highlight it
 MS Sans Serif is the default Font

Notice that the Font property has a cross next to it. This indicates that the property is
expandable. Click the cross to see the following:

Notice that the Font property has a cross next to it. This indicates that the property is
expandable. Click the cross to see the following:
As you can see, you can change a lot of Font properties from here: the Name of the font,
its Size, whether is should be Bold or not, etc. You can also click the square box with the
three dots in it. This brings up a dialogue box where you can change the font properties in
the same place.

Make the following changes to the three labels:

Font: Arial
Font Style: Bold
Font Size: 10

Change the Font of the three Textboxes so that they are the same as the Labels.

How to Save your VB .NET Projects


If you have a look in the top right of the Design Environment, you'll see the Solution
Explorer. (If you can't see it, click View > Solution Explorer.)

The Solution Explorer shows you all the files you have in your project (Notice that the
name of your project is at the top of the tree - "My First Project").
At first glance, it looks as though there are not many files in the project. But click the
Show All Files icon, circled below:

When you click Show All Files, the Solution Explorer will look something like this:

When you save your project, you are saving all these files.

To save your work, click File > Save All and you'll see the following dialogue box
(we've chopped ours down a bit):

The files are usually saved in the My Document folder in XP (Document folder in Vista),
under Visual Studio. If you want to save your projects elsewhere, click the Browse
button.

To actually save your work as you go along, just click File > Save All from the menu bar.
Or press Ctrl + Shift + S on your keyboard. Or click the icon in the Toolbar (the stack of
floppy disks). If you save often then you won't lose any of your work if anything goes
wrong with your computer.

Form Names in VB .NET


Time to get our hands dirty with a bit of programming. There's no putting it off any
longer, I'm afraid! We'll create a new project for this section.

So, if you already have you VB NET software open, you can get rid of the current project
by clicking File from the menu bar. From the drop down menu, choose Close Solution.
You will be returned to the Start Page. Click the "New Project" button at the bottom.
When you get the dialogue box popping up, choose Windows Application at the top.
Then change the name from WindowsApplication to Variables. You can keep the
location set at the default.

When you click the OK button, a new form will appear.

If you look at the Solution Explorer at the top, you'll see the name of the project has
changed to the Name you gave it.

The name of the Project is now Variables - the same name as the folder that is created for
you to hold all your project files.

Before we get started, it's worth exploring an oddity of the VB Net software.

Click on your Form to select it. Then change the Name property to frmVariables. Press
the return key on your keyboard to confirm the name change, or just click back on the
form:
The Name property of your Form has now been changed from Form1 to one of your own
choosing (well, ours).

Now press F5 to run the project, or click Debug > Start from the menu.

Instead of the programme running, you'll probably get this message box:

Click the No button to return to the Design Environment. You might now have Task List
screen displaying at the bottom of your screen:

A rather cryptic message appears: "Sub Main was not found in Variables Form1". You
can get rid of the Task List by clicking the X, or the Pin symbol.

The reason you got that message is that VB is still trying to run a form called Form1.
Even though you changed it's name, it still tries to do something with it. But you no
longer have a form called Form1, so VB gives you the error message. Hopefully,
Microsoft will correct this in the next version.

No more reading these lessons online - get the eBook here!


But here's the solution to the problem.

In the Solution Explorer window, right click on the name of your project (Variables). A
menu should appear. Click Properties right at the bottom:

Notice that we've clicked on "Variables", and not on "Solution Variables". When you
click Properties, you get this dialogue box popping up:

The Startup Object, as you can see, is still set to Form1. Click the down arrow to reveal
the following:
The name you gave your form is on the list. Select frmVariables and click the OK button.
Now run your programme again. You should get no error messages this time, and the
form will load properly

How to Create Variables in VB .NET


Why are we discussing variables? And what is a variable?

With Visual Basic, and most programming languages, what you are doing is storing
things in the computer's memory, and manipulating this store. If you want to add two
numbers together, you put the numbers into storage areas and "tell" Visual Basic to add
them up. But you can't do this without variables.

So a variable is a storage area of the computer's memory. Think of it like this: a variable
is an empty cardboard box. Now, imagine you have a very large room, and in this room
you have a whole lot of empty cardboard boxes. Each empty cardboard box is a single
variable. To add two numbers together, write the first number on a piece of paper and put
the piece of paper into an empty box. Write the second number on a piece of paper and
put this second piece of paper in a different cardboard box.

Now, out of all your thousands of empty cardboard boxes two of them contain pieces of
paper with numbers on them. To help you remember which of the thousands of boxes
hold your numbers, put a sticky label on each of the two boxes. Write "number1" on the
first sticky label, and "number2" on the second label.

What have we just done? Well, we've created a large memory area (the room and the
cardboard boxes), and we've set up two of the boxes to hold our numbers (two variables).
We've also given each of these variables a name (the sticky labels) so that we can
remember where they are.

Now examine this:

Dim number1 As Integer


Dim number 2 As Integer

number1 = 3
number2 = 5
That's code from Visual Basic Net. It's VB's way of setting up (or declaring) variables.

Here's a breakdown of the variable Declaration:

Dim
Short for Dimension. It's a type of variable. You declare (or "tell" Visual Basic)
that you are setting up a variable with this word. We'll meet other types of
variables later, but for now just remember to start your variable declarations with
Dim.
number1
This is the cardboard box and the sticky label all in one. This is a variable. In
other words, our storage area. After the Dim word, Visual Basic is looking for the
name of your variable. You can call your variable almost anything you like, but
there are a few reserved words that VB won't allow. It's good practice to give your
variables a name appropriate to what is going in the variable.
As Integer
We're telling Visual Basic that the variable is going to be a number (integer). Well
meet alternatives to Integer later.
Number1 = 3
The equals sign is not actually an equals sign. The = sign means assign a value of.
In other words, here is where you put something in your variable. We're telling
Visual Basic to assign a value of 3 to the variable called number1. Think back to
the piece of paper going into the cardboard box. Well, this is the programming
equivalent of writing a value on a piece of paper

Now that you have a basic idea of what variables are, let's write a little piece of code to
test them out. First, though, let's have our first look at the coding window.

To make life easier, we're going to put a command button on our form. When our
command button is clicked, a little message box will pop up. Fortunately, there's no
coding to write for a command button, and very little at all for a message box.

Using Variables, Integers, and Strings


All right you say, enough of the throat clearing, what can I DO with these things? I've
opened up a new Visual Basic project, and picked Windows application. I've got my
Windows form open in design mode, I've just dragged Button1 over on top of it from the
Toolbox, and I want to make this button do something. This current state of my form is
shown in Figure 1 below.
Figure 1. Making a Button do something

We'll leave its name as Button1 for now, but in a real program you would probably
choose to rename your buttons and labels to the Visual Basic naming conventions that
communicate their functions more clearly: btnSubmit, btnExit, lblMaryNewAge.

Right-click on the button and set the text property of Button1 to Show Mary's Age(s), as
shown in Figure 2.
Figure 2. Properties for the Button

Double-click on your button to get the code view of the situation (Figure 3). Note how
your cursor lands smack-dab in the middle of the place it needs to be to make that button
do something once an unsuspecting user clicks on it.
Figure 3. Code for the Button

Private Sub is not, despite what you might think, a sandwich that you don't have to share
with your coworkers, or your personal vehicle for a voyage under the sea. Private Sub is
the notation for a private sub procedure.

Private means that what goes on inside Private Sub until End Sub will not apply to the
rest of the program. So, if you name a variable MyDog and set the value of that variable
to the string Spot", only inside that sub procedure will the dog be Spot. If the user clicks
another button, the variable for the dog name could begin at Rover.

Type in the following code such that the whole sub procedure looks like this

Copy
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim intMaryAge As Integer
Dim strMaryName As String

intMaryAge = 7
strMaryName = "Mary"
MessageBox.Show("Hi, my name is " & strMaryName & _
" and my age is " & intMaryAge)
End Sub

When you click on the blue arrow to run the program, you should see a message window
pop-up that looks like Figure 4.

Figure 4. Message box

If you didn't type spaces exactly as in the example, the message words may be squashed
or look odd. Adjust what's inside the quotation marks until the message box text looks
normal.

To quit the program you just made, click on the blue square as shown in Figure 5.

Figure 5. Stopping the running program

Now for the purposes of this example, because the program is only displaying Mary's age
and not doing any math, we could have gotten away with making it strMaryAge rather
than intMaryAge and declaring that as a string instead of an integer. But in the next
example, we are going to do something to show the difference between integers and
strings.

1. Click on Design view.


2. Add another Label to your form (Label1).
3. Add a text box (TextBox1), with the text property in both the label and the text
box to be nothing. You do this by right-clicking on the label and the properties
window should show up on the right pane. Scroll down to where you can see the
Text property and edit it to be blank. Do the same for Label1. An example of
these setting is shown in Figure 6.

Figure 6. Properties for the TextBox

4. Go back to Design view and add another button to your form (Button2).
5. Make the Text property of Button2 say Exit.
6. Double Click on Button2 and type in these words inside the Private Sub:

Copy

Me.Close()

Now, go to the Private Sub for Button1, or, if you want to be sure, go back to
Design view and double-click on Button1. You should see all the stuff we typed
about Mary.

Make it look like this:


Copy

Private Sub Button1_Click(ByVal sender As System.Object, _


ByVal e As System.EventArgs) Handles Button1.Click
Dim intMaryAge As Integer
Dim strMaryName As String
Dim intNewAge As Integer

intMaryAge = 7
strMaryName = "Mary"
MessageBox.Show("Hi, my name is " & strMaryName & _
" and my age is " & intMaryAge)
intNewAge = CInt(TextBox1.Text)
intMaryAge = intMaryAge + intNewAge
Label1.Text = "Mary's new age is " & intMaryAge

End Sub

Type a number into TextBox1. Try to run the program (blue arrow), and if
something goes wrong, click the Exit button (Button2, the one you made work by
using Me.Close ()).

You should see the original message box show up and claim Mary's age is 7 (at that point
in the program, the variable intMaryAge has been assigned a value of 7). But in Label1,
where the math results are shown, intMaryAge has been changed to reflect the addition
of whatever value you typed into the Text box.

You've converted the value inside the Text property of TextBox1 to an Integer CInt is a
function already built into Visual Basic that will do this for you. This conversion ensures
that the sub procedure treats any numbers typed into the text box as numbers and not
strings. By the end of the sub procedure, you've displayed intMaryAge as its new value
in Label1. If you want a shorter way to write out your addition statement, you can write
intMaryAge += intNewAge and you get the same thing as intMaryAge = intMaryAge
+ intNewAge.

This program also sneaks in a few of the already-made elements of a Windows form that
you will find useful in other places. For example, getting a Message Box to show up for
the user, putting words into the Message Box, handling input from a Text Box, and
getting information to show up on a Label. All this code is very straightforward, and
meant to be so to allow you to get a handle on what changes went through your code.

Imports System

Class [class]
Shared Sub [shared](ByVal [boolean] As Boolean)
If [boolean] Then
Console.WriteLine("true")
Else
Console.WriteLine("false")
End If
End Sub
End Class

Module [module]
Sub Main()
[class].[shared](True)
End Sub
End Module

nstead putting direct text into your variables, such as "Bill" or "Gates", you can get text
from a textbox and put that straight into your variables. We'll see how that's done now.
First, do this:

 Add a new textbox to your form


 With the textbox selected, locate the Name property in the Properties area:

The current value of the Name property is Textbox2. This is not terribly descriptive.
Delete this name and enter txtLastName. Scroll down and locate the Text property.
Delete the default text, and just leave it blank.

Click on your first textbox to select it. Change the Name property from Textbox1 to
txtFirstName.

What we've done is to give the two textboxes more descriptive names. This will help us
to remember what is meant to go in them.
Unfortunately, if you view your code (click the Form1.vb tab at the top, or press F7 on
your keyboard), you'll see that the blue wiggly lines have returned:

If you hold your cursor of the Textbox1, you'll see this:

It's displaying this message because you changed the name of your Textbox1. You now
no longer have a textbox with this name. In the code above, change Textbox1 into
txtFirstName and the wiggly lines will go away. (Change it in your Button1 code as
well.) Your code should now read:

txtFirstName.Text = FullName

Run your programme again. If you see any error messages, stop the programme and look
for the wiggly lines in your code.

We'll now change our code slightly, and make use of the second textbox. You'll see how
to get at the text that a user enters.

Locate these two lines of code

FirstName = "Bill"
LastName = "Gates"

Change them to this

FirstName = txtFirstName.Text
LastName = txtLastName.Text

Remember: the equals ( = ) sign assigns things: Whatever is on the right of the equals
sign gets assigned to whatever is on the left. What we're doing now is assigning the text
from the textboxes directly into the two variables.

Amend your code slightly so that the Whole Name is now displayed in a message box.
Your code should now be this:

Dim FirstName As String


Dim LastName As String
Dim WholeName As String
FirstName = txtFirstName.Text
LastName = txtLastName.Text

WholeName = FirstName & " " & LastName

MsgBox(WholeName)

Run your programme. Enter "Bill" in the first textbox, and "Gates" in the second textbox.
Then click your "String Test" button. You should get this:

Before we changed the code, we were putting a person's name straight in to the variable
FirstName

FirstName = "Bill"

But what we really want to do is get a person's name directly from the textbox. This will
make life a whole lot easier for us. After all, not everybody is called Bill Gates! In the
line FirstName = txtFirstName.Text that is what we're doing - getting the name directly
from the textbox. What we're saying to Visual Basic is this

 Look for a Textbox that has the Name txtFirstName


 Locate the Text property of the Textbox that has the Name txtFirstName
 Read whatever this Text property is
 Put this Text property into the variable FirstName

And that's all there is too reading values from a textbox - just access its Text property,
and then pop it into a variable.
No more reading these lessons online - get the eBook here!

Exercise
 Add a third textbox to your form
 Change its Name property to txtWholeName
 Add labels to your form identifying each textbox (A quick way to add more labels
is to use the toolbox to add one label. Then right click on that label. Choose Copy
from the menu. Right click on the form, and select Paste.)
 Write code so that when the "String Test" button is clicked, the whole of the
persons name is displayed in your new textbox

When you complete this exercise, your form should look like this one (we've deleted the
first button and its code, but you don't have to):

In the next part, we'll explore some more variable types you can use.

What it all means is there are options you can use with your message box. The
first one is "text As String". The text in question is the text that will appear for
your message - the message itself, in other words. The next one is "caption As
String". This sets the white caption at the top of the message box.

So if your message box function was this:


MessageBox.Show("This menu will Undo an Operation", "Undo")

You would get this message box popping up:

Each option for your message box is separated by a comma. If you type a
comma after the "Undo" in the code above, you'll get another pop-up menu. On
this menu, you can specify which buttons you want on your message box:

If you only need the OK button on your message boxes, then double click this
item, then type a comma. Yet another pop-up menu will appear. On this menu,
you can specify the symbol that appear in the message box:

It's up to you which symbol you choose. Experiment with all of them and see
what they look like. In the image below, we've gone for the Information symbol:

Compare the message box above with the one we had earlier:
In a real programme, you should use the new MessageBox.Show( ) function,
rather than the MsgBox() we used (and will again because it saves on typing and
space!)

In the next section, we'll move on to Conditional Logic

You might also like