You are on page 1of 28

Visual Basic .

NET for Beginners

First Things First

What is this course about?


This course is about programming using the Visual Basic language. It's written for complete
beginners so I have tried to explain everything. If you're an experienced programmer, you'll
probably discover that you already know a lot of what is in this course. But if you're
completely new to programming, this course is what you're looking for.
If you're looking for a more complete course that covers more advanced topics, I
recommend the Visual Basic .NET 2008 Express - A "From the Ground Up" Tutorial.
Lesson One Contents
Visual Basic .NET for Beginners
First Things First
What is this course about?
Programming
Visual Studio
Getting Ready to Program
Get VB.NET Express

Get comfortable with Visual Studio


Writing Your First Program
The Skip Days program
Program goals:
Understanding what a "Windows Forms Application" is
Learning how to use Visual Studio
Using a Visual Basic control
Understanding "event oriented" programming
Writing the program code

The version of Visual Basic we'll use is called Visual Basic 2008 Express Edition and it is totally
free and the course is totally free too. You can download all of the software you will need
from Microsoft. We'll do that soon.

Programming
In this first lesson, we write a program that you might find useful for displaying dates on a
calendar. But the goal of the course is to understand how to write a program in Visual Basic.
The program we will write in this first lesson probably won't do exactly what anyone wants.
But if you work through it and understand it, then you'll be able to change it and make it do
what you want.
Basic was the first personal computer programming language. Visual Basic is the most recent
version of Basic - and I believe - the best programming language available for most people.
It was invented to help make Microsoft Windows successful. The last twenty years of
Windows success tells us that worked great!
Before a computer can do anything, a program has to tell it what to do. When Microsoft
Windows was first invented, writing a program that would work with Windows was really
hard because there is so much happening "behind the scenes" in Windows and early
Windows programs had to tell the computer how to do everything.
For example, the programmer had to write code to display a "window" on a computer in the
first place. Visual Basic does most of that hard work automatically. With VB.NET, you only
have to think about the parts of the program that does what your application needs.
Example:
The program we will write will display a calendar and changes date displayed. If you had to
write all the code to actually display the calendar and calculate the dates yourself, you might
never finish the job. With VB.NET, all you do is tell Visual Basic what date to display.

Programming
You may have heard that computers only understand 1's and 0's - binary numbers - and
that's true. How does a computer calculate spreadsheets, show web pages, run games, and
all the other things they do with just 1's and 0's?
Look at the screen in front of you with a magnifying glass. You'll see that it's really just a
series of tiny dots. Each dot is either light or dark. (Color is three dots - red, blue, or green in one place.) So whether a dot is light or dark can be controlled by just a 1 or a 0. When the
"processor" chip in a computer sees the right instruction, it will change the binary numbers
that control the image displayed on the screen. (This explanation boils a lot of technology
down to a really simple version, but it's still essentially correct.)
Thinking about the tiny dots on your screen again, you might be able to imagine just how
busy your computer must be trying to compute what each one should be. Remember that it
also has to work with pages coming in from the web, calculating how to display the cards in
your game of solitaire, and the thousands of other things all at the same time. Now think
about the job of sending the right commands to do all these things to the processor chip.
Sounds impossible, doesn't it? That it all works billions and billions of times every day all
over the world continues to amaze me.
Visual Basic is a program that creates other programs. Think of a program as being like a
factory that makes things. Some factories make things you use, like a new car. Other
factories make things that factories use, like a factory that makes welding machines that
make new cars. Visual Basic, and all programming languages, are like that second kind of
factory.

Visual Studio
Visual Basic is the name of the programming language that we'll use. Like other languages
(English, German, Swahili), it's really just a set of rules. Visual Studio is a "development
environment" that uses Visual Basic. So Visual Studio is actually the "program" I described
earlier. In practice, the two terms are often used to mean the same thing and that's the way
I use them.
Microsoft sells several different versions of Visual Studio with different capabilities. But all
versions are called Visual Studio .NET because they all use a powerful set of programs called
the .NET Framework to do much of the hard work. All versions of Windows sold today
include the .NET Framework and you can add it to older versions such as Windows XP.
I said that we would use Visual Basic 2008 Express Edition. That's Microsoft's "official" name
for it but it's really just a version of Visual Studio. (Microsoft switches the names around
too.) If you buy one of their more expensive versions of Visual Studio, you'll see that it looks
almost exactly the same as the version we will use.

