Presentation is loading. Please wait.

Presentation is loading. Please wait.

Repetition and Multiple Forms

Similar presentations


Presentation on theme: "Repetition and Multiple Forms"— Presentation transcript:

1 Repetition and Multiple Forms
Chapter 6 Repetition and Multiple Forms

2 Objectives Add a second form to a solution
Define a solution's startup object Create an object programmatically Write Do and For loops to execute statements repeatedly Draw and paint shapes on a form Use the Timer control Display, close, and hide forms Design a report Use the PrintPreviewDialog and PrintDocument controls to print a report preview to the screen Use the StringFormat class

3 Adding a Form to a Solution (1)
Most applications are made up of multiple forms To add a new form, click Project and then click Add New Item Select the type of item to add from the dialog box To add an existing form, click Project, and then click Add Existing Item

4 Adding a Form to a Solution (2)
Local Project Items folder expanded Select Windows Form Enter form name

5 Defining the Startup Object (1)
Every solution has one startup object Startup object can be a form or a sub procedure named Main Set the startup object using the Project's General Property Page Right-click the project in the Solution Explorer, and then select Property Pages to activate the Property Pages

6 Defining the Startup Object (2)
General tab selected Startup object is Sub Main

7 Creating an Object Programmatically
The Win Form Designer writes statements to create control instances This code is typically collapsed and appears in the region "Windows Form Designer generated code" You can write statements to create form and control instances too To create an object, declare a variable with the New keyword Data type is the class name For a form, this is the value of the Name property Create an instance of the form named frmSplash Dim frmNew As New frmSplash()

8 Displaying a Form as a Modal Dialog Box
Call ShowDialog method of the form When a form is displayed as a modal dialog, form must be close before other forms can get focus Note that form's constructor is called when form instance is created Display the form using the variable frmNewSplash Dim frmNewSplash As New frmSplash frmNewSplash.ShowDialog()

9 Executing Statements Repeatedly (1)
A Do Loop executes statements repeatedly until some condition becomes True or False Note that the same Do loop can be written in different ways Do loop has the following syntax: Do [ While | Until ] condition [ Statements ] [ Exit Do ] Loop

10 Executing Statements Repeatedly (2)
Do While and Do Until loops evaluate a condition before executing the statements in the loop Condition evaluates to a Boolean value Syntax is the same as the condition in an If statement While keyword causes statements to execute while a condition is true Until keyword causes statements to execute until a condition becomes true Exit Do statement marks the end of the Do Loop

11 Do While Loop (Example)
Print the numbers from 1 to 10 to the Output window Dim pintCounter As Integer Do While pintCounter <= 10 Debug.WriteLine(pintCounter) pintCounter = pintCounter + 1 Loop

12 Do While Loop (Analysis)
Dim pintCounter As Integer = 1 Initialize loop variable Test condition again Do Until pintCounter > 10 Test condition (pintCounter > 10) True Debug.WriteLine(pintCounter) pintCounter = pintCounter + 1 Loop False  statements Exit loop

13 Do Until Loop (Example)
Print the numbers from 1 to 10 to the Output window Dim pintCounter As Integer Do Until pintCounter > 10 Debug.WriteLine(pintCounter) pintCounter = pintCounter + 1 Loop

14 Do Until Loop (Analysis)
Dim pintCounter As Integer = 1 Initialize loop variable Test condition again Do While pintCounter <= 10 Test condition (pintCounter <= 10) True Debug.WriteLine(pintCounter) pintCounter = pintCounter + 1 Loop False  statements Exit loop

15 Do Loop While and Do Loop Until
Do Loop While and Do Loop Until statements test condition after executing the statements in the loop instead of before Loop statements will always execute at least once Do [ Statements ] [ Exit Do ] Loop [ While | Until ] condition

16 Do Loop While and Do Loop Until (Example)
Print the numbers 1 to 10 to the Output window Dim pintCounter As Integer = 1 Do Debug.WriteLine(pintCounter) pintCount = pintCounter + 1 Loop While Counter <= 10

17 Do Loop While (Analysis)
Initialize loop variable Dim pintCounter As Integer = 1 Do Debug.WriteLine(pintCounter) pintCounter = pintCounter + 1 Execute statements Execute statements again Test condition (pintCounter <= 10) True Loop While pintCounter <= 10 False  statements Exit loop

