Hand
out 002 ( Programming 2)
Working
with Toolbox Controls
After
completing this chapter, you will be able to:
Use TextBox and
Button controls to create a Hello World program.
Use the DateTimePicker
control to display your birth date.
Use CheckBox,
RadioButton, ListBox, and ComboBox controls to process
user input.
Use the LinkLabel
control and the Process.Start method to display a Web page by using
your system’s default browser.
Example Program 2
Create
a Hello World program
1.
Start Visual Studio 2008 if it isn’t already
open.
2.
On the File menu, click New Project.
Visual
Studio displays the New Project dialog box, which prompts you for the name of your
project and for the template that you want to use.
3.
Ensure that the Visual Basic project type and
the Windows category are selected, and then click the Windows Forms Application
template.
These
selections indicate that you’ll be building a stand-alone Visual Basic
application that will run under Windows.
4.
Remove the default project name
(WindowsApplication1) from the Name text box, and then type MyHello.
5. Click OK to create your new
project.
6.
Click the TextBox control on the Common
Controls tab of the Toolbox.
7. Draw a text box similar to this:
8.
Click the Button control in the
Toolbox.
9. Draw a button below the text box on the form.
10.
Set the following property for the button
object by using the Properties window:
Object
Property Setting
Button1 Text 2 objects
11.
Double-click the OK button, and type the following program statement between
the
Private Sub
Button1_Click and End Sub statements
in the Code Editor:
TextBox1.Text
= "Hello, world!"
12. Run the Hello
World program
Example Program 3
Using the DateTimePicker
Control
The
Birthday Program
The
Birthday program uses a DateTimePicker control and a Button control
to prompt the user for the date of his or her birthday. It then displays that
information by using a message box.
Give it a
try now.
Build
the Birthday program
1.
On the File menu, click Close Project to close
the MyHello project. The files associated with the Hello World program close.
2.
On the File menu, click New Project.
The New
Project dialog box opens.
3.
Create a new Visual Basic Windows Forms
Application project named MyBirthday. The new project is created, and a
blank form appears in the Designer.
4.
Click the DateTimePicker control in the
Toolbox.
5.
Draw a date/time picker object in the middle
of the form, as shown in the following illustration.
The
date/time picker object by default displays the current date, but you can
adjust the displayed date by changing the object’s Value property.
Displaying the date is a handy design guide—it lets you size the date/time
picker object appropriately when you’re creating it.
6.
Click the Button control in the
Toolbox, and then add a button object below the date/time picker. You’ll use
this button to display your birth date and to verify that the date/time picker works
correctly.
7.
In the Properties window, change the Text property
of the button object to Show My
Birthday.
Now you’ll add a few lines of program code to a procedure associated with the
button
object.
This is an event procedure because it runs when an event, such as a mouse
click, occurs, or fires, in the object.
8.
Double-click the button object on the form to
display its default event procedure, and then type the following program
statements between the Private Sub and End Sub statements in the Button1_Click
event procedure:
MsgBox("Your
birth date was " & DateTimePicker1.Text)
MsgBox("Day
of the year: " & _
DateTimePicker1.Value.DayOfYear.ToString())
These
program statements display two message boxes (small dialog boxes) with
information from the date/time picker object. The first line uses the Text property
of the date/time picker to display the birth date information you select when
using the object at run time. The MsgBox function displays the string
value “Your birth date was” in addition to the textual value held in the
date/time picker’s Text property. These two pieces of information are
joined together by the string concatenation operator (&).
The second
and third lines collectively form one program statement and have been broken by
the line continuation character (_) because the statement was a bit too long to
print in this book.
The
statement DateTimePicker1.Value.DayOfYear.ToString() uses the date/time picker object to calculate the day of the year in
which you were born, counting from January
1. This is
accomplished by the DayOfYear property and the ToString method,
which converts the numeric result of the date calculation to a textual value
that’s more easily displayed by the MsgBox function.
Methods
are special statements that perform an action
or a service for a particular object, such as converting a number to a string
or adding items to a list box. Methods differ from properties, which contain a
value, and event procedures, which execute when a user manipulates an object.
Methods can also be shared among objects, so when you learn how to use a particular
method, you’ll often be able to apply it to several circumstances.
After you
enter the code for the Button1_Click event procedure, the Code Editor
looks
similar to this:
9.
Click the Save All button to save your changes
your folder location.
Now you’re ready to run the Birthday
program.
Run the Birthday
program
A Word About
Terminology
Program
statement
A program
statement is a line of code in a Visual Basic program, a self-contained
instruction
executed
by the Visual Basic compiler that performs useful work within the application.
Program statements can vary in length (some contain only one Visual Basic keyword!),
but all program statements must follow syntax rules defined and enforced by the
Visual Basic compiler. In Visual Studio 2008, program statements can be
composed of keywords, properties, object names, variables, numbers, special
symbols, and other values.
Keyword
A keyword
is a reserved word within the Visual Basic language that is recognized by the Visual
Basic compiler and performs useful work. (For example, the End keyword
stops program execution.) Keywords are one of the basic building blocks of
program statements; they work together with objects, properties, variables, and
other values to form complete lines of code and (therefore) instructions for
the compiler and operating system.
Most
keywords are shown in blue type in the Code Editor.
Variable
A variable
is a special container used to hold data temporarily in a program. The
programmer creates variables by using
the Dim statement and then uses these variables to store the results of
a calculation, file names, input, and so on. Numbers, names, and property
values can be stored in variables.
Control
A control
is a tool you use to create objects in a Visual Basic program (most commonly, objects
are created on a form). You select controls from the Toolbox and use them to draw
objects with the mouse on a form. You use most controls to create user
interface elements, such as buttons, picture boxes, and list boxes.
Object
An object
is an element that you create in a Visual Basic program with a control in the
Toolbox. (In addition, objects are sometimes supplied by other system
components and many of these objects contain data.) In Visual Basic, the form
itself is also an object. Technically speaking, objects are instances of a
class that supports properties, methods, and events. Objects also have what is
known as inherent functionality—they know how to operate and can respond
to certain situations on their own. (A list box “knows” how to scroll, for
example.)
Class
A class is
a blueprint or template for one or more objects that defines what the object does.
Accordingly, a class defi nes what an object can do, but is not the object
itself. In Visual Basic, you can use existing Visual Studio classes (like System.Math
and System. Windows.Forms.Form), and you can build your own classes
and inherit properties, methods, and events from them. (Inheritance
allows one class to acquire the pre-existing interface and behavior
characteristics of another class.) Although classes might sound esoteric at
this point, they are a key feature of Visual Studio 2008, and in this book, you
will use them to build user interfaces rapidly and to extend the work that you
do to other programming projects.
Namespace
A
namespace is a hierarchical library of classes organized under a unique name,
such as System.Windows or System.Diagnostics. To access the
classes and underlying objects within a namespace, you place an Imports statement
at the top of your program code. Every project in Visual Studio also has a root
namespace, which is set using the project’s Properties page. Namespaces are
referred to as object libraries or class libraries in Visual Studio books and
documentation.
Property
A property
is a value, or characteristic, held by an object. For example, a button object
has a Text property to specify the text that appears on the button and
an Image property to specify the path to an image fi le that should
appear on the button face. In Visual Basic, properties can be set at design
time by using the Properties window or at run time by using statements in the
program code. In code, the format for setting a property is
Object.Property
= Value
where Object
is the name of the object you’re customizing, Property is the
characteristic you want to change, and Value is the new property
setting. For example,
Button1.Text
= "Hello"
could be
used in the program code to set the Text property of the Button1 object
to “Hello”.
Event
procedure
An event
procedure is a block of code that’s executed when an object is manipulated in a
program. For example, when the Button1 object is clicked, the Button1_Click
event procedure is executed. Event procedures typically evaluate and set
properties and use other program statements to perform the work of the program.
Method
A method
is a special statement that performs an action or a service for a particular object
in a program. In program code, the notation for using a method is Object.Method(Value)
where Object
is the name of the object you want to work with, Method is the
action
you want
to perform, and Value is an optional argument to be used by the method.
For
example, the statement
ListBox1.Items.Add("Check")
uses the Add
method to put the word Check in the ListBox1 list box.
Methods and properties
are often
identified by their position in a collection or object library, so don’t be surprised
if you see long references such as System.Drawing.Image.FromFile, which
would
be read as
“the FromFile method, which is a member of the Image class, which
is a member
of the System.Drawing
object library.”
Controls
for Gathering Input
Visual
Basic provides several mechanisms for gathering input in a program. Text
boxes accept typed input, menus present commands that can be clicked
or chosen with the keyboard, and dialog boxes offer a variety of
elements that can be chosen individually or selected in a group. In this
exercise, you’ll learn how to use four important controls that help you gather
input in several different situations. You’ll learn about the RadioButton,
CheckBox, ListBox, and ComboBox controls. You’ll explore
each of these objects as you use a Visual Basic program called Input Controls,
which is the user interface for a simple, graphics-based ordering system. As
you run the program, you’ll get some hands-on experience with the input
objects. In the next chapter, I’ll discuss how these objects can be used along
with menus in a full-fl edged program. As a simple experiment, try using the CheckBox
control now to see how user input is
processed
on a form and in program code.
Experiment
with the CheckBox control
Example Program 4
1.
On the File menu, click Close Project to close
the Birthday project.
2.
On the File menu, click New Project.
The New
Project dialog box opens.
3.
Create a new Visual Basic Windows Forms
Application project named MyCheckBox.
The new project is created, and a blank form
appears in the Designer.
4.
Click the CheckBox control in the
Toolbox.
5.
Draw two check box objects on the form, one
above the other. Check boxes appear as objects on your form just as other
objects do. You’ll have to click the CheckBox control in the Toolbox a
second time for the second check box.
6.
Using the PictureBox control, draw two square
picture box objects beneath the two
check
boxes.
7.
Set the following properties for the check box
and picture box objects:
Object Property Setting
CheckBox1 Checked True
Text
“Calculator” CheckBox2 “Copy
machine”
Text
PictureBox1 Image select
your picture
SizeMode StretchImage
PictureBox2
SizeMode StretchImage
In this
walkthrough, you’ll use the check boxes to display and hide images of a
calculator and a copy machine. The Text property of the check box object
determines the contents of the check box label in the user interface. With the Checked
property, you can set a default value for the check box. Setting Checked
to True places a check mark in the box, and setting Checked to False
(the default setting) removes the check mark. I use the SizeMode properties
in the picture boxes to size the images so that they stretch to fi t in the
picture box.
Your
form looks something like this:
8.
Double-click the first check box object to
open the CheckBox1_CheckedChanged event procedure in the Code Editor,
and then enter the following program code:
If
CheckBox1.CheckState = 1 Then
PictureBox1.Image
= System.Drawing.Image.FromFile (locate your picture selected)
PictureBox1.Visible
= True
Else
PictureBox1.Visible
= False
End If
The CheckBox1_CheckedChanged
event procedure runs only if the user clicks in the first
check box
object. The event procedure uses an If…Then decision structure to confirm the current status, or state,
of the first check box, and it displays a calculator picture from the folder if
a check mark is in the box. The CheckState property holds a value of 1
if there’s a check mark present and 0 if there’s no check mark present. (You
can also use the CheckState. Checked
enumeration, which appears in IntelliSense when you type, as an alternative to setting
the value to 1.) I use the Visible property to display the picture if a
check mark is present or to hide the picture if a check mark isn’t present.
9.
Click the View Designer button in Solution
Explorer to display the form again,
double-click
the second check box, and then add the following code to the
CheckBox2_Checked-Changed
event procedure:
If
CheckBox2.CheckState = 1 Then
PictureBox2.Image
= System.Drawing.Image.FromFile _
(locate
another picture you want)
PictureBox2.Visible
= True
Else
PictureBox2.Visible
= False
End If
This event
procedure is almost identical to the one that you just entered; only the names
of the image (copymach), the check box object (CheckBox2), and the
picture box object (PictureBox2) are different.
10.
Click the Save All button on the Standard
toolbar to save your changes, specifying the
folder as
the location.
Run
the CheckBox program
Exercises
No. 1
Create the
following form like this:
If the
users press the button for PC it will display the picture of computer on the
first box.
If the
users press the button for Macintosh it will display the picture of Macintosh
on the first box.
If the
users press the button for Laptop it will display the picture of laptop on the
first box.
If the
users check the answering machine it will display the picture answering machine
on the 2nd box.
If the
users check the answering calculator it will display the picture calculator on
the 3rd box.
If the
users check the copy machine it will display the picture copy machine on the 4th
box.
Under
payment method user will select on the following value ( Cash, credit)
Under List
Box it will display the different
peripheral devices such as CPU, disk Drive, CD Rom
No comments:
Post a Comment