Get VB.NET Express


Because Microsoft is constantly changing their site, I'm not going to give you an exact web
address to download VB.NET Express. Just go to their home page, Microsoft.com, and search
for "Visual Basic Express". That should take you right to the page you need. You'll see a lot of
other software that you can download and install too. Patience! If you're just getting started,
my advice is to do just one thing first.
Follow Microsoft's instructions on their site to download and install VB.NET Express. The
only cost is that you have to register with them to do it. You might also want to check their
System Requirements. If you're running Windows XP or later, you should be just fine.

Get comfortable with Visual Studio


With VB.NET installed, start the program by clicking the "Visual Basic 2008 Express" icon in
your All Programs list.
You've started writing your first program.
Here's the first page you see (for VB.NET Express 2008). The Start Page is just information
from Microsoft. You can close it immediately if you like.

To start programming, click the File Menu and select "New Project..." then "Windows Forms
Application" then type over "WindowsApplication1" in the Name textbox and use a new name,
"SkipDays".

VB.NET will respond by creating several files and folders on your computer and displaying a new
"Windows Form" in a window like this:

To better understand what's happening, stop right here and run your new program. You might think
that you haven't written one yet, but you have. Or rather, VB.NET has. The files and folders that are
created automatically by VB.NET will run before you have written even one line of code.

The program only includes the blank form that you can see already in the Design window,
but it will run.
To run the program in "Debug" mode, just press the F5 key on your computer. You can
move, resize, and close the form just like any other window so it is a real program. This
shows the form running:

When you exit the form (use the "X" in the control menu just like you would for any other window),
you should be back in VB.NET again and ready to start writing the code that will make your program
work.

The Skip Days program


There are times when you need to know exactly what day is 30 days, 90 days or 180 days
into the future. When you do, you have to find a calendar and start counting days. It's easy
to make a mistake. The program we'll write will show you the exact day with one click of a
button.

What are the goals?

Understanding what a "Windows Forms Application" is


Learning how to use Visual Studio

Using a Visual Basic control


Understanding "event oriented" programming
Writing the program code

Understanding what a "Windows Forms Application" is

You can write any kind of program using VB.NET. Some programs are designed to work on
the web. Others work "behind the scenes" and you don't see them at all.
The next lesson discusses how to create these other types of programs. A "Windows Forms
Application" is the kind of program that is most familiar to you. The program works using a
window on a screen and familiar "objects" like buttons and textboxes.
VB.NET programs consist of "programming statements" that you can understand. Here are
two statements as an example:
Dim myVariable As String
myVariable = "A String"

After you have written your part of the program, VB.NET merges your part and the part
created automatically and then "compiles" the program. The word "compile" means that it is
converted from statements you can read into statements that are designed for the
computer to read. As we learned earlier, computers only understand 1's and 0's.
Compiling a program is the first step in turning it into 1's and 0's.
The first statement above reserved a name, "myVariable" so you can save information and
refer to it using that name. There are specific "types" of information that you have to use
when you write a program and "String" is one of them.
Another example is "Decimal". The second statement associates specific information with
the name you reserved. If you code a later statement:
Debug.WriteLine(myVariable)

VB.NET will display A String in the Immediate Window because that's what the name
myVariable refers to.

Learning how to use Visual Studio


The statements in your part of the program are kept in a file that is similar to a file you might
make using Notepad. But VB.NET keeps everything organized for you and you can see it all in
the Solution Explorer part of VB.NET. The illustration you saw earlier shows you where
Solution Explorer is in VB.NET. VB.NET uses the word "Solution" instead of "Program"
because a lot of programs can be combined together into a bigger program and they needed
a different word.
When you write a Windows Forms Application, you will select and customize the visual parts
of your program (the form and things like buttons and textboxes) using the Design window
that you have already seen. These parts of the program are already there for you to use in
the VB.NET Toolbox and they're called "components" or "controls". (A "control" is a
"component" that has a visible interface like a Button. A "component" doesn't have any
visible interface like a Timer.) You can see the Toolbox lurking over on the left side in a
previous illustration. Because you only need the Toolbox when you're adding controls to