18 Counters and Accumulators
A counter is a variable, used in a loop, whose value is increased by 1 each time through the loop A counter has the following general form Counter = Counter + 1 An accumulator is a variable, used in a loop whose value is incremented so as to calculate a total An accumulator has the following general form Accumulator = Accumulator + 1

19 Shorthand Notation for Counters and Accumulators
+=, -=, *=, /= are shorthand operators The following statements are equivalent Note that both illustrate the use of a counter pintCounter = pintCounter + 1 pintCounter +=1

20 Infinite Loops Loops that never exit are called infinite loops
Infinite loops happen when a condition does not ever occur that causes the loop to exit Click the Break button on the Debug toolbar to suspend execution The following loop prints the value 1 to the Output window forever: Dim pintCounter = 1 Do Until pintCounter > 10 Debug.WriteLine pintCounter Loop

21 The For Loop Similar to a Do loop
Executes statements repeatedly using a counter Use when you know in advance how many times the loop will execute Executes faster than a comparable Do Loop Any For loop can be written as a Do Loop but not the other way around

22 For Loop Syntax (1) For loop executes statements between For and Next a fixed number of times For counter = start To end [ Step increment ] [ Statements ] Exit For Next [ counter ]

23 For Loop Syntax (2) counter is set to start when the loop is initialized Statements in loop execute until counter becomes greater than end Note VB .NET automatically sets the value of counter counter is incremented by 1 by default Use Step increment clause to increment or decrement counter by a different value The Exit For statement causes the For loop to exit immediately Note that the value of counter is preserved

24 For Loop (Analysis) VB.NET initializes pintCounter to 1 (start)
For pintCounter = 1 To 10 VB.NET increments pintCounter and tests condition again. Increment is not specified so increment by 1 Test condition (pintCounter <= 10) True Debug.WriteLine pintCounter Next pintCounter statements Exit loop when start is greater than end False

25 For Loop (Example 1) Print the numbers 1 through 10 to the Output window Dim pintCounter As Integer For pintCounter = 1 To 10 Debug.WriteLine pintCounter Next pintCounter

26 For Loop (Example 2) Print the values from 2000 to 1900 to the Output window decrementing the value by 5 each time Dim pintYear As Integer For pintYear = 2000 To 1900 Step -5 Debug.Writeline(pintYear) Next pintYear

27 Introduction to Drawing
Graphical Device Interface (GDI) provides standardized classes and methods to draw shapes to a graphical output device Output device can be a form Output device can be a printer System.Drawing namespace contains classes to define shapes Graphics class provides methods to draw to an output device

28 The System.Drawing Namespace
Use to define graphical elements Rectangle structure defines the size and position of a rectangle SolidBrush class defines a brush made up of a single color Pen class is used to draw lines and outline a rectangle Font class defines typeface, size, and other attributes for text

29 Graphics Class Methods draw shapes and text to a graphical output device DrawLine method draws a line between two points using a Pen DrawRectangle method draws an outline of a rectangle using a Pen FillRectangle method draws a filled rectangle using a brush DrawString method draws a string of characters using a font, a color, and a brush

30 Steps to Create and Draw a Rectangle
Create an instance of the Rectangle Use Size and Point structures or define the x and y coordinates Create an instance of the Pen class to define the color and width of the border surrounding the rectangle Call the DrawRectangle method of the Graphics class to draw the rectangle outline

31 The Point Structure The Point structure defines starting point or ending point of a line, or the upper-left corner of a rectangle Standard prefix is "pnt" Syntax: Overloads Public Sub New ( x As Integer, y As Integer ) x and y arguments define the point's coordinates Upper-left corner of device is 0, 0

32 The Point Structure (Example)
The following statement creates a new point at the upper-left corner of the output device: Dim pntCurrent As New Point(0, 0)

33 The Size Structure Defines the size of a Rectangle
Standard prefix is "siz" Syntax: Overloads Public Sub New( width As Integer, height As Integer ) width and height arguments define horizontal and vertical distance Values are measured in points

34 The Size Structure (Example)
Create a size 1 inch square Dim sizCurrent As New Size(72, 72)

35 Rectangle Constructor
Overloaded constructor creates a rectangle Standard prefix for a rectangle is "rec" One syntax variation uses existing Point and Size structures. The other uses the x, y coordinates, width and height Syntax: Overloads Public Sub New( location As Point, size As Size ) Overloads Public Sub New( x As Integer, y As Integer, width As Integer, height As Integer )

