Your Federal Quarterly Tax Payments are due April 15th Get Help Now >>

Visual Basic .NET - Sample Pages by tcu11291

VIEWS: 10 PAGES: 13

									                                     Visual Basic .NET – Contents

INTRODUCTION............................................................................................................7
   WHAT YOU NEED TO DO THE COURSE ..............................................................................7
   THE FREE VISUAL BASIC EXPRESS EDITION ...................................................................7
   ADDITIONAL FILES ..........................................................................................................8
GETTING STARTED .....................................................................................................9
THE TOOLBOX ............................................................................................................13
   ADDING A TOOL (CONTROL) TO YOUR FORM ................................................................14
PROPERTIES ................................................................................................................18
SAVING YOUR WORK ...............................................................................................25
ADDING A SPLASH OF COLOUR ............................................................................27
REVIEW NUMBER 1 ...................................................................................................31
GETTING OUR HANDS DIRTY ................................................................................32
VARIABLES ..................................................................................................................33
   ADDING A BUTTON TO THE FORM .................................................................................35
   STRING VARIABLES .......................................................................................................43
   ASSIGNING VALUES FROM TEXTBOXES TO YOUR VARIABLES .......................................47
   MORE ABOUT VARIABLES .............................................................................................51
   USING VARIABLES ........................................................................................................55
A CALCULATOR PROJECT......................................................................................60
   DESIGNING THE FORM ...................................................................................................60
   THE CODE .....................................................................................................................62
   THE 0 TO 9 BUTTONS ....................................................................................................63
   THE PLUS BUTTON ........................................................................................................64
REVIEW NUMBER 2 ...................................................................................................68
CONDITIONAL LOGIC ..............................................................................................69
   IF STATEMENTS .............................................................................................................69
   SELECT CASE ................................................................................................................72
COMBO BOXES............................................................................................................76
REVIEW NUMBER 3 ...................................................................................................80
   PART 1 - IF STATEMENTS...............................................................................................80
   MESSAGE BOX CONCATENATION ..................................................................................82
   PART 2 - SELECT CASE STATEMENTS ............................................................................83
LOOPING THE LOOP .................................................................................................84
   WHAT IS A LOOP? .........................................................................................................84
   FOR LOOPS ....................................................................................................................84
   DO LOOPS .....................................................................................................................88
THE TIMES TABLE PROGRAMME ........................................................................91
   THE CODE ......................................................................................................................93
   A DIGRESSION ...............................................................................................................94
MORE CONTROLS - MENUS ....................................................................................99
   CREATING SUB MENUS ...............................................................................................105
   UNDERLINE SHORTCUT ...............................................................................................107
   KEY COMBINATION SHORTCUTS ..................................................................................108
REVIEW FOUR...........................................................................................................111
MORE ABOUT THE MESSAGE BOX ....................................................................112
WRITING THE CODE FOR THE MENU SYSTEM .............................................115
   THE OPEN FILE DIALOGUE BOX ..................................................................................115
   THE INITIAL DIRECTORY .............................................................................................118
   THE TITLE PROPERTY..................................................................................................118
   THE FILTER PROPERTY ................................................................................................119
   SELECTING A FILE .......................................................................................................121
   THE SAVE MENU .........................................................................................................122
   THE EDIT MENU ..........................................................................................................124
   THE COPY MENU ........................................................................................................124
   THE PASTE MENU .......................................................................................................125
   THE CUT MENU...........................................................................................................127
   THE UNDO MENU ........................................................................................................127
   THE VIEW MENU.........................................................................................................128
   THE VIEW TEXTBOXES MENU ITEM .............................................................................128
   THE VIEW IMAGES MENU ITEM ...................................................................................130
   INSERT AN IMAGE........................................................................................................132
CHECK BOXES AND OPTION BUTTONS ............................................................134
   CHECK BOXES .............................................................................................................134
RADIO BUTTONS ......................................................................................................141
REVIEW NUMBER 5 .................................................................................................144
ERROR HANDLING AND DEBUGGING...............................................................149
ERROR HANDLING AND DEBUGGING...............................................................149
   TYPES OF ERROR .........................................................................................................149
   DESIGN TIME ERRORS .................................................................................................149
   RUNTIME ERRORS ........................................................................................................151
   TRY … CATCH ............................................................................................................153
   LOGIC ERRORS ............................................................................................................157
   BREAKPOINTS .............................................................................................................158
HURRAY FOR ARRAYS!..........................................................................................162
   WHAT IS AN ARRAY?...................................................................................................162
   ASSIGNING VALUES TO AN ARRAY...............................................................................167
   ARRAYS WHERE BOUNDARIES ARE NOT KNOWN .........................................................169
STRING MANIPULATION .......................................................................................172
   MANIPULATING DATA FROM A TEXT BOX ...................................................................175
   TRIM ...........................................................................................................................176
   ISNUMERIC( )..............................................................................................................177
   CHAR ..........................................................................................................................178
   INSTR( ) ......................................................................................................................181
   SUBSTRING ..................................................................................................................181
   EQUALS .......................................................................................................................183
   REPLACE .....................................................................................................................183
   INSERT ........................................................................................................................184
   SPLIT AND JOIN ...........................................................................................................184
   JOIN.............................................................................................................................186