your application, it snaps out of the way when you're not using it. If you leave the mouse
pointer over the Toolbox, it will snap out. You can then click and drag controls onto your
form. When you do that, VB.NET adds the programming for the control to your Solution.
(Just a note of history here. This innovation of adding controls to a program by dragging
them from a Toolbox was the thing that made the very first version of Visual Basic such a hit
because, for the first time, programmers didn't have to write the detailed code to create the
controls themselves.)
We're going to jump right in and add all of the controls that our SkipDays program will need
right now. This may be confusing if it's your first time and you may need to do it more than
once to get it right. If you get totally confused, close VB.NET and "Discard" the program and
start over. No problem. You're here to learn.
1. Make sure that the Design Window is displayed
2. Open the Toolbox and find the MonthCalendar control
3. Left-click and drag the MonthCalendar control to the form and release the mouse
button to drop it on the form.
4. Find the Button control. Drag and drop it on the form.
5. Find the GroupBox control. Drag and drop it on the form.
6. Find the RadioButton control. Drag and drop it on the GroupBox control. Drag and
drop two more RadioButton controls onto the GroupBox control.
That's all we'll need. Here's the way my program looks after doing that. Notice that I didn't
bother to arrange the controls neatly (we'll do that next). I just dropped them anywhere on
the form (except for the RadioButton controls - they have to be dropped on the Groupbox).
The MonthCalendar control isn't even fully on the form. Even though you haven't written a
line of code yet, the program still runs. You can click F5 again and see. (I show mine running
in the lower right corner.)

Here's where you put your "designer" hat on. Arrange all the controls so they look good on
the form.
You can move all of the controls around on the form after selecting them (by clicking inside
them). A control is "selected" when you can see the "sizing handles" around the edge. (The
MonthCalendar control only has three. The RadioButton controls only have one.) You can
drag a control when the mouse pointer turns into a four-way arrow. Visual Studio helps by
snappping them in place when they're lined up with other controls or the form. Notice that
when you move the GroupBox control, all of the RadioButton controls move with it. That's
why you dropped them on the GroupBox control. Adjust the RadioButton controls inside the
GroupBox but make sure you don't move them outside or they won't work the way we want
them to.
Here's my form after I arranged all the controls. Yours doesn't have to look just like mine.
You can design it any way you like.

The MonthCalendar control is a fairly complex piece of work, but there are actually controls
that are even more complex in the Toolbox. We'll use this control to learn about another
part of VB.NET: the Properties window.
The individual windows, such as Properties, can be resized and moved around to make them
easier to work with. I dragged the Properties window out into the middle of VB.NET in the
illustration below.

Some of the Properties for the MonthCalendar control are shown, but there are a lot more
that you can scroll down to see.

The Properties, Methods, and Events are what gives a control the power to do what you want done.

Properties include the information about the control, like the Name. Methods are what the
control can do, like ShowToday. Events make parts of your program start running, like
Keypress. We'll go over Methods and Events later.
Read through the Properties for the MonthCalendar control. (The Design window and the
MonthCalendar control must be selected first.) You'll see that there is a lot of stuff there. But
we don't have to change any of it to complete our program. You might notice that some of
the properties, like Location, change automatically as you design the form (move and resize
the controls). You can change them in the Properties window and it will change in the Design
Window automatically too.
Here are all the properties that you should change for the rest of the controls. You can
change the value of the property by clicking in the second column of the properties window
beside the property that you want to change. The RadioButton1 Checked property is
changed to True to make that the default.
Form
Name: SkipDays
Text: Skip Days
Button
Name: skipAhead
Text: Skip Ahead
GroupBox
Name: daysToSkip

Text: Days To Skip


RadioButton1
Name: skip30
Text: 30 Days
Checked: True
RadioButton2
Name: skip90
Text: 90 Days
RadioButton3
Name: skip180
Text: 180 Days

Those are the only properties you have to change! Most properties can be changed at
"runtime" (when your program is running) too. For example, you can change the Text
property at runtime.
skip90.Text = "Skip Town"

Both Methods (explained later) and Properties are identified in program code using the
"dot" notation. This statement changes the Text property for the control named skip90.

Understanding "event oriented" programming


Most programs today are "event oriented". This means that after the program starts
running, it normally just waits around for something to happen that it "knows" how to
respond to. For example, clicking a button. A program "knows" how to respond to an event
when there is an "event subroutine" for that event in VB.NET.
A subroutine is one of the fundamental units of program code in a VB.NET program. Here's a
Click event subroutine for a Button control named Button1:
Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
' ... program code goes here ...
End Sub

The underscore characters at the ends of the lines are "continuation characters". They allow
you to break one long statement into several shorter lines. I have to use them here because
space is limited on the web page.
All of the code above is entered for you by VB.NET. All you have to do is double click on the
control in the Design window and the Code window will open automatically with the default
event for that control already coded. (Click is the default event for a Button control.) The
automatically entered code can be very useful (other articles here explain how to use it) but
when you're just getting started, you can ignore it all and just enter your program code.

You can see all of the available events for a control in the Code window. Let's look at the
events you can use for a Button control. This isn't necessary to write the rest of the program,
but it's a good time to pause and see what's available.
1. Click the View Code icon in the Solution Explorer toolbar.
(It's just to the left of the View Designer icon that was shown in a previous
illustration.)
There are two "dropdown textboxes" above the code window. The one on the left is
called the "Class Name" box and the one on the right is the "Method Name" box.
2. Click the down arrow on the right side of the Class Name box to display the selection
list and then select skipAhead (the name we gave the Button control) in the
dropdown that pops open.
3. Click the down arrow on the right side of the "Method Name" box. A long list of
events will pop up. In general, you can write your own code for any of these events
and make your program do just about whatever you can think of.

If you then click any of the events in the Method Name box, VB.NET will automatically enter all the
event subroutine code you need for that event into the code window too. This is an alternative to
double-clicking the control in design view as described a little earlier. Double-clicking the control in
design view will only work for the default event.

Up to this point, we still have not written any code for the SkipDays program. You may have
accidentally created event subroutines while you were looking around. If you did, just
highlight and delete them in the code window. (Leave the Public Class SkipDays - End Class
statements there, however.) But now it's time to write some of our own code.

Using the Class Name and Method Name dropdown textboxes as described above, select the
skipAhead control and the Click Event again so VB.NET will create the event subroutine for
the Click event again.
Then enter this code in the subroutine:
If skip30.Checked = True Then
MonthCalendar1.SetSelectionRange( _
Today.AddDays(30), _
Today.AddDays(30))
ElseIf skip90.Checked = True Then
MonthCalendar1.SetSelectionRange( _
Today.AddDays(90), _
Today.AddDays(90))
ElseIf skip180.Checked = True Then
MonthCalendar1.SetSelectionRange( _
Today.AddDays(180), _
Today.AddDays(180))
End If

This is the only code we need to write to make the whole program work because the really
difficult coding is part of the control. The code we do have to write is an example of
"conditional statements". One of the main things you have to do as a programmer is decide
how to control the program with conditions. In this case, we select a date on the
MonthCalendar control based on which RadioButton is clicked.
But it's not quite that easy. You might be asking questions like, "Why is 'Today.AddDays'
there twice?" and "Why did you use the 'SetSelectionRange' method?" These are all good
questions. Let me ask you one: "How do you say, "Today is the first day of the rest of your
life." in the Swahili language?"
You say you don't speak Swahili? Neither do I. But I do speak Visual Basic because I've
studied it for years. This is an example of what you have to learn to "speak" it.
But there is a lot of help right at your fingertips. One of the first sources of help is
"Intellisense". VB.NET will suggest all of the members that you might be looking for as soon
as you enter the "dot" in the "dotted notation" above.
To see how this works, enter
skipAhead.

into the code just before the End Sub statement. As soon as you enter the "dot" a window
should open that displays the members - methods and properties - available for a Button
control. You'll see the Name and Text properties there, for example. You'll notice that a lot
of other "pop-up" help is available as you move your mouse around the code window too.

The Help system for VB.NET is exceptionally complete as well. It's amazing, especially when
you think about how much you paid for it. (Nothing!)
And ... of course ... I recommend that you check out the articles and tips available at About
Visual Basic, too!
At this point, your first program is complete, but it's far from perfect. For your first
homework assignment, change the code to go to your birthday for any year!

Lesson 2 of an About Visual Basic Tutorial


This course is about programming using the Visual Basic language. It's written for complete
beginners so I have tried to explain everything. If you're an experienced programmer, you'll
probably discover that you already know a lot of what is in this course. But if you're
completely new to programming, this course is what you're looking for.
The version of Visual Basic we'll use is called Visual Basic 2008 Express Edition and it is totally
free and the course is totally free too.
You can download all of the software you will need from Microsoft.
If you're looking for a more complete course that covers more advanced topics, I
recommend the Visual Basic .NET 2008 Express - A "From the Ground Up" Tutorial.
Lesson Two Content
Visual Basic .NET for Beginners
Visual Studio in More Depth
The Visual Studio GUI

The windows and


panes of Visual Studio
Clicking with Visual Studio
The Visual Studio Project Types
Everything is an object
Properties and the Design Window
What's in a Windows Forms Application?
The Form file and the Form object
Project properties
A Label that remembers your birthday

If you're looking for the first Lesson to start at the beginning of this tutorial, click here:
Lesson One.

Visual Studio in More Depth


Lesson 1 was a rapid fire introduction to VB.NET where we built a complete application that
displayed a date on a calendar. It didn't involve actually writing very much code, but you had
to use a lot of features of VB.NET and Visual Studio to get the job done. In this lesson, we're
going to concentrate on explaining all about those features and more. But since
programming is writing code, at the end of the lesson, we'll add a feature to the program
from Lesson 1 to display a message on a particular date. You could use it to remember your
birthday!

The Visual Studio GUI


GUI is Graphical User Interface.
In the decades that I've been programming, acronyms have always been a big part of it so
you might as well get used to using and knowing a lot of them. Often, there are several that
can mean the same thing. For example, I could have written "Visual Studio IDE" for
Integrated Development Environment.
Ads

Programming Tutorial
Basic Java Tutorial
Excel for Beginners
Microsoft Excel Tutorial
Visual Basic 6.0

Both of these mean the windows and tools that are all in the bag called "Visual Studio .NET".
We used a lot of them last time and we'll use more now.
As I noted last time, for our purposes here, Visual Studio .NET and Visual Basic .NET are
really the same thing. The versions of Visual Studio that you have to buy include other
languages in addition to Visual Basic. But Visual Basic .NET Express only supports Visual
Basic. It is actually a remarkably complete version of Visual Studio, however. If you decide to
buy a copy of Visual Studio, you'll find that the Visual Basic part of it is virtually identical.

The windows and panes of Visual Studio


Visual Studio is mainly a series of different windows that all work together. The main
windows are:

The Design and Code windows


The Solution Explorer Window
The Properties Window
The Debug windows, Immediate and Output
The Toolbox window

You might find some sources that call these "panes" instead of "windows". We'll just call
them "windows" here.
All of these windows except the Design and Code windows can be set in one of five different
"operational modes":

Floating
Dockable
Tabbed Document
AutoHide
Hide

As you advance in your programming skills, you'll discover that Visual Studio offers much
more. I still find tools in Visual Studio that I didn't know about.
The illustration below shows all of them as "Floating". You can click and drag them around
the screen or close them. Right click on the title bar to change from one mode to another.

In the Floating mode, you can "dock" these windows against any side of the screen or even stack
several on one side (where they become "tabbed documents" on that side). You might have had
trouble with docking in earlier software (for example, the Windows Taskbar can be "docked" too)
because it was so 'touchy' and hard to use. Visual Studio gives you an onscreen aid called "guide
diamonds" that make it simple.

My advice is to stick to the startup defaults for now. But don't be concerned about
experimenting to see what the different modes look like. You can always straighten out your
experiments by selecting "Reset Window Layout" from the Window menu.
One final tip ... AutoHide can be a little confusing to beginners too. This makes the window
collapse into the margin and pop out automatically when the mouse pointer hovers over the
tab for the window in the margin. Beginners sometimes get into a "whack a mole" mode
with these windows in AutoHide mode because they can't figure out how to keep the darn
window visible while they select something in it. The solution is to either "pin" the window
open (look for a pushpin in the title bar) or double click the tab in the margin. Either will
keep the window open while you select something.
Instructions for using software often just say, "click the mouse" but that too can be
confusing. Left-clicking gives you a completely different result than right-clicking; and
double-clicking is usually quite different than single-clicking.
There is a logic to left-clicking versus right-clicking (and this is true of nearly all Windows
software). Left-clicking always "does" something. You click a button to do whatever the
button does.
You click a down arrow to move something down or open a drop-down box. The point is,
something happens as a result. But right-clicking usually simply opens a "context menu" that
gives you more choices. The choices you get depend on the "context" where you right-click.
If you right-click inside a form, a different menu of choices will be displayed than if you click
in the Properties window ...

and the same for all the other windows. So if you want to do something with a window on
the screen, but you don't see that choice available anywhere, try right-clicking. What you
want to do may be in the context menu.
I just wrote that left-clicking "does something". When you single-click the left button, you
usually "select" whatever the mouse pointer is over. When you double-click things, you may
be asking Visual Studio to do something more involved. In the case of components in the
design window, you will open the code window and create the default event code for that
component. So avoid double-clicking unless that's what you want to do.

The Visual Studio Project Types


When you select "New Project", you are given a default selection of five different types of
projects that you can create with Visual Basic Express 2008:
1.
2.
3.
4.
5.

Windows Forms Application


Class Library
WPF Application
WPF Browser Application
Console Application

There's also a "Search Online Templates" icon, but that has never resulted in anything in my
experience. Sometimes, Microsoft creates blind alleys.
This tutorial primarily uses Windows Forms and Lesson 1 described them
Keep reading and you'll know a lot more about them.
WPF is "Windows Presentation Framework" and it's a completely new way to "present"
systems to the people who use them. Microsoft uses the word "presentation" because they
have opened it up to much more than just images on screens. Microsoft defines it this way:
"Windows Presentation Foundation (WPF) provides developers with a unified programming
model for building rich Windows smart client user experiences that incorporate UI, media,
and documents."
It's a huge and very new technology and most people expect WPF to replace Windows Forms
at some point. But it might take quite a few years. You can find articles explaining WPF here
at About Visual Basic. Just use our integrated search facility to search for WPF at this site.
A Class Library is a collection of software "objects" that can be used to do things inside a
program. The .NET Framework itself is just a mammoth collection of class libraries. You can
code your own as you advance in your skill as a programmer.
A Console Application is a program that does not have a GUI. (Both Windows Forms and WPF
create a GUI. The the first part of this lesson for an explanation of GUI.) To use a Console
Application, you have to use the Commmand Prompt "DOS" emulation built into Windows.
People sometimes use them to create "lightweight" (low memory and CPU use) applications

that can run even when Windows can't. For example, a lot of communications (TCPIP and
FTP) and other system utility programs are written as Console Applications.
Keep reading and you'll know a lot more about them.
WPF is "Windows Presentation Framework" and it's a completely new way to "present"
systems to the people who use them. Microsoft uses the word "presentation" because they
have opened it up to much more than just images on screens. Microsoft defines it this way:
"Windows Presentation Foundation (WPF) provides developers with a unified programming
model for building rich Windows smart client user experiences that incorporate UI, media,
and documents."
It's a huge and very new technology and most people expect WPF to replace Windows Forms
at some point. But it might take quite a few years. You can find articles explaining WPF here
at About Visual Basic. Just use our integrated search facility to search for WPF at this site.
A Class Library is a collection of software "objects" that can be used to do things inside a
program. The .NET Framework itself is just a mammoth collection of class libraries. You can
code your own as you advance in your skill as a programmer.
A Console Application is a program that does not have a GUI. (Both Windows Forms and WPF
create a GUI. The the first part of this lesson for an explanation of GUI.) To use a Console
Application, you have to use the Commmand Prompt "DOS" emulation built into Windows.
People sometimes use them to create "lightweight" (low memory and CPU use) applications
that can run even when Windows can't. For example, a lot of communications (TCPIP and
FTP) and other system utility programs are written as Console Applications.