36 Rectangle Constructor (Example)
Create a new Rectangle with existing Point and Size structures Dim pntCurrent As New Point(0, 0) Dim sizCurrent As New Size(72, 72) Dim recCurrent As New _ Rectangle(pntCurrent, sizCurrent) Create a new Rectangle without Point and Size structures Dim recCurrent As Rectangle recCurrent = New Rectangle(0, 0, 72, 72)

37 Understanding Color Computer monitors display colors by combining varying intensities of red, green, and blue colors The term RGB (red, green blue) describes these colors The alpha value defines the brightness of a color RGB values range from 0 to 255 0 means that color is turned of 255 means that color is applied at its full intensity

38 Color Structure Belongs to the System.Drawing namespace
Several named colors are supported Blue, Red, Yellow, etc… Create new colors using the FromArgb method R, G, B properties contain an RGB value "col" is the standard prefix for color Examples Dim colCurrent As Color = Color.Blue Dim colCurrent As Color = Color.Red

39 Creating a Color Overloaded FromArgb method creates a new color
FromArgb is a static method so do not use New keyword Overloads Public Shared Function FromArgb( red As Integer, green As Integer, blue As Integer ) red, green, and blue arguments contain RGB values

40 FromArgb Method (Example)
Create a new color (white) Dim colCurrent As Color colCurrent = Color.FromArgb(255, 255, 255, 255)

41 Brushes Used to fill rectangles
Standard prefix for a SolidBrush is "sb" SolidBrush constructor creates a brush having a solid color Syntax: Public Sub New( color As Color ) color argument contains an existing color Use named color or a color created from FromArgb method

42 Brushes (Example) Create a solid brush using a new color
Dim colCurrent As Color = _ Color.FromArgb(255,255,255) Dim sbCurrent = New SolidBrush(colCurrent) Create a solid brush using a named color Dim sbCurrent = New SolidBrush(Color.Red)

43 Filling a Rectangle Fill a rectangle by calling the overloaded FillRectangle method Two syntax variations The first uses an existing Rectangle structure In the second, you define the size and shape of the rectangle Syntax: Overloads Public Sub FillRectangle( brush As Brush, rect As Rectangle ) Overloads Public Sub FillRectangle( brush As Brush, x As Single, y As Single, width As Single, height As Single )

44 FillRectangle Method (Example)
Assume that the variable "e" references an instance of the Graphics class Code would appear in the form's Paint event handler Dim colCurrent As Color = _ Color.FromArgb(255, 255, 255) Dim sbCurrent = New SolidBrush(colCurrent) e.Graphics.FillRectangle(sbCurrent, 0, 0, 72, 72)

45 Introduction to Pens Use pens to draw lines
Standard prefix for a pen is "pen" Syntax: Overloads Public Sub New( color As Color ) Overloads Public Sub New( color As Color, width As Single ) color is named color or created color width is thickness of the line Unit of measure is points

46 Creating a Pen (Example)
Create a blue pen 3 points in width Dim penCurrent As New Pen(Color.Blue, 3)

47 Drawing an Outlined Rectangle
Call the DrawRectangle method of the Graphics class First method uses an existing Rectangle structure and the second uses rectangle coordinates and size Syntax: Overloads Public Sub DrawRectangle( pen As Pen, rect As Rectangle ) Overloads Public Sub DrawRectangle( pen As Pen, x As Integer, y As Integer, width As Integer, height As Integer )

48 DrawRectangle Method (Example)
Create an outlined rectangle the size of the form Assume that graCurrent references an instance of the Graphics class Dim penCurrent As New Pen(Color.Blue, 3) Dim recCurrent As Rectangle recCurrent = New Rectangle(0, 0, _ Me.Width – 3, Me.Height – 3) graCurrent.DrawRectangle(penCurrent, recCurrent)

49 DrawLine Method Draws a line using a Pen
Use existing Point structures or specify x,y coordinate values Syntax: Overloads Public Sub DrawLine( pen As Pen, point As Point, point As Point ) Overloads Public Sub DrawLine( pen As Pen, x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer )

50 DrawLine Method (Example)
Draw a line using x,y coordinate values Assume that e references an instance of the Graphics class Dim penLine As New Pen(Color.Red, 3) e.Graphics.DrawLine(penLine, 0, 0, _ , 100)

