1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional...

82
1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics

Transcript of 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional...

Page 1: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

1

Chapter 9 – Additional Controls and Objects

9.1 List Boxes and Combo Boxes

9.2 Eight Additional Controls and Objects

9.3 Multiple-Form Objects

9.4 Graphics

Page 2: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

2

9.1 List Boxes and Combo Boxes

• A Review of List Box Features

• Some Additional Features of List Boxes

• The Combo Box Control

• A Helpful Feature of Combo Boxes

Page 3: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

Fill a List Box at Design Time via its String Collection Editor

3

tasks button

click here to invoke string collection editor

Page 4: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

String Collection Editor

4

Fill by direct typing or by copying and pasting from a text editor or a spreadsheet.

Page 5: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

List Box at Run Time

5

lstMonths

selected item

The value of lstMonths.Text is the string consisting of the selected item.

Page 6: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

Indexes

The items in a list box are indexed with zero-based numbering.

lstBox.Items(0) lstBox.Items(1) . .

6

Page 7: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

List Box Properties

7

• lstBox.Text: The selected item as a string.

• lstBox.Items.Count: The number of items in the list box.

• lstBox.Sorted: If set to True, items will be displayed in ascending ANSI order.

• lstBox.SelectedIndex: The index of the selected item. If no item is selected, the value is -1.

Page 8: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

More List Box Properties

• lstBox.Items(n): The item having index n. It must be converted to a string before being displayed in a text or message box.

• lstBox.DataSource: The source of data to populate the list box.

8

Page 9: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

List Box Events• lstBox.SelectedIndexChanged: Raised

when the value of the SelectedIndex property changes. (default event procedure)

• lstBox.Click: Raised when the user clicks on the list box.

• lstBox.DoubleClick: Raised when the user double-clicks on the list box.

9

Page 10: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

List Box Methods

• lstBox.Items.IndexOf(value): Index of the first item to have the value.

• lstBox.Items.RemoveAt(n): Delete item having index n.

• lstBox.Items.Remove(strValue): Delete first occurrence of the string value.

• lstBox.Items.Insert(n, value): Insert the value as the item of index n.

10

Page 11: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

11

The Combo Box Control

• A list box combined with a text box

• The user has the option of filling the text box by selecting from a list or typing directly into the list box.

• Essentially same properties, events, and methods as a list box

• DropDownStyle property specifies one of three styles

Page 12: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

Combo Box Styles

12

Page 13: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

Styles at Run Time after Arrows are Clicked

13

The value of cboBox.Text is the contents of the text box at the top of the combo box.

Page 14: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

14

Example 3Private Sub btnDisplay_Click(...) _

Handles btnDisplay.Click

txtDisplay.Text = cboTitle.Text & " " & txtName.Text

End Sub

cboTitle

txtDisplay

txtName

cboTitle

Page 15: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

Helpful Feature of Combo Box

15

Page 16: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

16

9.2 Eight Additional Controls and Objects

• The Timer Control

• The Random Class

• The ToolTip Control

• The Clipboard Object

• The Picture Box Control

• The MenuStrip Control

• The Horizontal and Vertical Scroll Bar Controls

Page 17: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

17

The Timer Control

• Invisible during run time• Raises an event after a specified period of time• The Interval property specifies the time period

measured in milliseconds.• To begin timing, set Enabled property to True.• To stop timing, set Enabled property to False.• The event raised each time Timer1.Interval

elapses is called Timer1.Tick.

Page 18: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

18

Example 1: Form

OBJECT PROPERTY SETTING

tmrWatch Interval 100

Enabled False

txtSeconds

Page 19: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

19

Example 1: CodePrivate Sub btnStart_Click(...) Handles _ btnStart.Click txtSeconds.Text = "0" 'Reset watch tmrWatch.Enabled = TrueEnd Sub

Private Sub btnStop_Click(...) Handles btnStop.Click tmrWatch.Enabled = FalseEnd Sub

Private Sub tmrWatch_Tick(...) Handles tmrWatch.Tick txtSeconds.Text=CStr((CDbl(txtSeconds.Text) + 0.1))End Sub

Page 20: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

20

Example 1: Output

Page 21: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

21

The Random Class• A random number generator is declared

with the statement Dim randomNum As New Random()• If m and n are whole numbers and m < n

then the following generates a random whole number between m and n (including m, but excluding n)

randomNum.Next(m, n)

Page 22: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

22

Example 2Private Sub btnSelect_Click(...) Handles _

btnSelect.Click

Dim randomNum As New Random()

Dim num1, num2, num3 As Integer

