1 / 82

Chapter 9 – Additional Controls and Objects

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. 9.1 List Boxes and Combo Boxes. A Review of List Box Features Some Additional Features of List Boxes The Combo Box Control

hanley
Télécharger la présentation

Chapter 9 – Additional Controls and Objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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

  3. Fill a List Box at Design Time via its String Collection Editor tasks button click here to invoke string collection editor

  4. String Collection Editor Fill by direct typing or by copying and pasting from a text editor or a spreadsheet.

  5. List Box at Run Time lstMonths selected item The value of lstMonths.Text is the string consisting of the selected item.

  6. Indexes The items in a list box are indexed with zero-based numbering. lstBox.Items(0) lstBox.Items(1) . .

  7. List Box Properties • 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.

  8. 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.

  9. 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.

  10. 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.

  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

  12. Combo Box Styles

  13. Styles at Run Time after Arrows are Clicked The value of cboBox.Text is the contents of the text box at the top of the combo box.

  14. Example 3 Private Sub btnDisplay_Click(...) _ Handles btnDisplay.Click txtDisplay.Text = cboTitle.Text & " " & txtName.Text End Sub txtName cboTitle cboTitle txtDisplay

  15. Helpful Feature of Combo Box

  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

  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.

  18. Example 1: Form txtSeconds

  19. Example 1: Code Private Sub btnStart_Click(...) Handles _ btnStart.Click txtSeconds.Text = "0" 'Reset watch tmrWatch.Enabled = True End Sub Private Sub btnStop_Click(...) Handles btnStop.Click tmrWatch.Enabled = False End Sub Private Sub tmrWatch_Tick(...) Handles tmrWatch.Tick txtSeconds.Text=CStr((CDbl(txtSeconds.Text) + 0.1)) End Sub

  20. Example 1: Output

  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)

  22. Example 2 Private SubbtnSelect_Click(...)Handles _ btnSelect.Click DimrandomNum As NewRandom() Dimnum1, 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

  23. Example 2: Output

  24. 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

  25. Example 3: Form txtRate “ToolTip on ToolTip1” property of txtRate has the setting “Such as 5, 5.25, 5.5 …”

  26. Example 3: Output

  27. 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

  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

  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("")

  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.

  31. Coordinates in a Picture Box Each point in a picture box is identified by a pair of coordinates, (x, y). y pixels (x, y) x pixels

  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)

  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 with diameter d inscribed in it.

  34. Picture Box Containing a Red Circle picBox.CreateGraphics. DrawEllipse(Pens.Red, 35, 35, 70, 70)

  35. Picture Box Properties • A picture can be placed in a picture box control with the Imageproperty. • 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.

  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

  37. The MenuStrip Control Used to create menus like the following: Top-level menu Second-level menu

  38. Menu Events • Each menu item responds to the Click event • Click event is triggered by • the mouse • Alt + access key • Shortcut key

  39. The Horizontal and Vertical Scroll Bars

  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.

  41. Scroll Bar Properties The 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.

  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.

  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

  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.

  45. Add New Items Dialog Box • Select Windows Form from the center pane. • Optionally type in a name. • Press the Add button.

  46. Add New Items Dialog Box (continued) select Windows Form type in name press Add button

  47. Solution Explorer Both forms will be accessible through the Solution Explorer window.

  48. Startup Form • The form that loads when the program starts running is called the startup form. • To designate a form as the startup form • Right-click on the program name at the top of the Solution Explorer. • Click on Properties in the context menu. • Select the form from the “Startup form” drop-down list.

  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

  50. 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.

More Related