chapter 12: using controls
TRANSCRIPT
Chapter 12:
Using Controls
Using a LinkLabel
• LinkLabel
– Similar to a Label
– Provides the additional capability to link the user to other sources• Such as Web pages or files
• Default event
– The method whose shell is automatically created when you double-click the Control
– The method you are most likely to alter with Control
– The event that users most likely expect to generate
2Microsoft Visual C# 2012, Fifth Edition
Using a LinkLabel (cont’d.)
3Microsoft Visual C# 2012, Fifth Edition
• LinkLabel appears as underlined text
– The text is blue by default
• When you pass the mouse pointer over a LinkLabel, the pointer changes to a hand
• When a user clicks a LinkLabel, it generates a click event– Executing a LinkClicked() method
• The LinkVisited property can be set to truewhen you determine that a user has clicked a link
4Microsoft Visual C# 2012, Fifth Edition
Using a LinkLabel (cont’d.)
5Microsoft Visual C# 2012, Fifth Edition
Using a LinkLabel (cont’d.)
6Microsoft Visual C# 2012, Fifth Edition
Using a LinkLabel (cont’d.)
7Microsoft Visual C# 2012, Fifth Edition
Using a LinkLabel (cont’d.)
Using CheckBoxand RadioButtonObjects
• CheckBox objects
– GUI widgets the user can click to select or deselect an option
• RadioButtons
– Similar to CheckBoxes
– Only one RadioButton in a group can be selected at a time
8Microsoft Visual C# 2012, Fifth Edition
Using CheckBoxand RadioButtonObjects (cont’d.)
9Microsoft Visual C# 2012, Fifth Edition
10Microsoft Visual C# 2012, Fifth Edition
Using CheckBoxand RadioButtonObjects (cont’d.)
11Microsoft Visual C# 2012, Fifth Edition
Using CheckBoxand RadioButtonObjects (cont’d.)
12Microsoft Visual C# 2012, Fifth Edition
Using CheckBoxand RadioButtonObjects (cont’d.)
13Microsoft Visual C# 2012, Fifth Edition
Using CheckBoxand RadioButtonObjects (cont’d.)
Understanding GroupBoxes and Panels
• GroupBox or Panel
– Groups related Controls on a Form
– Can be anchored or docked inside a Form
• GroupBoxes
– Can display a caption
– Do not have scroll bars
• Panels
– Cannot display a caption
– Have scroll bars
14Microsoft Visual C# 2012, Fifth Edition
• To create a GroupBox or Panel, drag its icon from the Toolbox onto a Form.
• Then, drag new controls from the Toolbox directly into the GroupBox or Panel.
• To enable the scrollbars, set the Panel’s AutoScroll property to true.
• If the Panel cannot display all of its controls, scrollbars appear (Fig. 14.23).
GroupBoxes and Panels (Cont.)
©1992-2014 by Pearson Education, Inc. All Rights Reserved.
©1992-2014 by Pearson
Education, Inc. All Rights
Reserved.
17
NumericUpDown Control
• NumericUpDown
– Restrict a user’s input choices to a specific range of numeric values.
– Appears as a TextBox, with two small Buttons on the right side
– NumericUpDown’s ReadOnly property indicates if user can type a number into the control
18
NumericUpDown properties and event.
NumericUpDown properties and event Description
Common Properties
Increment Specifies by how much the current number in
the control changes when the user clicks the
control’s up and down arrows.
Maximum Largest value in the control’s range.
Minimum Smallest value in the control’s range.
Value The numeric value currently displayed in the
control.
Common Event
ValueChanged This event is raised when the value in the
control is changed. This is the default event
for the NumericUpDown control.
19
• A ToolTip component appears in the component tray—the region below the Form in Design mode.
• A ToolTip on property for each ToolTip component appears in the Properties window for the Form’s other controls.
• Figure 14.32 demonstrates the ToolTipcomponent.
ToolTips
©1992-2014 by Pearson Education, Inc. All Rights Reserved.
©1992-2014 by Pearson
Education, Inc. All Rights
Reserved.
• Set the tool-tip text for the Labels to “First Label” and “Second Label”
• Figure 14.33 shows the ToolTip in the component tray.
• We set the tool tip text for the first Label to "First Label" and the tool tip text for the second Label to "Second Label".
ToolTips (Cont.)
©1992-2014 by Pearson Education, Inc. All Rights Reserved.
©1992-2014 by Pearson
Education, Inc. All Rights
Reserved.
©1992-2014 by Pearson
Education, Inc. All Rights
Reserved.
Adding a PictureBox to a Form
• PictureBox
– A Control in which you can display graphics from a bitmap, icon, JPEG, GIF, or other image file type
25Microsoft Visual C# 2012, Fifth Edition
26Microsoft Visual C# 2012, Fifth Edition
27Microsoft Visual C# 2012, Fifth Edition
Adding a PictureBox to a Form (cont’d.)
28Microsoft Visual C# 2012, Fifth Edition
Adding a PictureBox to a Form (cont’d.)
Adding ListBox, CheckedListBox, and ComboBoxControls to a Form
• ListBox, ComboBox, and CheckedListBoxobjects
– List-type widgets that descend from ListControl
• ListBox Control
– Displays a list of items the user can select by clicking
– Allows the user to make a single selection or multiple selections by setting the SelectionMode property
– SelectedItem property
• Contains the value of the item a user has selected
29Microsoft Visual C# 2012, Fifth Edition
30
Adding ListBox, CheckedListBox, and ComboBoxControls to a Form (cont’d.)
Microsoft Visual C# 2012, Fifth Edition
31Microsoft Visual C# 2012, Fifth Edition
Adding ListBox, CheckedListBox, and ComboBoxControls to a Form (cont’d.)
32Microsoft Visual C# 2012, Fifth Edition
Adding ListBox, CheckedListBox, and ComboBoxControls to a Form (cont’d.)
33Microsoft Visual C# 2012, Fifth Edition
Adding ListBox, CheckedListBox, and ComboBoxControls to a Form (cont’d.)
34Microsoft Visual C# 2012, Fifth Edition
Adding ListBox, CheckedListBox, and ComboBoxControls to a Form (cont’d.)
• ComboBox Control
– Similar to a ListBox
– Displays an additional editing field• Allows the user to select from the list or to enter new text
• CheckedListBox Control
– Similar to a ListBox
– Check boxes appear to the left of each desired item
35Microsoft Visual C# 2012, Fifth Edition
Adding ListBox, CheckedListBox, and ComboBoxControls to a Form (cont’d.)
36Microsoft Visual C# 2012, Fifth Edition
Adding ListBox, CheckedListBox, and ComboBoxControls to a Form (cont’d.)
Adding MonthCalendarand DateTimePickerControls to a Form
• MonthCalendar and DateTimePickerControls
– Allow you to retrieve date and time information
37Microsoft Visual C# 2012, Fifth Edition
38Microsoft Visual C# 2012, Fifth Edition
Adding MonthCalendarand DateTimePickerControls to a Form (cont’d.)
39Microsoft Visual C# 2012, Fifth Edition
Adding MonthCalendarand DateTimePickerControls to a Form (cont’d.)
40Microsoft Visual C# 2012, Fifth Edition
Adding MonthCalendarand DateTimePickerControls to a Form (cont’d.)
41Microsoft Visual C# 2012, Fifth Edition
Adding MonthCalendarand DateTimePickerControls to a Form (cont’d.)
• DateTimePicker Control
– Displays a month calendar when the down arrow is selected
42Microsoft Visual C# 2012, Fifth Edition
Adding MonthCalendarand DateTimePickerControls to a Form (cont’d.)
43Microsoft Visual C# 2012, Fifth Edition
Adding MonthCalendarand DateTimePickerControls to a Form (cont’d.)
44Microsoft Visual C# 2012, Fifth Edition
Adding MonthCalendarand DateTimePickerControls to a Form (cont’d.)
Adding a MenuStrip to a Form
• Menu strip
– A horizontal list of general options that appears under the title bar of a Form or Window
• You can add a MenuStrip Control object to any Form you create
• When you double-click an entry in the MenuStrip, a Click() method is generated
45Microsoft Visual C# 2012, Fifth Edition
Adding a MenuStrip to a Form (cont’d.)
46Microsoft Visual C# 2012, Fifth Edition
Adding a MenuStrip to a Form (cont’d.)
47Microsoft Visual C# 2012, Fifth Edition
48Microsoft Visual C# 2012, Fifth Edition
Adding a MenuStrip to a Form (cont’d.)
• The TreeView control displays nodes hierarchically in a tree.
• A parent node contains child nodes, and the child nodes can be parents to other nodes.
• Two child nodes that have the same parent node are considered sibling nodes.
• The first parent node of a tree is the root node (a TreeView can have multiple roots).
TreeView Control
©1992-2014 by Pearson Education, Inc. All Rights Reserved.
• The nodes in a TreeView (Fig. 15.24) are instances of class TreeNode.
• Each TreeNode has a Nodes collection(type TreeNodeCollection), which contains a list of its children.
TreeView Control (Cont.)
©1992-2014 by Pearson Education, Inc. All Rights Reserved.
©1992-2014 by Pearson
Education, Inc. All Rights
Reserved.
• To add nodes to the TreeView visually, click the ellipsis next to the Nodes property in the Properties window.
• This opens the TreeNode Editor (Fig. 15.27).
TreeView Control (Cont.)
©1992-2014 by Pearson Education, Inc. All Rights Reserved.
©1992-2014 by Pearson
Education, Inc. All Rights
Reserved.
• To add nodes programmatically, first create a root node.
treeView1.Nodes.Add( );
• To add children to a root node first select the appropriate root node:
treeView1.selectedNode
• To remove nodes programmatically, first select a node, then remove.
treeView1.Nodes.Remove( );
TreeView Control (Cont.)
©1992-2014 by Pearson Education, Inc. All Rights Reserved.
TreeView Control (Example)
• TabControls contain TabPage objects, which are similar to Panels.
• Add TabControls visually by dragging and dropping them onto a Form in Design mode.
• To add TabPages in Design mode, click the top of the TabControl, open its smart tasks menu and select Add Tab (Fig. 15.34).
• To select a TabPage, click the control area underneath the tabs.
TabControl Control
©1992-2014 by Pearson Education, Inc. All Rights Reserved.
©1992-2014 by Pearson
Education, Inc. All Rights
Reserved.
©1992-2014 by Pearson
Education, Inc. All Rights
Reserved.
©1992-2014 by Pearson
Education, Inc. All Rights
Reserved.
©1992-2014 by Pearson
Education, Inc. All Rights
Reserved.
TabControl Example
• Many complex apps are multiple document interface (MDI) programs, which allow users to edit multiple documents at once.
• An MDI program’s main window is called the parent window, and each window inside the app is referred to as a child window.
• Figure 15.37 depicts a sample MDI app with two child windows.
• To create an MDI Form, create a new Form and set its IsMdiContainer property to true(Fig. 15.38).
•
Multiple Document Interface (MDI) Windows
©1992-2014 by Pearson Education, Inc. All Rights Reserved.
©1992-2014 by Pearson
Education, Inc. All Rights
Reserved.
©1992-2014 by Pearson
Education, Inc. All Rights
Reserved.
• Right click the project in the Solution Explorer, select Project > Add Windows Form…and name the file.
• Set the Form’s MdiParent property to the parent Form and call the child Form’s Show method.Form2 childForm = New Form2();
Form2.MdiParent = this;
Form2.Show();
Multiple Document Interface (MDI) Windows (Cont.)
©1992-2014 by Pearson Education, Inc. All Rights Reserved.
©1992-2014 by Pearson
Education, Inc. All Rights
Reserved.
• Figure 15.40 shows two images: one containing two minimized child windows and a second containing a maximized child window.
Multiple Document Interface (MDI) Windows (Cont.)
©1992-2014 by Pearson Education, Inc. All Rights Reserved.
©1992-2014 by Pearson
Education, Inc. All Rights
Reserved.