num1 = randomNum.Next(0, 10)

num2 = randomNum.Next(0, 10)

num3 = randomNum.Next(0, 10)

txtNumbers.Text = num1 & " " & num2 &

" " & num3

End Sub

Page 23: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

23

Example 2: Output

Page 24: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

The ToolTip Control

• Appears in component tray of form with default name ToolTip1

• Allows a tooltip to appear when user hovers over a control (such as a text box)

• Text displayed in tooltip is the setting of the “ToolTip on ToolTip1” property of the control to be hovered over

24

Page 25: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

Example 3: Form

25

txtRate

“ToolTip on ToolTip1” property of txtRate has the setting “Such as 5, 5.25, 5.5 …”

Page 26: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

Example 3: Output

26

Page 27: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

Other Tooltip Properties

• AutomaticDelay - determines the length of time (in milliseconds) required for a tooltip to appear

• AutoPopDelay - determines the length of time the tooltip remains visible while the cursor is stationary inside the control

27

Page 28: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

28

The Clipboard Object

• Used to copy information from one place to another

• Maintained by Windows. It can even be used with programs outside of Visual Basic.

• A portion of memory that has no properties or events

Page 29: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

29

Using the Clipboard Object• To place something into the Clipboard:

Clipboard.SetText(str)

• To get something out of the Clipboard:

str = Clipboard.GetText

To delete the contents of the Clipboard:

Clipboard.SetText("")

Page 30: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

30

Pixels

• The graphics unit of measurement is called a pixel.

• To get a feel for pixel measurement, place a picture box on a form and look at the picture box’s Size property. The two numbers in the setting give the width and height of the picture box in pixels.

Page 31: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

31

Coordinates in a Picture Box

Each point in a picture box is identified by a pair of coordinates, (x, y).

x pixels

y pixels

(x, y)

Page 32: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

32

The Picture Box Control

• Designed to hold drawings and pictures

• The following statement draws a blue rectangle of width w and height h inside the picture box whose upper left hand corner having coordinates (x, y) :

picBox.CreateGraphics. DrawRectangle(Pens.Blue, x, y, w, h)

Page 33: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

33

The Picture Box Control (continued)

• To draw a blue circle with diameter d:

• picBox.CreateGraphics.

DrawEllipse(Pens.Blue, x, y, d, d)

• The numbers x and y give the coordinates of the upper-left corner of a rectangle having the circle inscribed in it.

Page 34: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

34

Picture Box Containing a Red Circle

picBox.CreateGraphics. DrawEllipse(Pens.Red, 35, 35, 70, 70)

Page 35: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

35

Picture Box Properties

• A picture can be placed in a picture box control with the Image property.

• Prior to setting the Image property, set the SizeMode property.

• AutoSize will cause the picture box control to be resized to fit the picture.

• StretchImage will cause the picture to be resized to fit the picture box control.

Page 36: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

36

Picture Box at Run Time

• A picture also can be assigned to a picture box control at run time:

picBox.Image = Image.FromFile(filespec)

• The SizeMode property can be altered at run time with a statement such as

picBox.SizeMode = PictureBoxSizeMode.AutoSize

Page 37: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

37

The MenuStrip ControlUsed to create menus like the following:

Top-level menu

Second-level menu

Page 38: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

38

Menu Events

• Each menu item responds to the Click event

• Click event is triggered by

• the mouse

• Alt + access key

• Shortcut key

Page 39: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

39

The Horizontal and Vertical Scroll Bars

Page 40: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

40

Scroll Bar Behavior• When the user clicks on one of the arrow

buttons, the scroll box moves a small amount toward that button.

• When the user clicks between the scroll box and one of the arrow buttons, the scroll box moves a large amount toward that button.

• The user can also move the scroll box by dragging it.

Page 41: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

41

Scroll Bar PropertiesThe main properties of a scroll bar control are Minimum, Maximum, Value, SmallChange, and LargeChange.

hsbBar.Value is between hsbBar.Minimum and hsbBar.Maximum, and gives the location of the scroll box.

Page 42: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

42

Scroll Bar Notes• The setting for the Minimum property must be

less than the setting for the Maximum property. • The Minimum property determines the values

for the left and top arrow buttons. • The Maximum property determines the values

for the right and bottom arrow buttons.• The Scroll event is triggered whenever any part

of the scroll bar is clicked.

Page 43: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

43

9.3 Multiple-Form Programs

• Startup Form• Scope of Variables, Constants, and

Procedures• Modality• Close and ShowDialog Methods• The FormClosing Event Procedure• Importing an Existing Form• Deleting a Form from a Program

