Docsity

Intro to Programming with Visual Basic: Lecture 3 - Form Controls & Events, Study notes of Computer Science

Partial preview of the text

Download Intro to Programming with Visual Basic: Lecture 3 - Form Controls & Events and more Study notes Computer Science in PDF only on Docsity!

CS 0004 – Introduc/on to

Programming

Using Visual Basic Summer 2009 University of Pi@sburgh Lecture 3

Chapter 2.

GeMng Started with VB

Visual Basic Controls

Vocabulary words

  • Controls
    • Boxes, bu@ons, and other objects on forms that allow the user to interact with a program - You should know these controls: - Form - Textboxes - Bu@ons - Labels - List boxes

Star/ng a new VB Program

  • Visual Basic programs are also known as

solu+ons

  • And applica+ons
  • And projects
  • These words will be used interchangeably

throughout this class

  • Every VB program you write is actually a

collec/on of files – we’ll talk about this more

later.

Star/ng a new VB Project

  • From the Start page
    • Select FileNew Project
      • Select "Windows Forms Applica/on" (should be first op/on)
      • Give your project a name (“CS4‐Lecture3”)
  • Now you see the Windows Form Designer

Form Controls

  • Select Toolbox (leQ side)
    • Drag‐and‐drop to add Bu@ons, Textboxes, Labels, List boxes, etc.
    • Resize controls like you resize windows on your screen – click and drag from a corner or side
    • If you don’t want a control, select it with the mouse and then press the delete key
    • Each control has different proper/es that can be modified – let’s look at some...

Wait a minute… when is this a real program?

  • Go ahead and try hiMng the “Play” bu@on in

the toolbar.

  • You have just run a Visual Basic program that

you wrote. Congratula/ons!

You might want to save your

work…

  • Click on the “Save All” icon in the toolbar
  • Select where you want to save your program
    • Save it in “My Documents” for today
    • Take a look in the Explorer window to see your files

Bu@ons

  • Try…
    • Adding a few bu@ons to your form
    • Changing their sizes, moving them around
    • Modifying some proper/es
  • Clicking a bu@on in a running program will

create an event

  • We will learn about events and how to make things happen as soon as we finish playing with the interface controls

Bu@ons

  • Proper/es – look familiar?
    • Many of the same proper/es as Textboxes
    • Other controls will also have similar lists of proper/es
    • But each control is different

List boxes

  • Use list boxes to display tables, mul/ple lines

of output, or to let the user make a selec/on

  • Op/on A
  • Op/on B
  • Try it!
  • Which property did you modify to add text to the list box?

Naming conven/ons

  • Button1 is not very descrip/ve
    • Controls should have names that give an idea of what they do
    • Conven/ons exist for each type of control
  • Button1 becomes btnCapitalize
    • This makes sense if clicking the bu@on capitalizes some text.

Chapter 2.

Making Things Happen

Visual Basic Events

What is an event?

  • Users’ ac/ons are events:
    • Clicking a control
    • Pressing a key
  • The programmer defines what happens when

an event happens