Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms...

54
Chapter 12: Using Controls

Transcript of Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms...

Page 1: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

Chapter 12:Using Controls

Page 2: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

Examining the IDE’s Automatically Generated Code

• A new Windows Forms project has been started and given the name FormWithALabelAndAButton

• A Label has been dragged onto Form1 and its properties updated

• A Button has been dragged onto Form1 and its properties updated

2Microsoft Visual C# 2012, Fifth Edition

Page 3: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

3Microsoft Visual C# 2012, Fifth Edition

Examining the IDE’s Automatically Generated Code (cont’d.)

Page 4: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

Examining the IDE’s Automatically Generated Code (cont’d.)

• Within the Form1.Designer.cs file in Visual Studio, two lines of code are generated as follows:private System.Windows.Forms.Label label1;

private System.Windows.Forms.Button okButton;

• These lines appear under a gray box that contains Windows Form Designer generated code

• Expand the code generated by clicking the + in the method node or double-clicking the gray box

4Microsoft Visual C# 2012, Fifth Edition

Page 5: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

5Microsoft Visual C# 2012, Fifth Edition

Examining the IDE’s Automatically Generated Code (cont’d.)

Page 6: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

Setting a Control’s Font

• Use the Font class to change the appearance of printed text on your Forms

• Change the appearance of printed text using code– Create your own instance of the Font class

• The class includes a number of overloaded constructors

• Example:System.Drawing.Font bigFont = new System.Drawing.Font("Courier New", 16.5f);

this.label1.Font = bigFont;this.okButton.Font = bigFont;

6Microsoft Visual C# 2012, Fifth Edition

Page 7: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

Setting a Control’s Font (cont’d.)

7Microsoft Visual C# 2012, Fifth Edition

Page 8: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

8Microsoft Visual C# 2012, Fifth Edition

Setting a Control’s Font (cont’d.)

Page 9: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

9Microsoft Visual C# 2012, Fifth Edition

Setting a Control’s Font (cont’d.)

Page 10: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

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

10Microsoft Visual C# 2012, Fifth Edition

Page 11: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

Using a LinkLabel (cont’d.)

11Microsoft Visual C# 2012, Fifth Edition

Page 12: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

• 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 true when you determine that a user has clicked a link

12Microsoft Visual C# 2012, Fifth Edition

Using a LinkLabel (cont’d.)

Page 13: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

13Microsoft Visual C# 2012, Fifth Edition

Using a LinkLabel (cont’d.)

Page 14: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

14Microsoft Visual C# 2012, Fifth Edition

Using a LinkLabel (cont’d.)

Page 15: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

15Microsoft Visual C# 2012, Fifth Edition

Using a LinkLabel (cont’d.)

Page 16: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

Adding Color to a Form

• Color class– Contains a wide variety of predefined Colors that you can

use with your Controls

• You can change a Form’s color using its BackColor and ForeColor properties

16Microsoft Visual C# 2012, Fifth Edition

Page 17: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

Using CheckBox and RadioButton Objects

• 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

17Microsoft Visual C# 2012, Fifth Edition

Page 18: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

Using CheckBox and RadioButton Objects (cont’d.)

18Microsoft Visual C# 2012, Fifth Edition

Page 19: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

19Microsoft Visual C# 2012, Fifth Edition

Using CheckBox and RadioButton Objects (cont’d.)

Page 20: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

20Microsoft Visual C# 2012, Fifth Edition

Using CheckBox and RadioButton Objects (cont’d.)

Page 21: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

21Microsoft Visual C# 2012, Fifth Edition

Using CheckBox and RadioButton Objects (cont’d.)

Page 22: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

22Microsoft Visual C# 2012, Fifth Edition

Using CheckBox and RadioButton Objects (cont’d.)

Page 23: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

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

23Microsoft Visual C# 2012, Fifth Edition

Page 24: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

24Microsoft Visual C# 2012, Fifth Edition

Page 25: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

25Microsoft Visual C# 2012, Fifth Edition

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

Page 26: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

26Microsoft Visual C# 2012, Fifth Edition

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

Page 27: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

Adding ListBox, CheckedListBox, and ComboBox Controls to a Form