Page 44: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

44

Multiple Forms

• Visual Basic programs can contain more than one form

• To add the new form, select Add Windows Form from the Project menu, to invoke the Add New Items dialog box.

Page 45: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

45

Add New Items Dialog Box

• Select Windows Form from the center pane.

• Optionally type in a name.

• Press the Add button.

Page 46: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

46

Add New Items Dialog Box (continued)

select Windows Form

type in name press Add button

Page 47: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

47

Solution Explorer

Both forms will be accessible through the Solution Explorer window.

Page 48: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

Startup Form

• The form that loads when the program starts running is called the startup form.

• To designate a form as the startup form

1.Right-click on the program name at the top of the Solution Explorer.

2.Click on Properties in the context menu.

3.Select the form from the “Startup form” drop-down list.

48

Page 49: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

49

Variables and Multiple Forms• Variables declared in the Declarations

section of a form with Public, instead of Dim, will be available to all forms in the program. The variable is said to have namespace-level scope.

• When a Public variable is used in another form, it is referred to by an expression such as

otherForm.variableName

Page 50: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

ShowDialog Method

The statement

frmOther.ShowDialog()

displays frmOther as a modal form. An open modal form must be closed before the user can work with any other form.

50

Page 51: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

51

Example 1: frmIncome (start-up form)

txtTotIncome

Page 52: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

52

Example 1: frmSources

Page 53: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

53

Example 1: frmSource’s CodePublic sum As Double

Private Sub frmSources_Load(...) Handles _ MyBase.Load (clear text boxes)End Sub

Private Sub btnCompute_Click(...) Handles _ btnCompute.Click

sum = CDbl(txtWages.Text) +

CDbl(txtIntIncome.Text) +

CDbl(txtDivIncome.Text)

End Sub

Page 54: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

54

Example 1: frmIncome’s Code

Private Sub btnDetermine_Click(...) Handles _ btnDetermine.Click

frmSources.txtName.Text = txtName.Text

frmSources.ShowDialog()

txtTotIncome.Text = FormatCurrency(frmSources.sum)End Sub

Page 55: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

FormClosing Event

• Analogous to the Load event

• Load event is raised before a form is displayed for the first time

• FormClosing event is raised before a form closes.

55

Page 56: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

Importing an Existing Form

1. Click on Add Existing Item in Project menu.

2. Navigate to and open the folder for the form.

3. Double-click on formName.vb.

Note: Text files must be copied separately.

56

Page 57: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

57

9.4 Graphics

• Graphics Objects

• Lines, Rectangles, Circles, and Sectors

• Pie Charts

• Bar Charts

• Animation

• Printing Graphics

Page 58: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

58

Graphics Objects

• Our objective is to draw bar charts and pie charts in a picture box.

• A statement of the form

Dim gr As Graphics = picBox.CreateGraphics

declares gr to be a Graphics object for the picture box picBox.

Page 59: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

59

Pixels

• The graphics unit of measurement is called a pixel.

• To get a feel for pixel measurement, place a picture box on a form and look at the picture box’s Size property. The two numbers in the setting give the width and height in pixels.

Page 60: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

60

Coordinates in a Picture Box

Each point in a picture box is identified by a pair of coordinates, (x, y).

x pixels

y pixels

(x, y)

Page 61: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

61

Display Text in Picture Box

Dim gr As Graphics = picBox.CreateGraphics

gr.DrawString(string, Me.Font, _

Brushes.Color, x, y)

Displays string in the picture box. The upper-left corner of the text has coordinates (x, y), the font used is the Form’s font, and the color of the text is specified by color.

Note: IntelliSense will provide a list of colors.

Page 62: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

62

Display TextDim gr As Graphics = picBox.CreateGraphics

Dim strVar As String = "Hello"

gr.DrawString(strVar, Me.Font, Brushes.Blue, 4, 30)

gr.DrawString("World",Me.Font, Brushes.Red, 35, 50)

Page 63: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

63

Draw a Line in a Picture Box

Dim gr As Graphics = picBox.CreateGraphics

gr.DrawLine(Pens.Color, x1, y1, x2, y2)

draws a line in the specified color from (x1, y1) to (x2, y2).

Note: IntelliSense will provide a list of colors.

Page 64: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

64

Draw a Line in a Picture Box (continued)

Dim gr As Graphics = picBox.CreateGraphics

gr.DrawLine(Pens.Blue, 50, 20, 120, 75)

Page 65: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

65

Draw a Solid Rectangle in a Picture Box

Dim gr As Graphics = picBox.CreateGraphics