51 Drawing Text Create a font using the Font constructor
Font constructor creates a Font object using: Font name or family Font size Font style Draw a string using an existing Font and brush by calling the DrawString method

52 Font Class Standard prefix for the Font class is "fnt" Properties
Bold, Italic, Strikeout, and Underline properties define whether a particular font attribute is applied or not Name property gets font name SizeInPoints property gets the font size Value is measured in points Methods GetHeight method gets font height using current graphics context

53 Font Constructor Syntax:
Overloads Public Sub New( family As String, emSize As Single ) Overloads Public Sub New( family As String, emSize As Single, style As FontStyle) Family argument contains font name emSize contains font size in points Style defines attributes applied to the font (Bold, Italic, Underline, etc.)

54 Font Constructor (Example)
Create a 14 point, bold, Arial font Dim fntSplash As New Font("Arial", _ 14, FontStyle.Bold)

55 DrawString method Draws text to a graphics device using an existing font Syntax: Overloads Public Sub DrawString( s As String, font As Font, brush As Brush, x As Single, y As Single ) Overloads Public Sub DrawString( s As String, font As Font, brush As Brush, x As Single, y As Single, format As StringFormat )

56 DrawString (Example) Draw a string using the previously created font named fntSplash, and a green brush Upper-left corner is 5, 265 Assume graCurrent references an instance of the Graphics class graCurrent.DrawString( _ "Copyright 2003 – Course Technology", _ fntSplash, Brushes.Green, 5, 265)

57 The Timer Control Fires a Tick event at regular intervals based on the setting of the Interval property Control is not visible at run time so the control instance appears in resizable tray at the bottom of the form Properties Boolean Enabled property controls whether Tick event fires Interval property controls number of milliseconds between Tick events Events Tick event fires each Interval

58 Displaying, Closing, and Hiding Forms
Call the Show method to display a form Call the ShowDialog method to display a form as a modal dialog box Call the Hide method to hide a form but leave the form in memory Call the Close method to close the form and remove it from memory

59 Introduction to Report Design
Just as designing forms is important so too is report design Reports should have a title that is centered on the page Column headers and columns should be aligned Decimal points should be aligned in column Evenly space columns

60 Completed Report

61 The PrintPreviewDialog Control (1)
Create a report image on the desktop which can be sent to the printer Standard prefix is "ppd" Buttons allow zooming and navigation from one page to another Use the same drawing methods that you used to draw graphics to a form

62 The PrintPreviewDialog Control (2)
Properties Text property appears in the title bar Document property defines the document to preview Set property to instance of the PrintDocument control Location property defines the upper-left corner of the visible region MinimizeBox and MaximizeBox properties control the buttons that appear on the title bar Top, Left, Height, and Width properties define the position and size of the control instance Method ShowDialog method displays the dialog box

63 The PrintDocument Control
Works in conjunction with the PrintPreviewDialog control Standard prefix is "pd" Provides the reference to the Graphics object Similar to the way the Paint event supplies a reference to the Graphics class

64 The PrintDocument Control (Properties and Methods)
PrinterSettings property contains a reference to a PrinterSettings object Object contains information about the printer Events BeginPrint event fires before first page is printed EndPrint event fires after last page has printed PrintPage event occurs for each page printed

65 Printing Coordinates (1)
Piece of paper has a fixed size (typically 8 ½ x 11 inches) Upper-left corner has (x, y) coordinates of (0, 0) Lower-right corner has (x, y) coordinates of (850, 1100) Unit of measure is 1/100 of an inch

66 Printing Coordinates (2)
x-axis 0,0 850,0 y-axis 0,1100 850,1100

67 Handling Printing Events
PrintPage event applies to the PrintDocument class Supplies a reference to the PrintPageEventArgs class PrintPageEventArgs class, in turn supplies a reference to the Graphics object

68 PrintPageEventArgs class
Properties HasMorePages defines whether additional pages need to be printed Set property to false when finished printing MarginBounds property defines the area inside the page margin PageBounds defines the size of the paper PageSettings defines general printer characteristics such as whether paper orientation

69 The StringFormat Class
Applies to the System.Drawing namespace Alignment property controls how text is aligned in a rectangle Trimming property defines how characters are truncated If set to Character, text is trimmed to the nearest character If set to Word, then the characters are trimmed to the nearest word


Download ppt "Repetition and Multiple Forms"

Similar presentations


Ads by Google