34. The DrawString Method General Form Examples DrawString(StringToPrint, Font, Brush, Xcoordinate, Ycoordinate) e.Graphics.DrawString(printLineString, printFont, Brushes.Black, _ horizontalPrintLocationSingle, verticalPrintLocationSingle) e.Graphics.DrawString("My text string", myFont, Brushes.Black, _ 100.0, 100.0) e.Graphics.DrawString(nameTextBox.Text, New Font("Arial", 10), _ Brushes.Red, leftMarginSingle, currentLineSingle)
35.
36.
37.
38.
39. Aligning Decimal Columns Code Example (1 of 2) ' SizeF structure for font size info. Dim fontSizeF As New SizeF( ) ' Set X for left-aligned column. horizontalPrintLocationSingle = 200 ' Set ending position for right-aligned column. columnEndSingle = 500 ' Format the number. formattedOutputString= amountDecimal.ToString("C") ' Calculate the X position of the amount. ' Measure string in this font. fontSizeF= e.Graphics.MeasureString(formattedOutputString, _ printFont)
40. Aligning Decimal Columns Code Example (2 of 2) ' SizeF structure for font size info (cont). ' Subtract width of string from the column position. columnXSingle = columnEndSingle - fontSizeF.Width ' Set up the line--each element separately. e.Graphics.DrawString("The Amount = ", printFont, _ Brushes.Black, horizontalPrintLocationSingle, _ verticalPrintLocationSingle) e.Graphics.DrawString(formattedOutputString, printFont, _ Brushes.Black, columnXSingle, verticalPrintLocationSingle) ' Increment line for next line. verticalPrintLocationSingle += lineHeightSingle
Brief description on how to navigate within this presentation (ppt) The first time a Key Term from the chapter is used in the ppt it will display in blue Gold colored text boxes display coding examples Slides will be numbered (# of #) when multiple slides on same topic (Slide title) Speaker notes are included where appropriate for slides (*)Denotes either a comment for page reference to textbook or slide reference in ppt
Often there is a want to offer the user a list of items from which to choose-the Windows ListBox and ComboBox controls (key term) can be used to display lists on a form Items may be added to a list during design time, run time, or perhaps a combination of both
When adding a list control to a form, choose the style according to the space that is available and the box is to operate At design time, the behavior of list boxes and combo boxes differs—for list boxes VB displays the Name property in the control; for combo boxes the Text property displays which is blank by default Combo boxes have a Text property which can be set at design time; List boxes also have a Text property it can only be accessed at run time
Items in a collection can be referred to by an index, which is zero based—for example, if a collection holds 10 items, the indexes to refer to the items range from 0 to 9; the first tiem in the Items collection would be Item 0
Several methods can be used to fill the Items collection of a List box and Combo box. If the list contents are known at design time and the list never changes, it can be defined in the Properties window If items must be added to the list during program execution the Items.Add or Items.Insert method (key term) in an event procedure is used
Items to add can be a variable, a constant, the contents of the Text box at the top of a Combo box, or the Text property of another control
The index position is zero based-to insert a new item in the first position, use index position = 0
The SelectedIndex Property is used when a project is running and the user selects (highlights) an item from the list
Used to display one item from a list-refer to one element of the Items collection Specify which element is wanted by including an index This technique can be useful if needed to display a list item in a label or on another form
In addition to removing individual items at run time, items can also be cleared from a list
The computer is capable of repeating a group of instructions many times without calling the procedure for each new set of data—the process is called looping
*Next slide shows examples of prettest and posttest loops
Boolean variables can be very useful when setting and testing conditions for a loop Can be used to search through a list for a specific value Three step process-dimension a variable; set its initial value and then set a variable to True
The For/Next loop uses the For and Next statements as well as a counter variable called the loop index
The next slide shows a For/Next loop diagram
If in an endless loop that can be caused by changing the value of a loop index variable the program execution may need to be broken manually
There are several techniques used to make the text in a text box or list appear selected
.NET PrintDocument and PrintPreviewDialog components can be used to produce output for the printer and also to preview the output on the screen
The logic for the actual printing belongs in the PrintDocument’s PrintPage event procedure (Key term)
Exact locations needs to be specified for each element of the graphics pages that is to be printed You can specify the upper-left corner of any element by giving its X and Y coordinates, or by using a Point structure or a Rectangle structure You can use multiple PrintDocument objects if you have more than one type of output or report Each PrintDocument has its own PrintPage event Code the graphics commands to precisely print the page in each document’s PrintPage event procedure
The next slide displays the General Form and examples of the DrawString Method
If printing a selected Item from a List—the Text property holds the selected item and can be used to print that selected item
The next 2 slides display an example of sample code for Aligning Decimal Columns
A really great new feature of the new VB.NET printing model is print preview It’s easy to view the printer’s output on the screen and then decide to print or cancel *The next slide displays the PrintPreview Dialog Component