Everything is an object
Programming today is almost exclusively "object oriented" (OOP). A very fast definition of a
software object is software that includes data (properties) and can do things (methods). A
button is an object because it includes data like the size, name, and text on the button and
when you click it, a program starts running. You can study objects in depth in books written
about the concept, but this simple understanding will do just fine to get started. In general,
everything in .NET programming is an object. VB.NET includes a tool that is used exclusively
to find information about objects called the "Object Browser". It's usually on the Visual
Studio toolbar but you can also find it under the View menu.
The Object browser will give you information about all of the objects in the .NET Framework.
All of the methods (what an object can do), properties (data in an object) as well as the
events (subroutines that can be started by changes that the object detects) are listed there
in one place. You can use this tool to learn about unfamiliar objects and look for ways that
objects are used. Just for practice, you might want to search for MonthCalendar, the
component we learned about in the previous lesson.

Properties and the Design Window

In Lesson 1, we changed all of the Properties of the control objects that we added to our
project before doing anything else. That's a good habit to learn. Some of the properties of
objects can be difficult to change after a lot of code has been written, so getting their values
correct to start with is important. The Properties window only shows the properties of
objects when the Design window (not the Code window) is selected because you can't select
a specific object in the Code window and Visual Studio can't determine what control object
to display properties for.
The Name is an example of an object property that should always be changed as soon as the
object is added. The Visual Studio Property window sorts Name to the top of the Properties
window by adding parenthesis around it because the character "(" sorts before alpha
characters. You should change the Text property immediately as well.
When you use the Windows Forms Applicaton template for a new project, Visual Studio adds
a whole hierarchy of files to your project to support the forms. Some of the files are hidden
because you will never change them. But knowing about them will help you understand how
Visual Basic projects work. And sometimes you might need to look a what's in them to find
difficult bugs. The files that are added for a Windows Forms Application are different than
the files for other templates.
That's what makes the templates different.
You can see all of the files, including the hidden files, by clicking the Show All Files icon in the
Solution Explorer window. The illustration below shows all of the default files for a project,
as well as some things (such as References) that aren't files.

