Sie sind auf Seite 1von 5

F -X C h a n ge F -X C h a n ge

PD PD
1/5/2010 Visual Basic 6 forms and properties

!
W

W
O

O
N

N
y

y
bu

bu
to

to
k

k
lic

lic
C

C
w

w
m

m
w w
w

w
o

o
.d o .c .d o .c
c u -tr a c k c u -tr a c k

Previous Contents

LESSON 1 - Getting started

Tuesday, January 05, 2010

Defining basic terms


Application

An applic at ion is a c ollect ion of object s that work together to ac c omplish something useful. In VB the applic at ion is
c alled a Project. A Projec t could be a the management of a Video store, the calc ulation of mortgages, a dating servic e
or the Payroll for 1000 employees ...

Object

An objec t is a piec e of software that has propert ies and func tions that c an be manipulated. Whew! You're here so, you
must be somewhat familiar with the Windows environment. A window is an object. It has properties: size, c olor,
position on the screen, etc . (The purists among you may want t o talk about a class rather t han an object but, at this
point we just want to keep it simple, and the underlying conc ept is t he same). The window has func tions, also c alled
methods, that c an be manipulated: change the size, move it around, open it and close it. You do not have to write
c ode to resize a window - you just c lick and drag. But somebody had to write c ode at some point. Fortunately for us,
when t hey did they put it all in a nic e little pac kage and c alled it a window object. Now, whenever you need a
window in your Projec t you c an make a c opy of the window object , c hange its properties for c olor or size very easily,
and paste it where you want it . T hen you c an use its built-in methods to open it, close it when you want or resize it
whenever nec essary. When you create an applic ation using objec ts and combining them to produc e results, you are
working in an object-oriented environment.

Event-driven

To produc e an applic ation in COBOL, a proc edural language, you write COBOL sourc e programs, you c ompile them into
mac hine c ode and t hen you run them via a c ontrol interfac e such as JCL. A program c an c ontain 1000's of lines of
source c ode and c ould run for hours with no human intervention. In fac t, in large installations, a jobstream c an c onsist
of a dozen programs, all automatically ac c epting input from the previous program and produc ing output for the next.
The programmer c an be blissfully unaware that the program has run unless something c atastrophic happens.

In a VB project , the proc esses that oc c ur have to be assoc iated with events. An event is something that happens -
the user clicks on a button, a form is opened, the result of a c alculation is too large. T he operation is event-driven
bec ause everything that exec utes does so as the result of some kind of event. The role of the programmer is to
ant ic ipate the event s and to write the c ode that will be executed when the event oc c urs. A VB application is
interactive in the sense that the user is constantly interac ting with the program. The user inputs a Customer Id, the
program c hec ks the Id in the database and immediately brings up the customer's file or displays a message that the
partic ular Id is invalid.

Top

Jumping right in!


Project description

http://www.profsr.com/vb/vbless01.htm 1/5
F -X C h a n ge F -X C h a n ge
PD PD
1/5/2010 Visual Basic 6 forms and properties

!
W

W
O

O
N

N
Creating the Project
y

y
bu

bu
to

to
k

k
First thing to do is to c reate a Direc tory where you will store all your VB Projec ts. Call it VBApps,
lic

lic
C

C
w

w
m

m
w for example. Then start VB. The first screen will ask whether you want to open a new projec t or an w
w

w
o

o
.d o .c .d o .c
c u -tr a c k existing one - it's obviously a new one and it will be a Standard EXE. Then, maximize all the c u -tr a c k
windows (it's easier t o work with - some of the examples in the tutorial had to be reduc ed for the
sake of the presentation). Now, save your project. It will first ask you to save the form - c all it
Sc ore.frm - and then the Projec t - call it Sc orebrd.vbp. From now on, do File-->Save Projec t very,
very frequently.

Before you start to build-up the form, it will make it easier if you change the c olor of the form.
Otherwise you will be working with grey c ontrols on a grey bac kground. T o c hange the c olor, just
c lick anywhere on the form, go to the properties window, find the property c alled Bac kColor and
c hange it to the standard Window bac kground (teal) or to any c olor you want in the palette.

In our first example we will need 6 labels and 2 command buttons. Eac h one of t hese objec ts
that you put on a Form is c alled a control. To get a c ontrol you go to the Toolbox, click on the
c ontrol you want, c ome bac k to the Form and c lic k and drag the control to the size and position
you want . Position the controls somewhat like in the diagram below.

http://www.profsr.com/vb/vbless01.htm 2/5
F -X C h a n ge F -X C h a n ge
PD PD
1/5/2010 Visual Basic 6 forms and properties

