There are several ways to make
choices and selections. Among those,
the simplest way to make decision in a program is to use “If…Then”
statement. Let’s try it out in the next
section.
The basic structure of selection can be visualized in the following flowchart:

Flowchart of a Simple
“If…Then” Statement.
As you can see,
when you encounter the “condition”, the program must make a decision, and then
follow the branch depending on the value of the condition ("yes" or "no"; "true"
or "false"; "1" or "0").
We
can write a code using the “If…Then” construction in VB as
follows:
If condition
Then
(statement_block_1 to be
executed if the condition is satisfied)
(statement_block_2 to be
executed if the condition is NOT satisfied)

Flowchart of a Simple
“If…Then, Else” Statement.
The “If…Then, Else If” condition looks like:

Flowchart of a Simple
“If…Then, Else If” Statement.
If there are more than two conditions, we can use
If condition1
Then
(statement_block_1 to be
executed if the condition1 is satisfied)
(statement_block_2 to be
executed if the condition2 is satisfied)
(statement_block_3 to be
executed if the condition3 is satisfied)
.
(statement_block_N to be
executed if the NONE of the conditions are satisfied)

Flowchart of a “If…Then…Else
If” Statement
On some occasions, you may also use
a “Select Case” statement. See
page 105-107 of Kerman and Brown for details.
Develop the "form" shown in the figure above that we will use in the project. The form should contain a label, a text box, and two command buttonswith the following properties:
| Object | Property | Setting |
| Form | Caption Name |
Password Test frmPassword |
| Label | Caption Name Alignment |
Enter your password and press Test lblPrompt 2-Center |
| Text Box | Name PasswordChar |
txtPassword * |
| Command Button 1 | Caption Name |
Test Password cmdTest |
| Command Button 2 | Caption Name |
Exit cmdExit |
Your form should look like this:

Next, add the following code to the buttons: cmdTest and cmdExit. First, double click on the cmdTest button and type in the code for cmdTest_Click( ) procedure and then double click on the cmdExit button and type in the code for cmdEdit_Click( ) procedure.

A counter variable "i" is used to count how many times the user tries to test a password. Each time the cmdTest button is clicked (the cmdTest_Click( ) procedure is initiated), the counter is incremented and the password is tested. The outer If-Block tests if the password is correct or incorrect. If it is correct, then the success message is printed in the label (the "caption" is changed using the lblPrompt.Caption method). If the counter is less than 3 (i < 3) and the password is incorrect, then the failure message is printed in the label. If the counter reaches 3, the label message tells the user to exit the program. The counter variable needs to have global focus in order for the counter to track how many times the button has been clicked.
The cmdExit_Click( ) procedure simply ends the program using the End command.


Position of
the Objects and Position Properties
You may also realize that the origin (i.e., (Left, Top) = (0, 0)) is at the “Upper Left” corner of the screen.
You may have noticed that there are “Line”
and “Shape”
controls in the
toolbox. You may draw them on your forms by clicking on these buttons and dragging
and releasing the mouse, in the same way
that we placed other objects such as command buttons and textboxes. If you change the properties of the shape
(or line), you may change the shape, size and color of the object.

An Example of Drawing Lines and Shapes in Visual Studio (Japanese National Flag)
You may also write a program to draw points, lines and circles. First, experiment a little with this. Create a form with a button and a picture box with the following properties:
| Object | Property | Setting |
| Form | Caption Name |
Draw Test frmDraw |
| Command Button 1 | Caption Name |
Press to Draw cmdDraw |
| Picture Box | Name | picOutput |
Your form should look like this:

Next, add the following code to the cmdDraw button. Double click on the cmdDraw button and type in the code for cmdDraw_Click( ) procedure:

The procedure will draw a line, of length 1000 units, originating at the upper left-hand corner (origin of coordinate system) and then a circle, centered at the point (1000, 1000) with radius 1000 units. The units here are twips (1/20 of a printer point = 1/1440 in. = 1/567 cm), a pretty small unit.
Run the program and you should see the following:

