chapter 12: using controls

67
Chapter 12: Using Controls

Upload: others

Post on 27-Oct-2021

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 12: Using Controls

Chapter 12:

Using Controls

Page 2: 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

Page 3: Chapter 12: Using Controls

Using a LinkLabel (cont’d.)

3Microsoft Visual C# 2012, Fifth Edition

Page 4: Chapter 12: Using Controls

• 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.)

Page 5: Chapter 12: Using Controls

5Microsoft Visual C# 2012, Fifth Edition

Using a LinkLabel (cont’d.)

Page 6: Chapter 12: Using Controls

6Microsoft Visual C# 2012, Fifth Edition

Using a LinkLabel (cont’d.)

Page 7: Chapter 12: Using Controls

7Microsoft Visual C# 2012, Fifth Edition

Using a LinkLabel (cont’d.)

Page 8: Chapter 12: Using Controls

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

Page 9: Chapter 12: Using Controls

Using CheckBoxand RadioButtonObjects (cont’d.)

9Microsoft Visual C# 2012, Fifth Edition

Page 10: Chapter 12: Using Controls

10Microsoft Visual C# 2012, Fifth Edition

Using CheckBoxand RadioButtonObjects (cont’d.)

Page 11: Chapter 12: Using Controls

11Microsoft Visual C# 2012, Fifth Edition

Using CheckBoxand RadioButtonObjects (cont’d.)

Page 12: Chapter 12: Using Controls

12Microsoft Visual C# 2012, Fifth Edition

Using CheckBoxand RadioButtonObjects (cont’d.)

Page 13: Chapter 12: Using Controls

13Microsoft Visual C# 2012, Fifth Edition

Using CheckBoxand RadioButtonObjects (cont’d.)

Page 14: Chapter 12: Using Controls

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

Page 15: Chapter 12: Using Controls

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

Page 16: Chapter 12: Using Controls

©1992-2014 by Pearson

Education, Inc. All Rights

Reserved.

Page 17: Chapter 12: Using Controls

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

Page 18: Chapter 12: Using Controls

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.

Page 19: Chapter 12: Using Controls

19

Page 20: Chapter 12: Using Controls

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

Page 21: Chapter 12: Using Controls

©1992-2014 by Pearson

Education, Inc. All Rights

Reserved.

Page 22: Chapter 12: Using Controls

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

Page 23: Chapter 12: Using Controls

©1992-2014 by Pearson

Education, Inc. All Rights

Reserved.

Page 24: Chapter 12: Using Controls

©1992-2014 by Pearson

Education, Inc. All Rights

Reserved.

Page 25: Chapter 12: Using Controls

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

Page 26: Chapter 12: Using Controls

26Microsoft Visual C# 2012, Fifth Edition

Page 27: Chapter 12: Using Controls

27Microsoft Visual C# 2012, Fifth Edition

Adding a PictureBox to a Form (cont’d.)

Page 28: Chapter 12: Using Controls

28Microsoft Visual C# 2012, Fifth Edition

Adding a PictureBox to a Form (cont’d.)

Page 29: Chapter 12: Using Controls

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

Page 30: Chapter 12: Using Controls

30

Adding ListBox, CheckedListBox, and ComboBoxControls to a Form (cont’d.)

Microsoft Visual C# 2012, Fifth Edition

Page 31: Chapter 12: Using Controls

31Microsoft Visual C# 2012, Fifth Edition

Adding ListBox, CheckedListBox, and ComboBoxControls to a Form (cont’d.)

Page 32: Chapter 12: Using Controls

32Microsoft Visual C# 2012, Fifth Edition

Adding ListBox, CheckedListBox, and ComboBoxControls to a Form (cont’d.)

Page 33: Chapter 12: Using Controls

33Microsoft Visual C# 2012, Fifth Edition

Adding ListBox, CheckedListBox, and ComboBoxControls to a Form (cont’d.)

Page 34: Chapter 12: Using Controls

34Microsoft Visual C# 2012, Fifth Edition