!
W

W
O

O
N

N
y

y
bu

bu
to

to
k

k
lic

lic
C

C
w

w
m

m
w w
w

w
o

o
.d o .c .d o .c
c u -tr a c k c u -tr a c k

IMPORTANT NOTE: If this is your first experienc e wit h VB, don't be afraid to
experiment . T his is hands-on stuff! Remember that VB is a Mic rosoft produc t,
therefore it works with the standard Windows interface. All the func tions you
know from MS-Offic e work t he same way here: Copy, Cut, Paste,
(Ctrl)+(Click), (Shift)+(Clic k), drag the mouse over a group of c ontrols to
selec t t hem all, etc . The Undo button is a nic e one to keep handy - when you
modify a c ont rol you c an always Undo the c hange - remember this when you
get to the part about aligning the controls, making them all the same size and
so on. That part c an get tricky. If you ac c identally end up in the Code
window while palying around, go down a few paragraphs and you will see how
to get bac k to the Form. At this point t he worst that c an happen is that your
Form will get all messed up. So what! You can just sc rap it and start over
again, but you will have learned somet hing.

Now that we have a bunc h of c ontrols on the form, we have to jazz them up a bit. We do this by
c hanging the Properties of the c ontrols in the Properties window. Eac h c ontrol has a whole
series of properties, most of whic h we won't need right now. The ones we do need are:
Alignment = how t ext aligns in the control
Bac kColor = c hoose the color of the bac kground
Caption = the text that will appear in the c ontrol
Font = c hoose the font type and size
ForeColor = c hoose the color of the text (foreground)
As wit h all Windows applications, you can selec t multiple c ontrols with (Ctrl)+(Clic k) and c hange a
property for all of them at onc e. For example, if all bac kgrounds are white, selec t all controls,
c hange ForeColor to white and all of t hem are modified. Change your form to look like the one
below. Note that you do not have to change t he Capt ion for Label4, Label5 and Label6 and that
you c an't c hange the c olor of the buttons. They insist on being what was c alled in the old days
"IBM grey". Don't forget to save your project often as you go along!

http://www.profsr.com/vb/vbless01.htm 3/5
F -X C h a n ge F -X C h a n ge
PD PD
1/5/2010 Visual Basic 6 forms and properties

!
W

W
O

O
N

N
y

y
bu

bu
to

to
k

k
lic

lic
C

C
w

w
m

m
w w
w

w
o

o
.d o .c .d o .c
c u -tr a c k c u -tr a c k

If you Run the application at this point, you should see your Form appear, just the way you
c reat ed it. However if you click on any of the c ontrols, absolutely nothing happens! There are
events that oc c ur; the form opens, a button is c lic ked, etc . But, there is not hing that tells the
form what to do when it sees an event. That is why we have to write code, also called script.

Top

To switc h between the Code window and t he Form window, use the butt ons just over the Projec t
Explorer window (diagram on the left).
Onc e in the Code window, you have the option of seeing all the code for t he Projec t or the c ode
for one event at a time. Use the buttons in the lower left-hand c orner (diagram on the right).
To selec t the objec t and the event you wish t o c ode, use t he two Listboxes at the top of the
Code window. The one on the left for the objec t and the one on t he right for the event. Start with
General ... Declarations and then Form ... Load, etc .

At t his point you might want to download the sample program and study it. In the following
lessons we'll add func tionality to the exerc ice and we'll explain what the c ode means. But for the
moment, a good exerc ice would be to write part of the c ode and then try to figure out how to
improve certain aspec ts of the program.

You c an download the applic at ion here: FootScoreboard.zip

http://www.profsr.com/vb/vbless01.htm 4/5
F -X C h a n ge F -X C h a n ge
PD PD
1/5/2010 Visual Basic 6 forms and properties

!
W

W
O

O
N

N
y

y
bu

bu
to

to
k

k
lic

lic
C

C
w

w
m

m
w w
w

w
o

o
.d o .c .d o .c
c u -tr a c k c u -tr a c k

Now we can Run it and see something happen. When the Form loads, it will initialize the fields that
we spec ified in the code.

Now c ode the Command1 button and Run it t o see the result.

For additional resourc es you might find this sit e useful: Free Visual Basic 6 tutorials, samples
and source code examples.

If you haven't found the Visual Basic resource you're looking for,
use our Google Search box for more information.

Search

Top

Home | Tutorials | Contact | Sitemap | Add URL | Privacy policy

http://www.profsr.com/vb/vbless01.htm 5/5

Das könnte Ihnen auch gefallen