• ListBox, ComboBox, and CheckedListBox objects– 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

27Microsoft Visual C# 2012, Fifth Edition

Page 28: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

28

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

Microsoft Visual C# 2012, Fifth Edition

Page 29: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

29Microsoft Visual C# 2012, Fifth Edition

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

Page 30: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

30Microsoft Visual C# 2012, Fifth Edition

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

Page 31: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

31Microsoft Visual C# 2012, Fifth Edition

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

Page 32: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

32Microsoft Visual C# 2012, Fifth Edition

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

Page 33: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

• 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

33Microsoft Visual C# 2012, Fifth Edition

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

Page 34: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

34Microsoft Visual C# 2012, Fifth Edition

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

Page 35: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

Adding MonthCalendar and DateTimePicker Controls to a Form

• MonthCalendar and DateTimePicker Controls– Allow you to retrieve date and time information

35Microsoft Visual C# 2012, Fifth Edition

Page 36: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

36Microsoft Visual C# 2012, Fifth Edition

Adding MonthCalendar and DateTimePicker Controls to a Form (cont’d.)

Page 37: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

37Microsoft Visual C# 2012, Fifth Edition

Adding MonthCalendar and DateTimePicker Controls to a Form (cont’d.)

Page 38: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

38Microsoft Visual C# 2012, Fifth Edition

Adding MonthCalendar and DateTimePicker Controls to a Form (cont’d.)

Page 39: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

39Microsoft Visual C# 2012, Fifth Edition

Adding MonthCalendar and DateTimePicker Controls to a Form (cont’d.)

Page 40: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

• DateTimePicker Control– Displays a month calendar when the down arrow is

selected

40Microsoft Visual C# 2012, Fifth Edition

Adding MonthCalendar and DateTimePicker Controls to a Form (cont’d.)

Page 41: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

41Microsoft Visual C# 2012, Fifth Edition

Adding MonthCalendar and DateTimePicker Controls to a Form (cont’d.)

Page 42: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

42Microsoft Visual C# 2012, Fifth Edition

Adding MonthCalendar and DateTimePicker Controls to a Form (cont’d.)

Page 43: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

Working with a Form’s Layout

• Blue snap lines– Appear when you drag multiple Controls onto a Form– Help align new Controls with others already in place– Appear when you place a control closer to the edge of a

container than is recommended

• Can use the Location property in the Properties list to specify a location

43Microsoft Visual C# 2012, Fifth Edition

Page 44: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

Working with a Form’s Layout (cont’d.)

44Microsoft Visual C# 2012, Fifth Edition

Page 45: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

Working with a Form’s Layout (cont’d.)

• Dock property– Attaches a Control to the side of a container so that the Control stretches when the container’s size is adjusted

45Microsoft Visual C# 2012, Fifth Edition

Page 46: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

Working with a Form’s Layout (cont’d.)

46Microsoft Visual C# 2012, Fifth Edition

Page 47: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

Working with a Form’s Layout (cont’d.)

47Microsoft Visual C# 2012, Fifth Edition

Page 48: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

Understanding GroupBoxes and Panels

• GroupBox or Panel – Groups related Controls on a Form– Can be docked inside a Form

• GroupBoxes– Can display a caption– Do not have scroll bars

• Panels– Cannot display a caption– Have scroll bars

48Microsoft Visual C# 2012, Fifth Edition

Page 49: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

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

49Microsoft Visual C# 2012, Fifth Edition

Page 50: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

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

50Microsoft Visual C# 2012, Fifth Edition

Page 51: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

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

51Microsoft Visual C# 2012, Fifth Edition

Page 52: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

52Microsoft Visual C# 2012, Fifth Edition

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

Page 53: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

Using Other Controls

• If you click Project on the main menu and click Add New Item, you can add extra Forms, Files, Controls, and other elements to your project

53Microsoft Visual C# 2012, Fifth Edition

Page 54: Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.

You Do It

• Adding Labels to a Form and Changing Their Properties

• Examining the Code Generated by the IDE• Adding CheckBoxes to a Form• Adding RadioButtons to a Form

54Microsoft Visual C# 2012, Fifth Edition