Just to complete your understanding about how things work, the files and code for the project can be
seen outside of Visual Studio (after the project is saved). This illustration shows The files in Windows
Explorer and the default code for the Load event of a form in Notepad. You can see the code in
Notepad is the same as the code in the Code Window of Visual Studio. However .... It's always a bad
idea to change your program outside Visual Studio. Visual Studio keeps everything coordinated. If
you change the program outside Visual Studio, for example in Notepad as shown here, you will
amost always get things totally messed up.

You will need to know where things are is to do things like copying the executable (the end result of
programming and compiling a VB.NET program) to another location; or understand how to code the
file path to other files that you might reference in your project, such as a data file that has to be
opened and read.
The executable for your project is in the Debug or Release Folder for the project. An executable is a
".EXE" file (a program a user can run) or a ".DLL" file (a component that can be used by other
components or programs) and you can "Run" it directly from Windows. The illustration shows a
default WindowsApplication1 .EXE program being run in Windows.

The Form file and the Form object


You'll notice that if you add a new component to your project, such as the MonthCalendar
that we added in Lesson 1, no new files are added to the project. The reason is that the code
for these components is stored inside the file for the form. (Actually, most of the code is in
the .NET Framework, but some local code is created to make the object unique for your
project.) So, in addition to holding all of the code for the form object in your program, the
form file does double duty by holding the code for a lot of the rest of your project. That's
why the form "file" has a different properties window than the form "object". The
illustration below shows the different properties

