Visual Basic Part I - A tool for customizing your program - PowerPoint PPT Presentation

1 / 39
About This Presentation
Title:

Visual Basic Part I - A tool for customizing your program

Description:

Go to Start Programs Microsoft Visual Basic 6.0 Visual Basic ... messages, such as 'You like 'Garth Brooks', 'You like Mozart', 'You like ... – PowerPoint PPT presentation

Number of Views:74
Avg rating:3.0/5.0
Slides: 40
Provided by: Pete8
Category:

less

Transcript and Presenter's Notes

Title: Visual Basic Part I - A tool for customizing your program


1
Visual Basic Part I - A tool for customizing
your program
  • Advanced GIS
  • Fall 2003

2
OOP and structure programs
Structured programs - designed to solve one big
problem, subdivided into smaller sections.
Object-oriented programs - break the problem
down into objects, each with a life of their own,
with certain characteristics (properties)
and certain functions that it is able to perform
(methods)
3
Opening of Visual Basic screen
  1. Go to Start gt Programs gt Microsoft Visual Basic
    6.0 gt Visual Basic
  2. Click on OPEN once you see the window shown
    below

4
Title bar
Menu bar
Menu toolbar
Project explorer
Form
Properties window
toolbox
Code window
if you dont get this, double-click on Form
5
The Toolbar
Object browser
Break
Toolbox
End
Start
Redo
Undo
Save project
Form layout window
Open Project
Properties window
Menu editor
Project explorer
Add item type
Add Project (on File menu)
6
Form - central piece of the VB
  • typical application is derived from a Form
  • Like an artist, VB programmers lay down controls,
    such as command buttons, text boxes etc. on to
    the canvas - the form
  • Code - invisible to the users

7
Controls and the Toolbox
Picture TextBox CommandButton OptionButton ListBox
VScrollBox DriveListBox FileListBox Line Data
Pointer Label Frame CheckBox ComboBox HScrollBar T
imer DirListBox Shape Image OLE
8
Taste of the codeDraw a circle and two lines
Circle method Object.Circle(xo,yo), radius Line
(starting point) - step(offset xy) Line (x1,y1) -
(x2,y2) Line -(x2,y2) //draw from previous
point Line -step(x2,y2) // offset from previous
point
9
  • Run and see what happens
  • Your screen will have a blank form without lines
    and circle.
  • now, turn Autodraw from false to true (from
    Properties Window)
  • Ok, now run again.

10
This is what you get
11
Forms properties
  • Try ControlBox turned to False from True
    (you will lose control over close, Maximize
    and minimize windows
  • BorderStyle from 2-Sizable to 1-Fixed Single
    (not allowed to resize the window)

12
Code
  • Each form has certain events associated with it
    and the most important one is Load event. The
    Load event is triggered whenever a form is
    first loaded and just before it is displayed
  • Lets try to create a simple program that places
    todays date on the form

13
Comment Block(simply add a single quote before
the code line) or just following the following
instruction
  • Add comment to codes, like in Avenue
  • Add Edit toolbarplacing your cursor on the
    toolbar, clicking the right mouse button, and
    then choosing Edit.
  • highlight the code you want to use to create a
    comment block
  • Click the Comment Block or Uncomment Block button
    on the Edit toolbar.

14
Label Control
  • Drag a box after you select control box from
    toolbox
  • Run it (with code input) and see what do you get
    (with Circle method is
  • commented out)

(StartupPosition - 2 Center on Screen)
type in the statements shown here
15
Run it again and see the display
try the following Label1.Caption Now (it will
display the current time and date) Within
Unload event (switch your event list from load
to unload), type in Msgbox Bye! Run it and see
what happens when you close the window
16
Timer
  • You have to use Timer to show the moving time
    instead of only showing current time (previous
    case)
  • drag a timer to Form1 and type in the following
    code within Timer1_Timer()
  • Label1.Caption Now,
  • Set the timer Interval in its properties window
    to, 3 from 100 (millionsceonds), that means a
    tenth of a second
  • comment out your code in Form1.Load()

17
Working with Controls
  • Placing controls on the form
  • Click on command button from the toolbox (size
    it to the appropriate size and location on the
    form)
  • Go to Tools/Options../General/Turn off the Grid
  • Place one more command and lock them by going to
    Format/Lock Control and now try to move it and
    see what happens

18
This is what you will get
19
Now we need to make the control working
  • Three types of hook
  • 1) Properties how control looks and behaves, eg.
    people with height, weight...
  • 2) Methods all things that the control can do,
    people can eat
  • 3)Events the things you can do to a control that
    it will recognize and be able to respond to, eg.
    mouse over, click, down...

20
Make the control beep
  • type in Beep in code window as shown left
  • Click on Start or F5 and then Enter
  • Click on Command 1 and you will hear
  • Dong (you may not be able to hear it since
    there is no speakers attached to the lab machines)

21
Caption
  • The Caption Property found on objects such as
    forms, frames, and command buttons, is a piece of
    text that is displayed on screen to give the
    object some kind of header or title
  • Try to change the Caption of the command box to
    Caption here
  • Try this Change and the hot key (C) will be
    underlined

