1.3k likes | 1.42k Views
14. Graphical User Interface Concepts: Part 2. I claim not to have controlled events, but confess plainly that events have controlled me. Abraham Lincoln. Capture its reality in paint! Paul C é zanne.
E N D
14 • Graphical User Interface • Concepts: Part 2
I claim not to have controlled events, but confess plainly that events have controlled me. Abraham Lincoln • Capture its reality in paint! • Paul Cézanne • An actor entering through the door, you’ve got nothing. But if he enters through the window, you’ve got a situation. • Billy Wilder
But, soft! what light through yonder window breaks? It is the east, and Juliet is the sun! William Shakespeare
OBJECTIVES In this chapter you will learn: • To create menus, tabbed windows and multiple document interface (MDI) programs. • To use the ListView and TreeView controls for displaying information. • To create hyperlinks using the LinkLabel control. • To display lists of information in ListBox and ComboBox controls. • To input date and time data with the DateTimePicker. • To create custom controls.
14.1 Introduction • 14.2 Menus • 14.3 MonthCalendar Control • 14.4 DateTimePicker Control • 14.5 LinkLabel Control • 14.6 ListBox Control • 14.7 CheckedListBox Control • 14.8 ComboBox Control • 14.9 TreeView Control • 14.10 ListView Control • 14.11 TabControl Control • 14.12 Multiple Document Interface (MDI) Windows • 14.13 Visual Inheritance • 14.14 User-Defined Controls • 14.15 Wrap-Up
14.1 Introduction • Continue study of GUIs • Menus • Logically organized commands • LinkLabels • Enable user to visit one of several destinations • ListBox • ComboBoxes • TreeView • Display data hierarchically • TabControl • MDI windows • Multiple document interface
14.2 Menus • Menus • Groups of related commands for Windows applications • Integral part of GUIs • Organize commands without “cluttering” the GUI • All menu items can have access shortcuts or hotkeys • Type an ampersand (&) before the character to be underlined • Set the ShortcutKeys property of the appropriate ToolStripMenuItems • Non-top-level menus can also have shortcut keys • Menu items can display check marks • Indicates that multiple options on the menu can be selected at once
Shortcut key Submenu Menu Menu items Disabled command Checked menu item Seperator bar Fig. 14.1| Menus, submenus and menu items
Type menu name in TextBox Main menu bar MenuStrip icon Fig. 14.2 | Editing menus in Visual Studio.
Place & character before a letter to underline it in the menu, so the character can be used as an access shortcut TextBoxes for adding items to the menu Fig. 14.3| Adding ToolStripMenuItems to a MenuStrip.
Look-and-Feel Observation 14.1 • Buttons can have access shortcuts. Place the & symbol immediately before the desired character in the Button’s label. To press the button by using its access key in the running application, the user presses Alt and the underlined character.
Setting modifier keys Select key (modifier and key combination specifies the shortcut key for the menu item) Fig. 14.4| Setting a menu item’s shortcut keys.
Menu item options Fig. 14.5| Menu item options.
Fig. 14.6 | MenuStrip and ToolStripMenuItem properties and an event. (Part 1 of 2.)
Fig. 14.6 | MenuStrip and ToolStripMenuItem properties and an event. (Part 2 of 2.)
Look-and-Feel Observation 14.2 • It is a convention to place an ellipsis ( ) after the name of a menu item that when selected, displays a dialog (e.g. Save As...). Menu items that produce an immediate action without prompting the user for more information (e.g. Save) should not have an ellipsis following their name.
Outline MenuTestForm.cs (1 of 8) Event handler for aboutToolStripMenuItem for when it is selected Provide information through a MessageBox Event handler for exitToolStripMenuItem for when it is selected Terminates program
Outline Clears all checkmarks next to the menu items MenuTestForm.cs (2 of 8) Change the color and check the corresponding menu item
Outline MenuTestForm.cs (3 of 8) Change the color and check the corresponding menu item Change the color and check the corresponding menu item
Outline MenuTestForm.cs (4 of 8) Change the color and check the corresponding menu item Clears all checkmarks next to the menu items
Outline MenuTestForm.cs (5 of 8) Change the font style and check the corresponding menu item Change the font style and check the corresponding menu item
Outline MenuTestForm.cs (6 of 8) Change the font style and check the corresponding menu item Bold the current font style if it is not already; else, un-bold it
(a) (b) Outline MenuTestForm.cs (7 of 8) Italicize the current font style if it is not already; else, un-italicize it
(d) (c) (e) (f) Outline MenuTestForm.cs (8 of 8)
Software Engineering Observation 14.1 • The mutual exclusion of menu items is not enforced by the MenuStrip, even when the Checked property is true. You must program this behavior.
14.3 MonthCalendar Control • The .NET Framework provides two controls that allow an application to retrieve date and time information • MonthCalendar • Displays a monthly calendar on the Form • The default event for this control is DateChanged • Generated when a new date is selected • DateTimePicker(Section 14.4)
Current day is outlined Selected day is highlighted Fig. 14.8| MonthCalendar control.
14.4 DateTimePicker Control • DateTimePicker • Displays the calendar when a down arrow is selected • Retrieve date and time information from user • More customizable than a MonthCalendar • More properties are provided to edit the look and feel of the drop-down calendar • ValueChanged event • Occurs when user selects a date
Fig. 14.10 | DateTimePicker properties and an event. (Part 1 of 2.)
Fig. 14.10 | DateTimePicker properties and an event. (Part 2 of 2.)
Outline DateTimePickerForm.cs (1 of 3) Retrieve the date selected by the user Update outputLabel: Print the 3rd day after user’s selected date Update outputLabel: Print the 2nd day after user’s selected date
(a) (b) Outline The “minimum” date that can be selected is “today” DateTimePickerForm.cs (2 of 3) The “maximum” date that can be selected is a year from “today”
(c) (d) Outline DateTimePickerForm.cs (3 of 3)
14.5 LinkLabel Control • LinkLabel • Displays links to other resources • Ex: files or Web pages • LinkClicked event • Generated when LinkLabel clicked • Class LinkLabel is derived from class Label • Inherits all of class Label’s functionality Note: @ symbol before a string indicates that all characters should be interpreted literally • Class Process • Method Start • Allows you to execute other programs from an application
Look-and-Feel Observation 14.3 • A LinkLabel is the preferred control for indicating that the user can click a link to jump to a resource such as a Web page, though other controls can perform similar tasks.
LinkLabel on a Form Hand image displays when mouse moves over LinkLabel Fig. 14.12| LinkLabel control in running program.
Fig. 14.13 | LinkLabelproperties and an event. (Part 1 of 2.)
Fig. 14.13 | LinkLabelproperties and an event. (Part 2 of 2.)
Outline LinkLabelTestForm.cs (1 of 4) Change the color of the link to show that it has already been clicked before Open the C: folder
Outline Change the color of the link to show that it has already been clicked before LinkLabelTestForm.cs (2 of 4) Open Internet Explorer and visit the site Change the color of the link to show that it has already been clicked before Open new document in NotePad
Click first LinkLabel to look at contents of C: drive Outline LinkLabelTestForm.cs (3 of 4)
Click second Linklabel to go to Deitel Web site Click on third Linklabel to open Notepad Outline LinkLabelTestForm.cs (4 of 4)
14.6 ListBox Control • ListBox • Allows user to view and select from multiple items in a list • Static GUI entities • To add and remove items • Call the Add method • Call the AddRange method • Add an array of objects • Call the RemoveAt method • Call the Clear method • Remove all entries • Use the String Collection Editor
ListBox Selected items Scrollbars appear if necessary Checked item CheckedListBox Fig. 14.15| ListBox and CheckedListBox on a Form.
Fig. 14.16 | ListBox properties, methods and an event. (Part 1 of 2.)
Fig. 14.16 | ListBox properties, methods and an event. (Part 2 of 2.)
Outline ListBoxTestForm.cs (1 of 3) Add user’s input as a new element of displayListBox
Outline Remove the selected element from displayListBox ListBoxTestForm.cs (2 of 3) Remove every element from displayListBox Terminate program