You can select the form file properties by selecting the form in the Solution Explorer
window. Visual Studio does it's best to change the object Name to match the file Name, but
if you change the Name in both places, is won't be the same. This is another reason to pick a
fundamental property values, like Name, as soon as you add them and then avoid changing
them. Inside the code for the form, VB.NET insists that you refer to the form object as "Me"
instead of using the actual name:
Me.Location = (New System.Drawing.Point(400, 400))

(Location is a property of the form. Point is an object that we haven't learned about yet.
System.Drawing is the namespace where the Point object can be found in the .NET

Framework. New is a method, called the "constructor" method, of objects that is used to
create a copy of the object for your program.)

Project properties
Just like the Form has it's own properties, so does the project. In fact, the project properties
are extensive and can be quite technical. You can display a window for the project properties
by selecting it in the Project menu or by right-clicking the Project in Solution Explorer and
selecting Properties from the context menu.
The illustration below shows that there are eleven different tabs in the Project Properties
window. That's a lot of properties!!

A complete description of project properties is a more advanced topic.

Programmers write code. So we still need to write some for this lesson.
In Lesson 1, we wrote a very simple program that would display a date 30, 90, or 180 days
from the current date. I suggested that you might think about how to change the program to
go to your birthday at the very end. In this lesson, we'll start with the program that you
coded in the first lesson and add a label that pops up if the date you go to happens to be
your birthday.