Adding ListBox, CheckedListBox, and ComboBoxControls to a Form (cont’d.)

Page 35: Chapter 12: Using Controls

• 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.)

Page 36: Chapter 12: Using Controls

36Microsoft Visual C# 2012, Fifth Edition

Adding ListBox, CheckedListBox, and ComboBoxControls to a Form (cont’d.)

Page 37: Chapter 12: Using Controls

Adding MonthCalendarand DateTimePickerControls to a Form

• MonthCalendar and DateTimePickerControls

– Allow you to retrieve date and time information

37Microsoft Visual C# 2012, Fifth Edition

Page 38: Chapter 12: Using Controls

38Microsoft Visual C# 2012, Fifth Edition

Adding MonthCalendarand DateTimePickerControls to a Form (cont’d.)

Page 39: Chapter 12: Using Controls

39Microsoft Visual C# 2012, Fifth Edition

Adding MonthCalendarand DateTimePickerControls to a Form (cont’d.)

Page 40: Chapter 12: Using Controls

40Microsoft Visual C# 2012, Fifth Edition

Adding MonthCalendarand DateTimePickerControls to a Form (cont’d.)

Page 41: Chapter 12: Using Controls

41Microsoft Visual C# 2012, Fifth Edition

Adding MonthCalendarand DateTimePickerControls to a Form (cont’d.)

Page 42: Chapter 12: Using Controls

• 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.)

Page 43: Chapter 12: Using Controls

43Microsoft Visual C# 2012, Fifth Edition

Adding MonthCalendarand DateTimePickerControls to a Form (cont’d.)

Page 44: Chapter 12: Using Controls

44Microsoft Visual C# 2012, Fifth Edition

Adding MonthCalendarand DateTimePickerControls to a Form (cont’d.)

Page 45: Chapter 12: Using Controls

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

Page 46: Chapter 12: Using Controls

Adding a MenuStrip to a Form (cont’d.)

46Microsoft Visual C# 2012, Fifth Edition

Page 47: Chapter 12: Using Controls

Adding a MenuStrip to a Form (cont’d.)

47Microsoft Visual C# 2012, Fifth Edition

Page 48: Chapter 12: Using Controls

48Microsoft Visual C# 2012, Fifth Edition

Adding a MenuStrip to a Form (cont’d.)

Page 49: Chapter 12: Using Controls

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

Page 50: Chapter 12: Using Controls

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

Page 51: Chapter 12: Using Controls

©1992-2014 by Pearson

Education, Inc. All Rights

Reserved.

Page 52: Chapter 12: Using Controls

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

Page 53: Chapter 12: Using Controls

©1992-2014 by Pearson

Education, Inc. All Rights

Reserved.

Page 54: Chapter 12: Using Controls

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

Page 55: Chapter 12: Using Controls

TreeView Control (Example)

Page 56: Chapter 12: Using Controls

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

Page 57: Chapter 12: Using Controls

©1992-2014 by Pearson

Education, Inc. All Rights

Reserved.

Page 58: Chapter 12: Using Controls

©1992-2014 by Pearson

Education, Inc. All Rights

Reserved.

Page 59: Chapter 12: Using Controls

©1992-2014 by Pearson

Education, Inc. All Rights

Reserved.

Page 60: Chapter 12: Using Controls

©1992-2014 by Pearson

Education, Inc. All Rights

Reserved.

TabControl Example

Page 61: Chapter 12: Using Controls

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

Page 62: Chapter 12: Using Controls

©1992-2014 by Pearson

Education, Inc. All Rights

Reserved.

Page 63: Chapter 12: Using Controls

©1992-2014 by Pearson

Education, Inc. All Rights

Reserved.

Page 64: Chapter 12: Using Controls

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

Page 65: Chapter 12: Using Controls

©1992-2014 by Pearson

Education, Inc. All Rights

Reserved.

Page 66: Chapter 12: Using Controls

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

Page 67: Chapter 12: Using Controls

©1992-2014 by Pearson

Education, Inc. All Rights

Reserved.