22
Text Box and other exercise
  • Draw a text box from the toolbar and change the
    font to Tahoma/10 (font name and size)
    BackColor to desktop
  • Change name to txtDisplay for textbox and
    cmdChange for command box
  • change the Multiline property of the text box
    from false to true.
  • change the Locked Property to True (you cant
    type in any letters now)
  • Double click on the Change button and add the
    following line to the event code
  • txtDisplay.TexttxtDisplay.Text Hello!

23
  • Run the program and press the Change button and
    see what happens
  • This is what you get after pressing Change many
    times

24
Label - used for placing text beside Text or
other controls
  • lightweight controls - use less system resources,
    memory and processor power.
  • Responds to many events except KeyPress (it
    doesnt take input)

25
Getting Data from a Text Box
  • add two labels, two text boxes, and a command
    button to Form1
  • Name the text box to txtName and txtGrade and
    label controls to lblName, lblGrade and
    command button to cmdOK and the form frmMain
    and set MaxLength property of the Grade textbox
    to 1

26
Adding code to the KeyPress Event
  • KeyPress used to check the input data
  • double-click on the txtName textbox to bring up
    the code window, when it appears, click on the
    arrow next to the word Change and select
    KeyPress from the list of events shown
  • Add one line of code //make sure you dont have
    name with numeric number
  • If KeyAscii gt Asc(0) And KeyAscii lt Asc(9)
    Then KeyAscii 0
  • End if

27
Type in the code for OK command
  • If txtGrade.Text "" Then
  • MsgBox "You must enter your grade"
  • Exit Sub
  • End If
  • If txtName.Text "" Then
  • MsgBox "Enter your name please"
  • Exit Sub
  • End If
  • MsgBox "Congratulations"

28
Second form in same project
  • You may add second form to the current project.
  • Go to ProjectgtAdd Form.
  • Once you have codes input to the second form, you
    may run it. However, you need to set the priority
    of the form, otherwise, the project will always
    run the first one and stop.
  • Go to ProjectgtProject1 Properties and in the
    Startup Object select second form to start with.

29
Check Boxes
  • Allows you to present on/off, true/false options
    to users
  • Add three check boxes to the Form1
  • Change the first one to Click Me (Caption) and
    add the following to Check1_Click()
  • MsgBox The value is now Check1.Value
  • Value properties 0 - Unchecked, 1-Checked,
    2-Greyed out

30
Result
after you check the first checkbox
31
Exercise
  • Create a title for the page in the form1
  • create four Option boxes and name them
    optCountry, optClassical, optJazz, and optRock
  • Caption - Country, Classical, Jazz and Rock
  • Align them on left
  • Associate MsgBox to each of them by showing the
    messages, such as You like Garth Brooks, You
    like Mozart, You like Armstrong, How about
    Beetles,....

32
Image and Picture Controls
  • Image file formats .bmp, .wmf, gif, jpg,
    .ico. The most important property of these two
    is Picture property
  • Images cannot be placed on top of other
    controls, unless they are first placed inside a
    frame or picture box.
  • Picture Control More functional than image, can
    act as a container (like form in form)

33
Exercise
  • Create a Image box and place SleeplingPuppy.jpg
    inside of the box
  • make Stretch true from false
  • Create a Picture box and place ttu.gif inside
    of the box

34
Enable Property - makes control grayed out
  • Exercise - put two command buttons on the form
    and name them cmbEnable and cmbMessage and
    Caption them as Enable/Disable and Message
  • Double click on cmdEnable and add the following
    line of code to the buttons click event
  • cmbMessage.Enable Not (cmdMessage.Enabled)
  • Add the following code to cmdMessages Click
    event
  • MsgBox Hello!
  • Run the program and click the message button

35
Click on Enable/Disable you get
reverse the messages Enable property
Click on Message you get
36
TabIndex -focus or not
  • use tab to move around the controls
  • TabIndex 0 -gt the first focus and so on.
  • Check to see the TabIndex in cmdMessage and you
    will find that it is 1 , Change it to 0 and
    run the program again.

37
Exercise - Controlling focus
  • create two textboxes and name them as txtUserid
    and txtPassword and have one OK command box
    as shown on left

Double click on txtUserid and select event
LostFocus and type in If txtUserid.Text
Then txtUserid.SetFocus If user move focus to
other text box without entering ID , this
Lostfocus code will set the focus back on to
itself
38
Homework 6 30 points, due on next Thursday
midnight. Detailed description of the homework
can be found in the link on the webpage.
  • Create two forms in VB project. Save forms and
    project under your folder.
  • The first form should contain textbox for asking
    user to select option boxes and once the box is
    selected, the imagebox will reflect the image
    linked to the option box (see demo)
  • The second form will ask user to guess a number
    between 1 and 5. Then shows msgbox for guessing
    right or wrong. A cmdVerify button will confirm
    users input is correct or not. cdmExit will have
    SetFocus while the input match random number
    generated from the Rnd() function.
  • Use rnd() to generate random number and use a
    variable for receiving the random number
  • Make sure you set intRnd Int(5Rnd()). Then,
    use a string variable to receive this number as
    string, such as
  • strguessNumber format((intRnd)

39
OLE (Object Linking and Embedding)
  • Once you put an OLE control on From1 a new window
    (Insert Object) appears. Select Microsoft Word
    Document
  • The format of the Form window will shift to Words
    format. Try to type a sentence and see what
    happens
Write a Comment
User Comments (0)
About PowerShow.com