Computers are clever, but they can't figure out what your birthday is unless you tell them.
The first thing to add to the code is a way to tell the computer what your birthday is. In this
particular case, I'm using a new statement: Const. This is a way of telling VB.NET about a
value that will never change; in other words, a "constant".
We'll see a different constant supplied by Visual Basic just a little later. I add this Const to the
SkipDays class outside all of the subroutines at the top of the code. This makes the entire
SkipDays class the "scope" of the Const. In other words, all of the procedures in the class will
be able to use the same Const. The Const is coded like this:
Private Const myBDay As Date _
= #12/7/1941#

Notice that this is of type Date and that when a date is assigned to the Const, it's enclosed in
the "#" symbols. Just as strings are enclosed in double quote marks in Visual Basic, dates are
enclosed in pound symbols. That way, Visual Basic knows how to process them correctly.
Just one note on program design ...
Coding something like this as a Const in the source code of the program is actually very bad
design. If anyone else uses the program, the source code will have to be changed to a new
date and the program will have to be recompiled. That's terrible design!! A much better way
to do this would be to make this a parameter that is passed into the program from a
configuration file.
And more advanced courses on this site show you how to do that.) I'm doing it this way to
demonstrate how VB.NET works, not as an example of great programming.
Our modifications to the program will cause a big banner display to pop up the first time the
program calculates a date on the same month and day as the date in myBDay. The banner
can be dismissed again with a button.
To show the banner, add a Label component and then add a button to dismiss the banner. I
named the banner label BDayGreet and the button BDayOK. I changed the background of
the label to the color Fuchsia and I also changed the font Name and Size. And - very
important - I set the Visible property to False so they will only be displayed when I want
them to be.
But I didn't change the Text to my chosen greeting: "Happy Birthday To You!!". Why?
Because there is no way to make the greeting appear on multiple lines by changing it in the
Properties Window. If I had put it in the Text property using the Properties Window, it would
all be on one long line and I want it to be on three lines so the banner is in a square that will
cover the face of the MonthCalendar control.
But there is a way. This falls under the heading of programming tips and tricks. You'll learn
lots of these as you gain skill as a programmer.