You can change the thickness of the lines that your program draws by using the ".DrawWidth" method associated with the picOutput object. Try adding the following line before the picOutput.Line command and reruning you program:
picOutput.DrawWidth = 5
You can control the filling of your drawing objects by using the ".FillStyle" method associated with the picOutput object. Try adding the following line before the picOutput.Line command and reruning you program:
picOutput.FillStyle = 0
Fill styles include: 0 = solid; 1 = horizontal lines; 2 = vertical lines; 3 = diagonal lines; and 6 = hatch.
picOutput.FillColor = RGB(0, 255, 0)
picOutput.Line (0, 0)-(1000, 1000), RGB(255, 255, 0)
picOutput.Circle (1000, 1000), Radius, RGB(255, 255, 0)


Let's change the code for cmdDraw_Click( ) procedure to look like:

When you run the program, it creates an output like the following:

The program is drawing circles, beginning at the origin with radius 200 units, and moving the center outwards and increasing the radius. The color is also changing as the circles are drawn, starting at RGB(0,0,0) = black and ending at RGB(250, 50, 50) = some kind of red color.
Points can also be drawn using the ".PSet" method. You can try adding
picOutput.PSet (CurrentX, CurrentY), RGB(R, G, B)
and see the result.
Plotting a Graph in VB
We can produce a graph of a function in VB. This example is taken from Example 10_26 on pages 275 - 278 of the text. Create a form with a button and a picture box with the following properties:
| Object | Property | Setting |
| Form | Caption Name |
Example10_26 frmExample10_26 |
| Command Button 1 | Caption Name |
Press to Graph cmdGraph |
| Picture Box | Name | picOutput |
Your form should look like this:

Next, add the following code to the cmdGraph button. Double click on the cmdGraph button and type in the code for cmdGraph_Click( ) procedure:

The procedure will draw a plot of the function y = x^2. First the coordinate system of the picOutput object is scaled to the range of interest of the function. Then the axes are drawn as lines in the vertical and horizontal directions. Then the line width is increased and the function is graphed in the color red.
The Scale method takes as parameters the coordinates of two opposite corners of a region. It sets a PictureBox's Scale properties so they match that region. For example, this code:
picOutput.Scale (x1, y1)-(x2, y2)
sets the picture box's Scale properties to these values:
| Property | Value |
|---|---|
| picOutput.ScaleLeft | x1 = -15 |
| picOutput.ScaleWidth | x2 - x1 = 30 |
| picOutput.ScaleTop | y1 = 120 |
| picOutput.ScaleHeight | y2 - y1 = 130 |
The Line method takes as parameters the coordinates of the two endpoints of a line and draws a line between them. For example this code:
picOutput.line (x1, y1)-(x2, y2)
draws the line segment between the two points (x1, y1) and (x2, y2). The Line method can draw straight lines, rectangles, and rectangles filled with color. Set the thickness of the line with the DrawWidth property. You can draw dashed and dotted lines by setting the DrawStyle property.
The DrawWidth method sets the width of the lines that are drawn on the object which the method is applied to. For example, this code
picOutput.DrawWidth = 5
sets the drawing width to 5 units in the picOutput object.
The PSet method is used to color one pixel at a time on the screen, but the size is set by the DrawWidth method or the default setting.. For example, the code
picOutput.PSet (x, y), RGB(255,0,0)
will draw red dots on the picture box at the location (x,y).
Run the program and you should see the following:

Chapter 5, problems 13 and 14 pages 119-120. Given the function

1. Write a program that computes the values of f(x) when x values range from -5 to +5 with an increment of 0.5. Display x and corresponding f(x) values in a table within a picture box and an output file. Also graph the values of the function in another picture window in the user interface.
2. Modify your program to prompt the user for an x value and output the corresponding f(x) value in a picture window. The program should end when the user enters a value of 100,000 for x.
1. The code for your program from part 1
A screenshot from running you program in part 1. Include the table and graph in the shot.
2. The code for your program from part 2
A screenshot from running you program in part 2. Include the picture box output for 2 values of x and then the exit value.