gr.FillRectangle(Brushes.Color, x, y, w, h)

draws a solid rectangle of width w and height h in the color specified and having the point with coordinates (x, y) as its upper-left corner.

Note: IntelliSense will provide a list of colors.

Page 66: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

66

Draw a Solid Rectangle in a Picture Box (continued)

Dim gr As Graphics = picBox.CreateGraphics

gr.FillRectangle(Brushes.Blue, 50, 20, 70, 55)

Page 67: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

67

Draw a Solid Ellipse in a Picture Box

Dim gr As Graphics = picBox.CreateGraphics

gr.FillEllipse(Brushes.Color, x, y, w, h)

draws a solid ellipse in the color specified inscribed in the rectangle described by the values x, y, w, and h.

Note: When w = h, the ellipse is a circle. This is the only type of ellipse we will consider.

Page 68: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

68

Draw a Solid Ellipse in a Picture Box (continued)

Dim gr As Graphics = picBox.CreateGraphics

gr.FillEllipse(Brushes.Color, _

a - r, b - r, 2 * r, 2 * r)

draws a solid circle in the color specified with center (a, b) and radius r.

.

Page 69: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

69

Draw a Solid Ellipse in a Picture Box (continued)

For example,

gr.FillEllipse(Brushes.Blue, _ 80 - 40, 50 - 40, 2 * 40, 2 * 40)

Draws a solid blue circle of radius 40 and center (80, 50).

Page 70: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

70

A Sector of a CircleA sector of a circle (shown below as upper-left sector) is specified by two angles, θ1 (the start angle) and θ2 (the sweep angle).

Page 71: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

71

Start and Sweep Angles

Page 72: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

72

Draw a Sector

The statement

gr.FillPie(Brushes.Color, a - r, b - r, _

2 * r, 2 * r, startAngle, sweepAngle)

draws a solid sector of a circle with center (a, b), radius r, and having the specified startAngle and sweepAngle. The color of the sector is determined by the value of Color.

Page 73: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

73

Brushes, Pens, and Fonts

Variables can be used for brushes, pens, and fonts. For example, the statement

gr.FillRectangle(Brushes.Blue, 50,20,70,55)

can be replaced with

Dim br As Brush = Brushes.Blue

gr.FillRectangle(br, 50, 20, 70, 55)

Page 74: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

74

Single Data Type

Numeric variables used in Draw and Fill statements must be of type Integer or Single.

The Single data type is similar to the Double type, but has a smaller range (-3.4∙1038 to

3.4∙1038).

CSng converts other data types to the Single data type.

Page 75: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

75

Financing Public Schools Data

Amount (in billions) Percent

Federal $33 8%

State $206 49%

Local $180 43%

Page 76: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

76

Financing Public Schools Pie Chart

Page 77: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

77

Create the Pie ChartDim gr As Graphics = picBox.CreateGraphics

Dim percent() As Single = {.08, .49, .43}Dim br() As Brush = {Brushes.Blue, _ Brushes.Red, Brushes.Tan}Dim sumOfSweepAngles As Single = 0For i As Integer = 0 To 2 gr.FillPie(br(i), 5, 5, 200, 200, _ sumOfSweepAngles, percent(i) * 360) sumOfSweepAngles += percent(i) * 360Next

Page 78: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

78

Financing Public Schools Bar Chart

Page 79: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

79

Financing Public Schools Bar Chart

• Suppose the x-axis is 110 pixels below the top of the picture box.

• Let the unit for the rectangle heights be .5 pixels.

• Then the top of a rectangle corresponding to the quantity q is 110 – q/2 pixels from the top of the picture box.

Page 80: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

80

Create the Bar ChartDim gr As Graphics = picBox.CreateGraphics

Dim quantity() As Single = {33, 206, 180}'Draw x-axisgr.DrawLine(Pens.Black, 40, 110, 210, 110) 'Draw y-axisgr.DrawLine(Pens.Black, 40, 110, 40, 0)For i As Integer = 0 To 2 gr.FillRectangle(Brushes.Blue, _ 60 + i * 40, (110 – quantity(i) / 2), _ 20, quantity(i) / 2)Next

Page 81: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

81

Animation

Place an image into a picture box, and move the picture box a small distance with each tick of a Timer control.

Page 82: 1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes and Combo Boxes 9.2 Eight Additional Controls and Objects 9.3 Multiple-Form Objects 9.4 Graphics.

82

Move Ball

The following code moves the ball along a diagonal with each tick of the timer.

Private Sub Timer1_Tick(...) Handles _

Timer1.Tick

picBall.Left += 1

picBall.Top += 1

End Sub