One of the events that you can use is the form Load event. This is a good way to do what is
called "initialization" ... setting things up for the program. In the form Load event subroutine,
I added the statement:
BDayGreet.Text = _
"Happy" & vbCrLf & _
"Birthday" & vbCrLf & _
"To You!!"

vbCrLf is a Visual Basic constant - one of many that you can use in your code. This particular
one is equal to "carriage return line feed" characters. The use of these characters to force a
new line goes back to the earliest days of programming - well before PC's were even
dreamed of - but it's still supported. I use the "concatenation" operator "&" to insert a CrLf
at the end of each line I want to appear and put that in the Text property when the form is
loaded.
To control the display of the banner, I add this code after the new date is calculated:
If _
MonthCalendar1.SelectionRange.Start.Day = _
myBDay.Day And _
MonthCalendar1.SelectionRange.Start.Month = _
myBDay.Month And _
DidItAlready = False Then
BDayGreet.Visible = True
BDayOK.Visible = True
End If

(Just a reminder ... I use a lot of continuation characters - underscores - in my example code
to keep the lines short so they will fit on the web page here. You don't have to use them if
you copy this code.)
In words, the code above states that if the myBDay Day and Month is equal to the one
calculated, make the label and button visible. And change this other variable "DidItAlready"
to false. What's that all about?
Once the banner is dismissed (we'll code that next), I don't want this code to pop it up again.
So I use this variable to make sure it only happens once. The variable also has to be declared
in the same place that the Const was declared so it will also have class level scope:
Private DidItAlready As Boolean = False

The last thing we have to code is the Click event subroutine for the button to dismiss the
banner:
Private Sub BDayOK_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles BDayOK.Click
BDayGreet.Visible = False
BDayOK.Visible = False

DidItAlready = True
End Sub

The end result looks like this when you hit your birthday:

Here's a download for the updated program in case you want to see exactly how I did it.
Download the code.

You might also like