REVIEW NUMBER 6 .................................................................................................188
TEXT FILES ................................................................................................................189
   OPENING A TEXT FILE FOR READING ..........................................................................189
   READING LINE BY LINE ...............................................................................................192
   WRITING TO A TEXT FILE ............................................................................................193
   APPENDING TEXT TO A FILE ........................................................................................195
   CREATING A FILE IF IT DOESN’T EXIST .........................................................................196
   COPYING FILES ............................................................................................................197
   MOVING FILES .............................................................................................................198
   DELETING FILES ..........................................................................................................198
REVIEW NUMBER SEVEN ......................................................................................200
FUNCTIONS AND SUBS ...........................................................................................203
   PARAMETERS ..............................................................................................................206
   BYVAL AND BYREF ....................................................................................................209
   FUNCTIONS .................................................................................................................210
   STANDARD MODULES .................................................................................................214
EVENTS........................................................................................................................221
   THE CLICK EVENT.......................................................................................................221
   MOUSEDOWN .............................................................................................................223
   THE KEYDOWN EVENT ...............................................................................................227
   THE FORM LOAD EVENT .............................................................................................230
VB.NET AND CLASSES ............................................................................................232
   OBJECT ORIENTED PROGRAMMING .............................................................................232
   CLASSES AND OBJECTS ...............................................................................................232
   THE NET FRAMEWORK ..............................................................................................233
   NAMESPACES ..............................................................................................................233
   CREATING YOUR OWN CLASSES ..................................................................................235
   CREATING METHODS IN YOUR CLASSES......................................................................237
   CREATING AN OBJECT FROM A CLASS .........................................................................238
   CREATING AND USING METHODS THAT DON’T RETURN A VALUE ................................241
   CREATING YOUR OWN PROPERTIES .............................................................................243
   ADDING A PROPERTY TO A CLASS ...............................................................................243
   USING YOUR NEW PROPERTY ......................................................................................247
REVIEW NUMBER EIGHT ......................................................................................251
VB.NET AND DATABASES ......................................................................................254
   VISUAL BASIC 2005 EXPRESS AND DATABASES – THE EASY WAY ..............................254
THE DATABASE OBJECTS .....................................................................................266
   THE CONNECTION OBJECT ..........................................................................................266
   DATA SETS AND DATA ADAPTERS ..............................................................................270
   STRUCTURED QUERY LANGUAGE ...............................................................................271
   NAVIGATING THROUGH THE DATASET........................................................................276
   MOVE FORWARD ONE RECORD AT A TIME .................................................................280
   MOVE BACK ONE RECORD AT A TIME ........................................................................281
   MOVING TO THE LAST RECORD IN THE DATASET .......................................................282
   MOVING TO THE FIRST RECORD IN THE DATASET.......................................................283
   UPDATING A RECORD ..................................................................................................284
   ADD A NEW RECORD ..................................................................................................286
   DELETING RECORDS ...................................................................................................289
REVIEW NINE ............................................................................................................293
WORKING WITH FORMS .......................................................................................295
   ANCHORING AND DOCKING ........................................................................................295
   DOCKING.....................................................................................................................297
   TOOLBARS ..................................................................................................................298
   CODING FOR YOUR TOOLBAR BUTTONS ......................................................................303
   CREATING OTHER FORMS ............................................................................................303
   MODAL AND NONE MODAL FORMS ............................................................................305
   GETTING AT VALUES ON OTHER FORMS ......................................................................307
REVIEW TEN..............................................................................................................312
   HELP WITH THE CODING..............................................................................................314
FINAL WORD .............................................................................................................321
                               Getting Started


Launch your Visual Basic .NET or Visual Studio software. When the software first
loads, you’ll see a screen something like this one:




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. At the moment, the area labelled “Recent Projects” 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.




                                            1
Visual Basic .NET




At the bottom of the Recent Projects area, there are two links: “Open” and “Create”.
To get started, click the “Create” link. (If you can’t see a Create link, click File
from the menu bar. From the File menu, select New Project). When you do, you’ll
see this dialogue box appear:




As a beginner, you’ll normally want the option selected: “Windows Forms
Application”. 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):


                                          2
                                                                        Home and Learn




That's a very daunting piece of software, hey? Well, don't worry. We'll break it down
bit by bit, and pretty soon you'll be zipping your way around it like a pro!

The first thing to concentrate on is that funny grey square in the middle. 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 Debugging
       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:




                                            3
Visual Basic .NET


                        The Form Running as a Programme




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.

So, let's get on and do some designing! Before we can start designing a form, though,
we need some tools. And where are tools kept? In a toolbox!




                                           4
                                                                        Home and Learn



                                The Toolbox


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:




                                           5
Visual Basic .NET




As you can see, there an awful lot of tools to choose from! For this first section, we’ll
only be using the Button, the Textbox and the Label.

If you want to keep the toolbox displayed, click the Pin icon next to the X. To close
the toolbox, simply move your mouse away.




Adding a Tool (Control) to your form
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:




                                            6
                                                                        Home and Learn




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:




                                            7
Visual Basic .NET


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 Debugging
from the menu bar. Or press F5 on your keyboard:




                                           8
                                                                       Home and Learn




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




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?

To do those things, we need to discuss something called a Property.




                                           9

								
To top