Download presentation
Presentation is loading. Please wait.
Published byMason Sowell Modified over 9 years ago
1
2006 Pearson Education, Inc. All rights reserved. 1 14 Graphical User Interface Concepts: Part 2
2
2006 Pearson Education, Inc. All rights reserved. 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
3
2006 Pearson Education, Inc. All rights reserved. 3 But, soft! what light through yonder window breaks? It is the east, and Juliet is the sun! — William Shakespeare
4
2006 Pearson Education, Inc. All rights reserved. 4 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.
5
2006 Pearson Education, Inc. All rights reserved. 5 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
6
2006 Pearson Education, Inc. All rights reserved. 6 14.1 Introduction Continue study of GUIs – Menu s Logically organized commands – LinkLabel s Enable user to visit one of several destinations – ListBox – ComboBox es – TreeView Display data hierarchically – TabControl – MDI windows Multiple document interface
7
2006 Pearson Education, Inc. All rights reserved. 7 14.2 Menu s Menu s – 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
8
2006 Pearson Education, Inc. All rights reserved. 8 Fig. 14.1 | Menus, submenus and menu items Seperator bar Checked menu item Disabled command Shortcut key Submenu Menu items Menu
9
2006 Pearson Education, Inc. All rights reserved. 9 Fig. 14.2 | Editing menus in Visual Studio. Main menu bar MenuStrip icon Type menu name in TextBox
10
2006 Pearson Education, Inc. All rights reserved. 10 Fig. 14.3 | Adding ToolStripMenuItems to a MenuStrip. TextBoxes for adding items to the menu Place & character before a letter to underline it in the menu, so the character can be used as an access shortcut
11
2006 Pearson Education, Inc. All rights reserved. 11 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.
12
2006 Pearson Education, Inc. All rights reserved. 12 Fig. 14.4 | Setting a menu item’s shortcut keys. Setting modifier keys Select key (modifier and key combination specifies the shortcut key for the menu item)
13
2006 Pearson Education, Inc. All rights reserved. 13 Fig. 14.5 | Menu item options. Menu item options
14
2006 Pearson Education, Inc. All rights reserved. 14 Fig. 14.6 | MenuStrip and ToolStripMenuItem properties and an event. (Part 1 of 2.)
15
2006 Pearson Education, Inc. All rights reserved. 15 Fig. 14.6 | MenuStrip and ToolStripMenuItem properties and an event. (Part 2 of 2.)
16
2006 Pearson Education, Inc. All rights reserved. 16 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.
17
2006 Pearson Education, Inc. All rights reserved. 17 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
18
2006 Pearson Education, Inc. All rights reserved. 18 Outline MenuTestForm.cs (2 of 8) Clears all checkmarks next to the menu items Change the color and check the corresponding menu item
19
2006 Pearson Education, Inc. All rights reserved. 19 Outline MenuTestForm.cs (3 of 8) Change the color and check the corresponding menu item
20
2006 Pearson Education, Inc. All rights reserved. 20 Outline MenuTestForm.cs (4 of 8) Change the color and check the corresponding menu item Clears all checkmarks next to the menu items
21
2006 Pearson Education, Inc. All rights reserved. 21 Outline MenuTestForm.cs (5 of 8) Change the font style and check the corresponding menu item
22
2006 Pearson Education, Inc. All rights reserved. 22 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
23
2006 Pearson Education, Inc. All rights reserved. 23 (a) (b) Outline MenuTestForm.cs (7 of 8) Italicize the current font style if it is not already; else, un-italicize it
24
2006 Pearson Education, Inc. All rights reserved. 24 Outline MenuTestForm.cs (8 of 8) (c) (d) (e) (f)
25
2006 Pearson Education, Inc. All rights reserved. 25 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.
26
2006 Pearson Education, Inc. All rights reserved. 26 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)
27
2006 Pearson Education, Inc. All rights reserved. 27 Fig. 14.8 | MonthCalendar control. Current day is outlined Selected day is highlighted
28
2006 Pearson Education, Inc. All rights reserved. 28 Fig. 14.9 | MonthCalendar properties and an event.
29
2006 Pearson Education, Inc. All rights reserved. 29 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
30
2006 Pearson Education, Inc. All rights reserved. 30 Fig. 14.10 | DateTimePicker properties and an event. (Part 1 of 2.)
31
2006 Pearson Education, Inc. All rights reserved. 31 Fig. 14.10 | DateTimePicker properties and an event. (Part 2 of 2.)
32
2006 Pearson Education, Inc. All rights reserved. 32 Outline DateTimePickerForm.cs (1 of 3) Retrieve the date selected by the user Update outputLabel : Print the 3 rd day after user’s selected date Update outputLabel : Print the 2 nd day after user’s selected date
33
2006 Pearson Education, Inc. All rights reserved. 33 Outline DateTimePickerForm.cs (2 of 3) (a)(b) The “minimum” date that can be selected is “today” The “maximum” date that can be selected is a year from “today”
34
2006 Pearson Education, Inc. All rights reserved. 34 Outline DateTimePickerForm.cs (3 of 3) (c)(d)
35
2006 Pearson Education, Inc. All rights reserved. 35 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
36
2006 Pearson Education, Inc. All rights reserved. 36 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.
37
2006 Pearson Education, Inc. All rights reserved. 37 Fig. 14.12 | LinkLabel control in running program. Hand image displays when mouse moves over LinkLabel LinkLabel on a Form
38
2006 Pearson Education, Inc. All rights reserved. 38 Fig. 14.13 | LinkLabel properties and an event. (Part 1 of 2.)
39
2006 Pearson Education, Inc. All rights reserved. 39 Fig. 14.13 | LinkLabel properties and an event. (Part 2 of 2.)
40
2006 Pearson Education, Inc. All rights reserved. 40 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
41
2006 Pearson Education, Inc. All rights reserved. 41 Outline LinkLabelTestForm. cs (2 of 4) Change the color of the link to show that it has already been clicked before 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
42
2006 Pearson Education, Inc. All rights reserved. 42 Outline LinkLabelTestForm. cs (3 of 4) Click first LinkLabel to look at contents of C: drive
43
2006 Pearson Education, Inc. All rights reserved. 43 Outline LinkLabelTestForm. cs (4 of 4) Click second Linklabel to go to Deitel Web site Click on third Linklabel to open Notepad
44
2006 Pearson Education, Inc. All rights reserved. 44 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
45
2006 Pearson Education, Inc. All rights reserved. 45 Fig. 14.15 | ListBox and CheckedListBox on a Form. CheckedListBox ListBox Scrollbars appear if necessary Selected items Checked item
46
2006 Pearson Education, Inc. All rights reserved. 46 Fig. 14.16 | ListBox properties, methods and an event. (Part 1 of 2.)
47
2006 Pearson Education, Inc. All rights reserved. 47 Fig. 14.16 | ListBox properties, methods and an event. (Part 2 of 2.)
48
2006 Pearson Education, Inc. All rights reserved. 48 Fig. 14.17 | String Collection Editor.
49
2006 Pearson Education, Inc. All rights reserved. 49 Outline ListBoxTestForm.cs (1 of 3) Add user’s input as a new element of displayListBox
50
2006 Pearson Education, Inc. All rights reserved. 50 Outline ListBoxTestForm.cs (2 of 3) Remove the selected element from displayListBox Remove every element from displayListBox Terminate program
51
2006 Pearson Education, Inc. All rights reserved. 51 Outline ListBoxTestForm.cs (3 of 3) (a) (b) (c) (d)
52
2006 Pearson Education, Inc. All rights reserved. 52 14.7 CheckListBox Control CheckListBoxes – Extend ListBox – Include CheckBox es next to each item in the list – Multiple items can be selected – SelectionMode property Only possible values are None and One – Give the user multiple selection or no selection at all Behavior consistent with that of CheckBox es – Event ItemCheck Occurs whenever a user checks or unchecks a CheckedListBox item
53
2006 Pearson Education, Inc. All rights reserved. 53 Common Programming Error 14.1 The IDE displays an error message if you attempt to set the SelectionMode property to MultiSimple or MultiExtended in the Properties window of a CheckedListBox. If this value is set programmatically, a runtime error occurs.
54
2006 Pearson Education, Inc. All rights reserved. 54 Fig. 14.19 | CheckedListBox properties, methods and events. (Part 1 of 2.)
55
2006 Pearson Education, Inc. All rights reserved. 55 Fig. 14.19 | CheckedListBox properties, methods and events. (Part 2 of 2.)
56
2006 Pearson Education, Inc. All rights reserved. 56 CheckedListBoxTest Form.cs (1 of 3) Outline Retrieve the selected element as a string Add the retrieved element to displayListBox Remove the retrieved element to displayListBox
57
2006 Pearson Education, Inc. All rights reserved. 57 Outline CheckedListBoxTest Form.cs (2 of 3) (a) (b)
58
2006 Pearson Education, Inc. All rights reserved. 58 Outline (c) (d) CheckedListBoxTest Form.cs (3 of 3)
59
2006 Pearson Education, Inc. All rights reserved. 59 14.8 ComboBox Control ComboBox – Combines TextBox features with a drop-down list – Contains a list from which a value can be selected – User can enter text into the TextBox – User can click the down arrow to select from a list of predefined items
60
2006 Pearson Education, Inc. All rights reserved. 60 Fig. 14.21 | ComboBox demonstration. Click the down arrow to display items in drop-down list Selecting an item from drop-down list changes text in TextBox portion
61
2006 Pearson Education, Inc. All rights reserved. 61 Look-and-Feel Observation 14.4 Use a ComboBox to save space on a GUI. A disadvantage is that, unlike with a ListBox, the user cannot see available items without expanding the drop-down list.
62
2006 Pearson Education, Inc. All rights reserved. 62 Fig. 14.22 | ComboBox properties and an event. (Part 1 of 2.)
63
2006 Pearson Education, Inc. All rights reserved. 63 Fig. 14.22 | ComboBox properties and an event. (Part 2 of 2.)
64
2006 Pearson Education, Inc. All rights reserved. 64 Outline comboBoxTextForm.Cs (1 of 3) Create all the necessary objects for drawing
65
2006 Pearson Education, Inc. All rights reserved. 65 Outline comboBoxTextForm.Cs (2 of 3) Property that returns the index of the selected element Chooses and draw the appropriate shape based on the user’s selection
66
2006 Pearson Education, Inc. All rights reserved. 66 Outline comboBoxTextForm.Cs (3 of 3) (a) (b) (c)(d)
67
2006 Pearson Education, Inc. All rights reserved. 67 Look-and-Feel Observation 14.5 Make lists (such as ComboBoxes ) editable only if the program is designed to accept user-submitted elements. Otherwise, the user might try to enter a custom item that is improper for the purposes of your application.
68
2006 Pearson Education, Inc. All rights reserved. 68 14.9 TreeView Control TreeView – Displays nodes hierarchically in a tree Nodes are objects that contain values and can refer to other nodes – A parent node contains child nodes – Child nodes can be parents to other nodes – Child nodes with the same parent node are considered sibling nodes A tree is a collection of nodes organized in a hierarchical manner – The first parent node of a tree is the root node – The nodes in a TreeView are instances of class TreeNode Each TreeNode has a Nodes collection – Contains a list of other TreeNode s Parent property – Returns a reference to the parent node – Returns null if the node is a root node
69
2006 Pearson Education, Inc. All rights reserved. 69 Fig. 14.24 | TreeView displaying a sample tree. Click + to expand node and display child nodes Root node Click - to collapse node and hide child nodes Child nodes (of Manager2)
70
2006 Pearson Education, Inc. All rights reserved. 70 Fig. 14.25 | TreeView properties and an event.
71
2006 Pearson Education, Inc. All rights reserved. 71 Fig. 14.26 | TreeNode properties and methods. (Part 1 of 2.)
72
2006 Pearson Education, Inc. All rights reserved. 72 Fig. 14.26 | TreeNode properties and methods. (Part 2 of 2.)
73
2006 Pearson Education, Inc. All rights reserved. 73 14.9 TreeView Control (Cont.) TreeView – Adding nodes TreeNode Editor Call method Add
74
2006 Pearson Education, Inc. All rights reserved. 74 Fig. 14.27 | TreeNode Editor. Delete current node
75
2006 Pearson Education, Inc. All rights reserved. 75 Outline TreeViewDirectoryS tructureForm.cs (1 of 4) An array of strings representing directories
76
2006 Pearson Education, Inc. All rights reserved. 76 Outline TreeViewDirectoryS tructureForm.cs (2 of 4) Create an object of TreeNode for the current directory Append the current directory to the parent directory Continue process for the rest of the subdirectories
77
2006 Pearson Education, Inc. All rights reserved. 77 Outline TreeViewDirectoryS tructureForm.cs (3 of 4) Unauthorized access of directories Check to see if the user specified directory exist or not Append directory to directoryTreeView Add the subdirectories
78
2006 Pearson Education, Inc. All rights reserved. 78 Outline TreeViewDirectoryS tructureForm.cs (4 of 4) (a) (b) Notify user that the directory does not exist
79
2006 Pearson Education, Inc. All rights reserved. 79 14.10 ListView Control ListView – Similar to ListBox Both display lists from which the user can select one or more items – Difference between the two classes is that a ListView can display icons next to the list items Allows you to define the images used as icons for ListView items – To display images, an ImageList component is required
80
2006 Pearson Education, Inc. All rights reserved. 80 Fig. 14.29 | ListView properties and an event. (Part 1 of 2.)
81
2006 Pearson Education, Inc. All rights reserved. 81 Fig. 14.29 | ListView properties and an event. (Part 2 of 2.)
82
2006 Pearson Education, Inc. All rights reserved. 82 Fig. 14.30 | Image Collection Editor window for an ImageList component.
83
2006 Pearson Education, Inc. All rights reserved. 83 Outline ListViewTestForm.cs (1 of 7) Retrieve the current project’s directory
84
2006 Pearson Education, Inc. All rights reserved. 84 Outline ListViewTestForm.cs (2 of 7) Get the number of selected items Load the parent’s directory Create an object that holds the information for the directory
85
2006 Pearson Education, Inc. All rights reserved. 85 Outline ListViewTestForm.cs (3 of 7) Load the selected directory Update displayLabel with the current directory
86
2006 Pearson Education, Inc. All rights reserved. 86 Outline ListViewTestForm.cs (4 of 7) Remove all elements from browserListView Add an element to go back to the parent directory Update the current directory information Store all of the current directory’s files and subdirectories Add all the current directory’s subdirectories to ListView Specify which image to use for the directories
87
2006 Pearson Education, Inc. All rights reserved. 87 Outline ListViewTestForm.cs (5 of 7) Add all the current directory’s files to ListView Specify which image to use for the files Notify user of unauthorized access
88
2006 Pearson Education, Inc. All rights reserved. 88 Outline ListViewTestForm.cs (6 of 7) (a) Specify the images associated with directories and files Initially, load program with the current project’s directory
89
2006 Pearson Education, Inc. All rights reserved. 89 Outline ListViewTestForm.cs (7 of 7) (b) (c)
90
2006 Pearson Education, Inc. All rights reserved. 90 Software Engineering Observation 14.2 When designing applications that run for long periods of time, you might choose a large initial delay to improve performance throughout the rest of the program. However, in applications that run for only short periods of time, developers often prefer fast initial loading times and small delays after each action.
91
2006 Pearson Education, Inc. All rights reserved. 91 14.11 TabControl Control TabControl – Creates tabbed windows – Specify more information in the same amount of space – Contain TabPage objects Add controls to the TabPage objects Add the TabPage s to the TabControl Only one TabPage is displayed at a time To view different TabPage s click the appropriate tab – Click event Generated when TabPage ’s tab is clicked
92
2006 Pearson Education, Inc. All rights reserved. 92 Fig. 14.32 | Tabbed windows in Visual Studio. Tab windows
93
2006 Pearson Education, Inc. All rights reserved. 93 Fig. 14.33 | TabControl with TabPages example. TabPage Controls in TabPage TabControl
94
2006 Pearson Education, Inc. All rights reserved. 94 Fig. 14.34 | TabPage s added to a TabControl.
95
2006 Pearson Education, Inc. All rights reserved. 95 Fig. 14.35 | TabControl properties and an event.
96
2006 Pearson Education, Inc. All rights reserved. 96 Outline UsingTabsForm.cs (1 of 4) Change font color of displayLabel to black Change font color of displayLabel to red
97
2006 Pearson Education, Inc. All rights reserved. 97 Outline UsingTabsForm.cs (2 of 4) Change font color of displayLabel to green Change font size to 12 Change font size to 16
98
2006 Pearson Education, Inc. All rights reserved. 98 Outline UsingTabsForm.cs (3 of 4) Change font size to 20 Output “Hello!” Output “Goodbye!”
99
2006 Pearson Education, Inc. All rights reserved. 99 Outline UsingTabsForm.cs (4 of 4) (a) (b) (c) (d)
100
2006 Pearson Education, Inc. All rights reserved. 100 Software Engineering Observation 14.3 A TabPage can act as a container for a single logical group of RadioButtons, enforcing their mutual exclusivity. To place multiple RadioButton groups inside a single TabPage, you should group RadioButtons within Panels or GroupBoxes contained within the TabPage.
101
2006 Pearson Education, Inc. All rights reserved. 101 14.12 Multiple Document Interface (MDI) Windows Multiple Document Interface (MDI) – Parent window Main application window Only one in application – Child window Each window inside the application Only one child can be active at a time Cannot also be a parent window As many child windows as you want
102
2006 Pearson Education, Inc. All rights reserved. 102 Fig. 14.37 | MDI parent window and MDI child windows. MDI parent MDI child
103
2006 Pearson Education, Inc. All rights reserved. 103 Fig. 14.38 | SDI and MDI forms. Single Document Interface (SDI) Multiple Document Interface (MDI)
104
2006 Pearson Education, Inc. All rights reserved. 104 14.12 Multiple Document Interface (MDI) Windows (Cont.) Creating an MDI Form – Create a new Form Set its IsMdiContainer property to true – Create a child Form Add the child Form to the parent – Set its MdiParent property to the parent Form Call the child Form ’s Show method – To add a child Form to a parent, write: ChildFormClass childForm = New ChildFormClass(); childForm.MdiParent = parentForm; childForm.Show();
105
2006 Pearson Education, Inc. All rights reserved. 105 Fig. 14.39 | MDI parent and MDI child properties, method and event.
106
2006 Pearson Education, Inc. All rights reserved. 106 Good Programming Practice 14.1 When creating MDI applications, include a menu that displays a list of the open child windows. This helps the user select a child window quickly, rather than having to search for it in the parent window.
107
2006 Pearson Education, Inc. All rights reserved. 107 Fig. 14.40 | Minimized and maximized child windows. (a)(b) Parent window icons: minimize,maximize and close Maximized child window icons: minimize, restore and close Parent title bar indicates maximized child Minimized child window icons: restore, maximize and close
108
2006 Pearson Education, Inc. All rights reserved. 108 Fig. 14.41 | MenuItem property MdiList example. 9 or more child windows enables the More Windows… option Child windows list
109
2006 Pearson Education, Inc. All rights reserved. 109 Fig. 14.42 | MdiLayout enumeration values. (c) TileHorizontal (a) ArrangeIcons (b) Cascade (d) Tilevertical
110
2006 Pearson Education, Inc. All rights reserved. 110 Outline UsingMDIForm.cs (1 of 4) Create a new child form Set this form to be the child form’s parent Show the child form
111
2006 Pearson Education, Inc. All rights reserved. 111 Outline UsingMDIForm.cs (2 of 4) Create another new child form Set this form to be the child form’s parent Show the child form Create another new child form Set this form to be the child form’s parent Show the child form Terminate program
112
2006 Pearson Education, Inc. All rights reserved. 112 Outline UsingMDIForm.cs (3 of 4) Specify how to display the child forms on the MDI parent form
113
2006 Pearson Education, Inc. All rights reserved. 113 Outline UsingMDIForm.cs (4 of 4) (a) (b) (c) (d)
114
2006 Pearson Education, Inc. All rights reserved. 114 Outline ChildForm.cs ChildForm’s constructor Display the image specified by the filename
115
2006 Pearson Education, Inc. All rights reserved. 115 14.13 Visual Inheritance Visual Inheritance – Enables to achieve visual consistency across applications – All Form s created so far derives from class System.Windows.Forms.Form – Derived Form s contains the functionality of its base class Form Includes any base-class properties, methods, variables and controls – The derived class also inherits all visual aspects Ex: sizing, component layout, spacing between GUI components, colors and fonts
116
2006 Pearson Education, Inc. All rights reserved. 116 Outline VisualInheritanceF orm.cs VisualInheritanceForm inherits from Form Display MessageBox
117
2006 Pearson Education, Inc. All rights reserved. 117 Fig. 14.46 | Form demonstrating visual inheritance.
118
2006 Pearson Education, Inc. All rights reserved. 118 Outline VisualInheritanceT estForm.cs (1 of 2) VisualInheritanceFormTest inherits from VisualInheritanceForm Display MessageBox
119
2006 Pearson Education, Inc. All rights reserved. 119 Outline VisualInheritanceT estForm.cs (2 of 2) Derived class cannot modify these controls. Derived class can modify this control.
120
2006 Pearson Education, Inc. All rights reserved. 120 14.14 User-Defined Controls User-Defined Controls –.NET Framework allow you to create custom controls Appear in the user’s Toolbox Can be added to Forms, Panels or GroupBoxes in the same way that we add other predefined controls – The simplest way to create a custom control is to derive a class from an existing control Useful if you want to add functionality to an existing control
121
2006 Pearson Education, Inc. All rights reserved. 121 14.14 User-Defined Controls (Cont.) – Method OnPaint Contained by all controls Called when a component must be redrawn Passed a PaintEventArgs object The derived control’s OnPaint method is called through polymorphism The base class’s OnPaint implementation is not called – Must call it explicitly from the new OnPaint implementation before we execute our custom-paint code Ensure the control will be displayed correctly
122
2006 Pearson Education, Inc. All rights reserved. 122 14.14 User-Defined Controls (Cont.) – UserControl Create a new control composed of existing controls Acts as a container for the controls added to it Contains constituent controls – Controls added to a custom control – Does not determine how these constituent controls are displayed – Method OnPaint of the UserControl Cannot be overridden – To control the appearance of each constituent control, you must handle each control’s Paint event The Paint event handler is passed a PaintEventArgs object
123
2006 Pearson Education, Inc. All rights reserved. 123 14.14 User-Defined Controls (Cont.) – Create a brand new control by inheriting from class Control Does not define any specific behavior Control handles the items associated with all controls – Ex: events and sizing handles Method OnPaint – Should contain a call to the base class’s OnPaint method – Add code that draws custom graphics inside the overridden OnPaint method
124
2006 Pearson Education, Inc. All rights reserved. 124 Fig. 14.48 | Custom control creation.
125
2006 Pearson Education, Inc. All rights reserved. 125 Outline ClockUserControl.cs Update time to displayLabel at every tick event
126
2006 Pearson Education, Inc. All rights reserved. 126 14.14 User-Defined Controls (Cont.) Share custom controls with other developers (Example: UserControl ) – Create a new Class Library project. – Delete Class1.cs, initially provided with the application. – Right click the project in the Solution Explorer and select Add > User Control. – Inside the project, add controls and functionality to the UserControl. – Build the project. – Create a new Windows application. – Right click the ToolBox and select Choose Items. In the Choose Toolbox Items dialog, click Browse. Browse for the.dll file from the class library created. The item will then appear in the Choose Toolbox Items dialog. If it is not already checked, check this item. Click OK to add the item to the Toolbox. This control can now be added to the Form as if it were any other control.
127
2006 Pearson Education, Inc. All rights reserved. 127 Fig. 14.50 | Custom-control creation.
128
2006 Pearson Education, Inc. All rights reserved. 128 Fig. 14.51 | Custom control added to the ToolBox.
129
2006 Pearson Education, Inc. All rights reserved. 129 Fig. 14.52 | Custom control added to a Form. New ToolBox icon Newly inserted control
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.