Download presentation
Presentation is loading. Please wait.
Published byCarol Hines Modified over 9 years ago
2
Jozef Goetz contribution, 2012 1 2011 Pearson Education, Inc. All rights reserved. 2002 Prentice Hall. All rights reserved.
3
Jozef Goetz contribution, 2012 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
4
Jozef Goetz contribution, 2012 3 But, soft! what light through yonder window breaks? It is the east, and Juliet is the sun! William Shakespeare
5
Jozef Goetz contribution, 2012 4 Chapter 15 – Graphical User Interfaces Part 2 Outline 15.1 Introduction 15.2 Menus 15.3 MonthCalendar 15.4 DateTimePicker 15.5 LinkLabels 15.6 ListBoxes 15.7 CheckedListBoxes 15.8 ComboBoxes 15.9 TreeViews 15.10 ListViews 15.11Tab Control 15.12 Multiple Document Interface (MDI) Windows 15.13Visual Inheritance 15.14 User-Defined Controls
6
Jozef Goetz contribution, 2012 5 Chapter 15: Graphical User Interface Concepts: Part 2: Objectives 1.To be able to create menus, window tabs and multiple-document-interface (MDI) programs. 2.To understand the use of the ListView and TreeView controls for displaying information. 3.To be able to use hyperlinks with the LinkLabel control. 4.To be able to display lists using ListBoxes and ComboBoxes. 5.To create custom controls.
7
Jozef Goetz contribution, 2012 6 15.1 Introduction Visual Studio provides a large set of GUI components. Visual Studio also enables you to design custom controls and add them to the ToolBox Continues study of Graphical User Interface Explores: Menus LinkLabels ListBox CheckedListBox ComboBoxes TreeView control Tab controls Multiple-document interface MDI windows
8
Jozef Goetz contribution, 2012 7 Menus provide groups of related commands (Fig. 15.1). Menus organize commands without “cluttering” the GUI. Fig. 15.1 | Menus, submenus and menu items. Checked menu item Submenu Separator bar Shortcut key Disabled commands Menu Menu items Menu icons 15.2 Menus
9
Jozef Goetz contribution, 2012 8 To create a menu, open the Toolbox and drag a MenuStrip control onto the Form. To add menu items to the menu, click the Type Here TextBox (Fig. 15.2) and type the menu item’s name. Fig. 15.2 | Editing menus in Visual Studio. MenuStrip icon Type menu name in TextBox Main menu bar 15.2 Menus
10
Jozef Goetz contribution, 2012 9 After you press the Enter key, the menu item is added. More Type Here TextBox es allow you to add more items (Fig. 15.3). Fig. 15.3 | Adding ToolStripMenuItems to a MenuStrip. 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 15.2 Menus
11
Jozef Goetz contribution, 2012 10 Menus can have Alt key shortcuts which are accessed by pressing Alt and the underlined letter. To make the File menu item have a key shortcut, type &File. The letter F is underlined to indicate that it is a shortcut. 15.2 Menus
12
Jozef Goetz contribution, 2012 11 Menu items can have shortcut keys as well (Ctrl, Shift, Alt, F1, F2, letter keys, and so on). To add other shortcut keys, set the ShortcutKeys property (Fig. 15.4). Fig. 15.4 | Setting a menu item’s shortcut keys. Select key (modifier and key combination specifies the shortcut key for the menu item) Setting modifier keys 15.2 Menus
13
Jozef Goetz contribution, 2012 12 Look-and-Feel Observation 15.1 Buttons can have access keyboard 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. You can remove a menu item by selecting it with the mouse and pressing the Delete key. Menu items can be grouped by separator bars, which are inserted by right clicking and selecting Insert Separator or by typing “ - ” for the text of a menu item.
14
Jozef Goetz contribution, 2012 13 Fig. 15.5 | Menu-item options. Menu item options Visual Studio allows you to add TextBox es and ComboBox es as menu items. Before you enter text for a menu item, you are provided with a drop-down list. – Clicking the down arrow allows you to select the type of item to add (Fig. 15.5). 15.2 Menus
15
Jozef Goetz contribution, 2012 14 Fig. 15.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)
16
Jozef Goetz contribution, 2012 15 Fig. 15.5 | Menu item options. Menu item options
17
Jozef Goetz contribution, 2012 16 Fig. 15.6 | MenuStrip and ToolStripMenuItem properties and an event. (Part 1 of 2.)
18
Jozef Goetz contribution, 2012 17 Fig. 15.6 | MenuStrip and ToolStripMenuItem properties and an event. (Part 2 of 2.)
19
Jozef Goetz contribution, 2012 18 Look-and-Feel Observation 15.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.
20
Jozef Goetz contribution, 2012 19 MenuTestForm.cs (1 of 8) (c)(c) (d)(d) (e)(e) (f)(f) displayLabel.Text is preset manually in class
21
Jozef Goetz contribution, 2012 20 Outline MenuTestF orm.cs (2 of 8) (c)(c) (d)(d) (e)(e) (f )
22
Jozef Goetz contribution, 2012 21 Outline MenuTestF orm.cs (3 of 8) (c)(c) (d)(d) (e)(e) (f )
23
Jozef Goetz contribution, 2012 22 Outline MenuTestF orm.cs (4 of 8) (c)(c) (d)(d) (e)(e) (f )
24
Jozef Goetz contribution, 2012 23 Outline MenuTestF orm.cs (5 of 8) (a) (b)
25
Jozef Goetz contribution, 2012 24 Outline MenuTestF orm.cs (6 of 8) (a) (b)
26
Jozef Goetz contribution, 2012 25 (a) (b) Outline MenuTestF orm.cs (7 of 8)
27
Jozef Goetz contribution, 2012 26 Outline MenuTestF orm.cs (8 of 8) (c) (d) (e) (f)
28
Jozef Goetz contribution, 2012 27 Software Engineering Observation 15.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. 15.2 Menus
29
Jozef Goetz contribution, 2012 28 Fig. 15.8 | MonthCalendar control. Current day is outlined Selected day is highlighted
30
Jozef Goetz contribution, 2012 29 The MonthCalendar control (Fig. 15.8) displays a monthly calendar on the Form. Multiple dates can be selected by clicking dates on the calendar while holding down the Shift key. Fig. 15.8 | MonthCalendar control. Current day is outlined Selected day is highlighted Fig. 15.8 | MonthCalendar control.
31
Jozef Goetz contribution, 2012 30 Fig. 15.9 | MonthCalendar properties and an event.
32
Jozef Goetz contribution, 2012 31 Fig. 15.10 | DateTimePicker properties and an event. (Part 1 of 2.) The DateTimePicker control displays a calendar when a down arrow is selected. The DateTimePicker can be used to retrieve date and time information from the user.
33
Jozef Goetz contribution, 2012 32 Fig. 15.10 | DateTimePicker properties and an event. (Part 2 of 2.)
34
Jozef Goetz contribution, 2012 33 Outline DateTimePickerForm.cs (c)(d) The DateTime structure’s DayOfWeek property determines the day of the week on which the selected date falls. Retrieving the selected date from the Value property. Using DateTime ’s AddDays method to increase the date by two days or three days. the arrival date is always two days after drop-off, or three days if a Sunday is reached.
35
Jozef Goetz contribution, 2012 34 Outline DateTimePickerForm.cs Fig. 15.11 | Demonstrating DateTimePicker. (Part 4 of 4.) a) Clicking the down arrow b) Selecting a day from the calendar c) The Label updates d) Selecting another day Setting the MinDate and MaxDate properties to keep drop-off sometime in the next year.
36
Jozef Goetz contribution, 2012 35 15.5 LinkLabels LinkLabel Displays links to other resources Files Web pages Behavior similar to web page hyperlink Can change color –New –Previously visited
37
Jozef Goetz contribution, 2012 36 15.5 LinkLabel s Fig. 15.5 LinkLabel control in the design phase and in running program. LinkLabel on a form Hand image displayed when mouse cursor over a LinkLabel
38
Jozef Goetz contribution, 2012 37 The LinkLabel control displays links to other resources, such as files or web pages (Fig. 15.12). Fig. 15.12 | LinkLabel control in running program. Look-and-Feel Observation 15.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 15.5 LinkLabel Control
39
Jozef Goetz contribution, 2012 38 15.5 LinkLabel s Represents a Windows label control that can display hyperlinks. For a list of all members of this type, see LinkLabel Members.LinkLabel Members System.Object System.MarshalByRefObject System.ComponentModel.Component System.Windows.Forms.Control System.Windows.Forms.Label System.Windows.Forms.LinkLabel System.ObjectSystem.MarshalByRefObjectSystem.ComponentModel.ComponentSystem.Windows.Forms.ControlSystem.Windows.Forms.Label like a hyperlink in a Web page Displays links to other objects Uses event handlers to link to right file or program Start method of Process class opens other programs Derived from class Label, inherits functionality
40
Jozef Goetz contribution, 2012 39 15.5 LinkLabel s
41
2002 Prentice Hall. All rights reserved. Outline 40 LinkLabelTest.cs 1 // Fig. 13.7 ed1 or 15.14 ed4: LinkLabelTest.cs 2 // Using LinkLabels to create hyperlinks. 3 4 using System; 5 using System.Drawing; 6 using System.Collections; 7 using System.ComponentModel; 8 using System.Windows.Forms; 9 using System.Data; 10 11 public class LinkLabelTest : System.Windows.Forms.Form 12 { 13 // linklabels to C: drive, www.deitel.com and Notepad 14 private System.Windows.Forms.LinkLabel driveLinkLabel; 15 private System.Windows.Forms.LinkLabel deitelLinkLabel; 16 private System.Windows.Forms.LinkLabel notepadLinkLabel; 17 18 [STAThread] 19 static void Main() 20 { 21 Application.Run( new LinkLabelTest() ); 22 } 23 24 // browse C:\ drive 25 private void driveLinkLabel_LinkClicked( object sender, 26 System.Windows.Forms.LinkLabelLinkClickedEventArgs e ) 27 { 28 driveLinkLabel.LinkVisited = true; 29 System.Diagnostics.Process.Start( "C:\\" ); //Start( @”C:\”) in ed4 @ indicates that all characters in the string should be interprated literally – this is known as a verbatim string 30 } 31 C drive linkNotepad linkDeitel website link C drive event handler Start method to open other programs in class
42
2002 Prentice Hall. All rights reserved. Outline 41 32 // load www.deitel.com in Web broswer 33 private void deitelLinkLabel_LinkClicked( object sender, 34 System.Windows.Forms.LinkLabelLinkClickedEventArgs e ) 35 { 36 deitelLinkLabel.LinkVisited = true; 37 System.Diagnostics.Process.Start( 38 "IExplore", "http://www.deitel.com" ); // or just the 2 nd parameter 39 } 40 41 // run application Notepad 42 private void notepadLinkLabel_LinkClicked( 43 object sender, 44 System.Windows.Forms.LinkLabelLinkClickedEventArgs e ) 45 { 46 notepadLinkLabel.LinkVisited = true; 47 48 // program called as if in run 49 // menu and full path not needed 50 System.Diagnostics.Process.Start( "notepad" ); 51 } 52 53 } // end class LinkLabelTest Deitel website event handler Notepad event handler System.Diagnostics.Process - Provides access to local and remote processes and enables you to start and stop local system processes. System.Object System.MarshalByRefObject System.ComponentModel.Component System.Diagnostics.Process 1.Process.Start(ProcessStartInfo) - Starts the process resource that is specified by the parameter containing process start information (for example, the file name of the process to start) and associates the resource with a new Process component. 2.Process.Start(String) - Starts a process resource by specifying the name of a document or application file and associates the resource with a new Process component. 3.Process.Start(String, String) - Starts a process resource by specifying the name of an application and a set of command line arguments, and associates the resource with a new Process component.
43
Jozef Goetz contribution, 2012 42 Outline Fig. 15.14 | LinkLabel s used to link to a drive, a web page and an application. (Part 4 of 6.) Click first LinkLabel to look at contents of C: drive LinkLabelTest Form.cs (4 of 6 )
44
Jozef Goetz contribution, 2012 43 Outline Fig. 15.14 | LinkLabel s used to link to a drive, a web page and an application. (Part 5 of 6.) Click second LinkLabel to go to Deitel website LinkLabelTest Form.cs (5 of 6 )
45
Jozef Goetz contribution, 2012 44 Outline Fig. 15.14 | LinkLabel s used to link to a drive, a web page and an application. (Part 6 of 6.) Click on third LinkLabel to open Notepad LinkLabelTest Form.cs (6 of 6 )
46
Jozef Goetz contribution, 2012 45 15.6 ListBox es and CheckListBox es Fig. 15.15 ListBox and CheckedListBox on a form. ListBox Selected Items Checked item CheckedListBox Scroll bars appear if necessary Extends ListBox by placing check boxes next to items Can select more than one object at one time allows the user to view and select from multiple items in a list
47
Jozef Goetz contribution, 2012 46 15.6 ListBox es ListBoxe s Allow users to view and select from items on a list Static objects – static GUI entities SelectionMode property determines number of items that can be selected Property Items returns all objects in list Property SelectedItem returns current selected item Property SelectedIndex returns index of selected item Property GetSelected returns true if property at given index is selected Event: SelectedIndexChanged – generated when selected index changes For a list of all members of this type, see ListBox Members.ListBox Members System.Object System.Object System.MarshalByRefObject System.ComponentModel.Component System.Windows.Forms.Control System.Windows.Forms.ListControl System.Windows.Forms.ListBox System.Windows.Forms.CheckedListBoxSystem.MarshalByRefObjectSystem.ComponentModel.ComponentSystem.Windows.Forms.ControlSystem.Windows.Forms.ListControlSystem.Windows.Forms.CheckedListBox
48
Jozef Goetz contribution, 2012 47 15.6 ListBox es
49
Jozef Goetz contribution, 2012 48 Fig. 15.16 | ListBox properties, methods and an event. (Part 2 of 2.)
50
Jozef Goetz contribution, 2012 49 15.6 ListBox es Fig. 15.17 String Collection Editor. Use Add method to add to Items collection myListBox.Items.Add(“myListItem”) or Using the Items property by clicking the ellipsis we can add as the following editor is open up A Collection is a data structure, similar to an array and can be used with a foreach statement.
51
2002 Prentice Hall. All rights reserved. Outline 50 in class 1 // Fig 13.11:ed1 or 15.18 ed4 ListBoxTest.cs 2 // Program to add, remove and clear list box items. 3 4 using System; 5 using System.Drawing; 6 using System.Collections; 7 using System.ComponentModel; 8 using System.Windows.Forms; 9 using System.Data; 10 11 public class ListBoxTest : System.Windows.Forms.Form 12 { 13 // contains user-input list of elements 15 private System.Windows.Forms.ListBox displayListBox; 15 16 // user input textbox 17 private System.Windows.Forms.TextBox inputTextBox; 18 19 // add, remove, clear and exit command buttons 20 private System.Windows.Forms.Button addButton; 21 private System.Windows.Forms.Button removeButton; 22 private System.Windows.Forms.Button clearButton; 23 private System.Windows.Forms.Button exitButton; 24 25 [STAThread] 26 static void Main() 27 { 28 Application.Run( new ListBoxTest() ); 29 } 30 Display ListBoxText field for inputAdd button Remove Button Clear button Exit button Below developed Class ListBoxTest Allows users to add and remove items from ListBox Uses event handlers to add to, remove from and clear list
52
2002 Prentice Hall. All rights reserved. Outline 51 ListBoxTest.cs 31 // add new item (text from input box) 32 // and clear input box 33 private void addButton_Click( 34 object sender, System.EventArgs e ) 35 { 36 displayListBox.Items.Add( inputTextBox.Text ); 37 inputTextBox.Clear(); 38 } 39 40 // remove item if one selected 41 private void removeButton_Click( 42 object sender, System.EventArgs e ) 43 { 44 // remove only if item selected 45 if ( displayListBox.SelectedIndex != -1 ) 46 displayListBox.Items.RemoveAt( 47 displayListBox.SelectedIndex ); 48 } 49 50 // clear all items 51 private void clearButton_Click( 52 object sender, System.EventArgs e ) 53 { 54 displayListBox.Items.Clear(); 55 } 56 57 // exit application 58 private void exitButton_Click( 59 object sender, System.EventArgs e ) 60 { 61 Application.Exit(); 62 } 63 64 } // end class ListBoxTest Add event handler Add method Remove methodClear methodTest if item is selected Exit
53
2002 Prentice Hall. All rights reserved. Outline 52 ListBoxTest.cs Program Output
54
Jozef Goetz contribution, 2012 53 15.7 CheckedListBox es Displays a ListBox in which a check box is displayed to the left of each item.ListBox For a list of all members of this type, see CheckedListBox Members.CheckedListBox Members System.Object System.MarshalByRefObject System.ComponentModel.Component System.Windows.Forms.Control System.Windows.Forms.ListControl System.Windows.Forms.ListBox System.Windows.Forms.CheckedListBox System.ObjectSystem.MarshalByRefObjectSystem.ComponentModel.ComponentSystem.Windows.Forms.ControlSystem.Windows.Forms.ListControlSystem.Windows.Forms.ListBox CheckedListBox derives from class ListBox Can add to, remove from or clear list Can select multiple items from the list Properties CurrentValue and NewValue return state of object selected Properties CheckedItems and CheckedIndices return the objects and indices of selected items respectively
55
Jozef Goetz contribution, 2012 54 15.7 CheckListBox es
56
Jozef Goetz contribution, 2012 55 15.7 CheckListBox es System.Windows.Forms Namespace has public enum CheckState.Checked Specifies the state of a control, such as a check box, that can be checked, unchecked, or set to an indeterminate state.
57
2002 Prentice Hall. All rights reserved. Outline 56 CheckedListBoxTe st.cs 1 // Fig. 13.13 ed1 or 15.20 ed3: CheckedListBoxTest.cs 2 // Using the checked list boxes to add items to a list box 3 4 using System; 5 using System.Drawing; 6 using System.Collections; 7 using System.ComponentModel; 8 using System.Windows.Forms; 9 using System.Data; 10 11 public class CheckedListBoxTest : System.Windows.Forms.Form 12 { 13 // list of available book titles 14 private System.Windows.Forms.CheckedListBox 15 inputCheckedListBox; 16 17 // user selection list 18 private System.Windows.Forms.ListBox displayListBox; 19 20 [STAThread] 21 static void Main() 22 { 23 Application.Run( new CheckedListBoxTest() ); 24 } 25 26 // item about to change, 27 // add or remove from displayListBox 28 private void inputCheckedListBox_ItemCheck( 29 object sender, 30 System.Windows.Forms.ItemCheckEventArgs e ) 31 { // public class ItemCheckEventArgs : EventArgs 32 // get reference of selected item 33 string item = 34 inputCheckedListBox.SelectedItem.ToString(); 35 CheckedListBoxListBoxItemCheck event handler
58
2002 Prentice Hall. All rights reserved. Outline 57 36 // if item checked add to listbox 37 // otherwise remove from listbox 38 if ( e.NewValue == CheckState.Checked )// check a new state 39 displayListBox.Items.Add( item ); // Items collection inherited 40 else// from ListBox 41 displayListBox.Items.Remove( item ); 42 43 } // end method inputCheckedListBox_Click 44 45 } // end class CheckedListBox System.Windows.Forms Namespace has public enum CheckState.Checked - specifies the state of a control, such as a check box, that can be checked, unchecked, or set to an indeterminate state.
59
Jozef Goetz contribution, 2012 58 15.8 ComboBox es Fig. 15.21 Demonstrating a ComboBox. Combine TextBox and drop-down list Add method adds object to collection Properties: DropDownStyle: determines type of ComboBox: Simple text is always visible and editable, doesn’t display a drop-down arrow button but has a scroll bar DropDown the default means that the text portion is editable but an arrow button must be clicked to see the list portion. DropDownList the text portion is not editable and the arrow button must be clicked to see the list portion.
60
Jozef Goetz contribution, 2012 59 15. 8 ComboBoxes ComboBox Combines TextBox features with drop-down list Drop-down list Contains a list from which a value can be selected Scrollbar appears if necessary Common Properties: Items: returns objects in the list SelectedItem: returns object selected SelectedIndex: returns index of selected item
61
Jozef Goetz contribution, 2012 60 15. 8 ComboBox es Represents a Windows combo box control. For a list of all members of this type, see ComboBox Members.ComboBox Members System.Object System.MarshalByRefObject System.ComponentModel.Component System.Windows.Forms.Control System.Windows.Forms.ListControl System.Windows.Forms.ComboBox System.ObjectSystem.MarshalByRefObjectSystem.ComponentModel.ComponentSystem.Windows.Forms.ControlSystem.Windows.Forms.ListControl
62
Jozef Goetz contribution, 2012 61 15. 8 ComboBox es Encapsulates a GDI+ drawing surface. This class cannot be inherited. For a list of all members of this type, see Graphics Members.Graphics Members System.Object System.MarshalByRefObject System.Drawing.Graphics System.ObjectSystem.MarshalByRefObject Defines an object used to draw lines and curves. This class cannot be inherited. For a list of all members of this type, see Pen Members.Pen Members System.Object System.MarshalByRefObject System.Drawing.Pen System.ObjectSystem.MarshalByRefObject Defines a brush of a single color. Brushes are used to fill graphics shapes, such as rectangles, ellipses, pies, polygons, and paths. This class cannot be inherited. For a list of all members of this type, see SolidBrush Members.SolidBrush Members System.Object System.MarshalByRefObject System.Drawing.Brush System.Drawing.SolidBrush System.ObjectSystem.MarshalByRefObjectSystem.Drawing.Brush
63
Jozef Goetz contribution, 2012 62 15. 8 ComboBox es
64
2002 Prentice Hall. All rights reserved. Outline 63 ComboBoxTest.cs 1 // Fig. 13.16 ed1 or 15.23 ed4: ComboBoxTest.cs 2 // Using ComboBox to select shape to draw 3 4 using System; 5 using System.Drawing; 6 using System.Collections; 7 using System.ComponentModel; 8 using System.Windows.Forms; 9 using System.Data; 10 11 public class ComboBoxTest : System.Windows.Forms.Form 12 { 13 // contains shape list (circle, square, ellipse, pie) 14 private System.Windows.Forms.ComboBox imageComboBox; 15 16 [STAThread] 17 static void Main() 18 { 19 Application.Run( new ComboBoxTest() ); 20 } 21 22 // get selected index, draw shape 23 private void imageComboBox_SelectedIndexChanged( 24 object sender, System.EventArgs e ) 25 { 26 // create graphics object, pen and brush 27 Graphics myGraphics = base.CreateGraphics(); 28 29 // create Pen using color DarkRed 30 Pen myPen = new Pen( Color.DarkRed ); 31 32 // create SolidBrush using color DarkRed 33 SolidBrush mySolidBrush = 34 new SolidBrush( Color.DarkRed ); 35 Create ComboBoxSelectedIndexChanged event handler Create graphics objectCreate penCreate brush
65
2002 Prentice Hall. All rights reserved. Outline 64 ComboBoxTest.cs 36 // clear drawing area setting it to color White 37 myGraphics.Clear( Color.White ); 38 39 // find index, draw proper shape 40 switch ( imageComboBox.SelectedIndex ) 41 { 42 case 0: // case circle is selected 43 myGraphics.DrawEllipse( 44 myPen, 50, 50, 150, 150 ); //x,y – center, width, high 45 break; // in this case circle 46 case 1: // case rectangle is selected 47 myGraphics.DrawRectangle( 48 myPen, 50, 50, 150, 150 ); //x,y – upper-left of //the rectangle, width, hight 49 break; 50 case 2: // case ellipse is selected 51 myGraphics.DrawEllipse( 52 myPen, 50, 85, 150, 115 ); //x,y – center, width, hight 53 break; 54 case 3: // case pie as a portion of an ellipse: 55 // x,y – upper-left of the rectangle, width, // high, the start angle (in degrees), the sweep angle of pie 55 myGraphics.DrawPie( 56 myPen, 50, 50, 150, 150, 0, 45 ); 57 break; 58 case 4: // case filled circle is selected 59 myGraphics.FillEllipse( 60 mySolidBrush, 50, 50, 150, 150 ); 61 break; 62 case 5: // case filled rectangle is selected 63 myGraphics.FillRectangle( 64 mySolidBrush, 50, 50, 150, 150 ); 65 break; 66 case 6: // case filled ellipse is selected 67 myGraphics.FillEllipse( 68 mySolidBrush, 50, 85, 150, 115 ); 69 break; Switch statement to determine correct object to draw Draw object
66
2002 Prentice Hall. All rights reserved. Outline 65 ComboBoxTest.cs Program Output 70 case 7: // case filled pie is selected 71 myGraphics.FillPie( 72 mySolidBrush, 50, 50, 150, 150, 0, 45 ); 73 break; 74 75 } // end switch 76 77 } // end method imageComboBox_SelectedIndexChanged 78 79 } // end class ComboBoxTest
67
2002 Prentice Hall. All rights reserved. Outline 66 ComboBoxTest.cs Program Output
68
Jozef Goetz contribution, 2012 67 15.9 TreeView control Fig. 15.24 Displaying a sample tree in a TreeView. Click to expand node, displaying child nodes Root node Child nodes Click to collapse node, hiding child nodes Displays a hierarchical collection of labeled items, each represented by a TreeNode.TreeNode Folders of TreeView can be expanded or collapsed by clicking the + or – boxes that appear to their left Displays nodes hierarchically Nodes Objects that contain values –Parent node Contains child nodes Can be expanded or collapsed –Root node First parent node of a tree –Sibling nodes Have same parent node –Child nodes Can have child nodes of their own
69
Jozef Goetz contribution, 2012 68 15. 9 TreeView s Displays a hierarchical collection of labeled items, each represented by a TreeNode. TreeNode For a list of all members of this type, see TreeView Members.TreeView Members System.Object System.MarshalByRefObject System.ComponentModel.Component System.Windows.Forms.Control System.Windows.Forms.TreeView System.ObjectSystem.MarshalByRefObjectSystem.ComponentModel.ComponentSystem.Windows.Forms.Control Parent nodes have children The first parent node is called the root Use Add method to add nodes The tree nodes in this collection are referred to as the root tree nodes. Any tree node that is subsequently added to a root tree node is referred to as a child node. A TreeNode represents a node of a TreeView.TreeView For a list of all members of this type, see TreeNode Members.TreeNode Members System.Object System.MarshalByRefObject System.Windows.Forms.TreeNode System.ObjectSystem.MarshalByRefObject Each TreeNode has a Nodes collection which contains all the children of other TreeNodes objects assigned to the current TreeNode. Nodes You can add, remove, or clone a TreeNode; –when doing so, all child tree nodes are added, removed, or cloned. Each TreeNode can contain a collection of other TreeNode objects.
70
Jozef Goetz contribution, 2012 69 TreeNode.Nodes Property Gets the collection of TreeNode objects assigned to the current tree node.TreeNode public TreeNodeCollection Nodes {get;}TreeNodeCollection The Nodes property can hold a collection of other TreeNode objects.TreeNode Each of the tree node in the collection has a Nodes property that can contain its own TreeNodeCollection. TreeNodeCollection This nesting of tree nodes can make it difficult to navigate a tree structure. The FullPath property makes it easier to determine your location in a tree.FullPath TreeNodeCollection class represents a collection of TreeNode objects. TreeNodeCollectionTreeNode System.Object System.Windows.Forms.TreeNodeCollection System.Object Methods of TreeNodeCollection:TreeNodeCollection Add –Adds a new tree node to the end of the current tree node collection with the specified label text : public virtual TreeNode Add(string);public virtual TreeNode (string); –Adds a previously created tree node to the end of the tree node collection: public virtual int Add (TreeNode);public virtual int Remove –Removes the specified tree node from the tree node collection: public void Remove( TreeNode node );voidTreeNode Remove At –Removes a tree node from the tree node collection at a specified index. public virtual void RemoveAt( int index );voidint
71
Jozef Goetz contribution, 2012 70 15. 9 TreeView
72
Jozef Goetz contribution, 2012 71 15. 9 TreeNode
73
Jozef Goetz contribution, 2012 72 15. 9 TreeView Fig. 15.27 TreeNode Editor obtained after clicking on property Nodes Delete current node
74
2002 Prentice Hall. All rights reserved. Outline 73 TreeViewDirector yStructureTest.c s 1 // Fig. 13.21 ed1: TreeViewDirectoryStructureTest.cs 2 // Using TreeView to display directory structure, use the load event 3 // it creates a tree for the entire C: drive 4 using System; 5 using System.Drawing; 6 using System.Collections; 7 using System.ComponentModel; 8 using System.Windows.Forms; 9 using System.Data; 10 using System.IO; 11 12 public class TreeViewDirectoryStructureTest 13 : System.Windows.Forms.Form 14 { 15 // contains view of c: drive directory structure 16 private System.Windows.Forms.TreeView directoryTreeView; 17 // Dock property visually set to Fill 18 [STAThread] 19 static void Main() 20 { 21 Application.Run( 22 new TreeViewDirectoryStructureTest() ); 23 } 24 25 public void PopulateTreeView( 26 string directoryValue, TreeNode parentNode ) ;//takes (name, root node) 27 { 28 // populate current node with subdirectories 29 string[] directoryArray = 30 Directory.GetDirectories( directoryValue );// System.IO namespace 31 // returns an array of subdirectories Class that creates children of root Get subdirectories of root
75
2002 Prentice Hall. All rights reserved. Outline 74 TreeViewDirector yStructureTest.c s 32 // populate current node with subdirectories 33 try 34 { 35 if ( directoryArray.Length != 0 ) 36 { 37 // for every subdirectory, create new TreeNode, 38 // add as child of current node and recursively 39 // populate child nodes with subdirectories 40 foreach ( string directory in directoryArray ) // 1 st time is "C:\\", 41 { 42 // create TreeNode for current directory 43 TreeNode myNode = new TreeNode( directory ); 44 45 // add current directory node to parent node 46 parentNode.Nodes.Add( myNode ); 47 48 // recursively populate every subdirectory 49 PopulateTreeView( directory, myNode ); 50 } 51 52 } // end if 53 } 54 55 // catch exception 56 catch ( UnauthorizedAccessException ) 57 { 58 parentNode.Nodes.Add( "Access denied" ); // instead of displaying // the subdirectories 59 } 60 61 } // end PopulateTreeView 62 Catches security exception Create new nodeRecursive call to finish tree
76
2002 Prentice Hall. All rights reserved. Outline 75 TreeViewDirector yStructureTest.c s 63 // called by system when form loads 64 private void TreeViewDirectoryStructureTest_Load( 65 object sender, System.EventArgs e) 66 { 67 // add c:\ drive to directoryTreeView and 68 // insert its subfolders 69 directoryTreeView.Nodes.Add( "C:\\" ); 70 PopulateTreeView( 71 "C:\\", directoryTreeView.Nodes[ 0 ] ) ;//takes (name, root node’s index) 72 } 73 74 } // end class TreeViewDirectoryStructure Create root
77
2002 Prentice Hall. All rights reserved. Outline 76 TreeViewDirector yStructureTest.c s Program Output
78
Jozef Goetz contribution, 2012 77 Outline TreeViewDirectory StructureForm.cs (1 of 4) (a)(a) (b)(b)
79
Jozef Goetz contribution, 2012 78 Outline TreeViewDirectoryStr uctureForm.cs (2 of 4)
80
Jozef Goetz contribution, 2012 79 Outline TreeViewDirectoryStr uctureForm.cs (3 of 4) (a)(a) (b)(b)
81
Jozef Goetz contribution, 2012 80 Outline TreeViewDirectoryStr uctureForm.cs (4 of 4) (a) (b)
82
Jozef Goetz contribution, 2012 81 15.10 ListViews ListView Control Similar to ListBox View and select from multiple items Select multiple items at same time Displays icons along side of list items
83
Jozef Goetz contribution, 2012 82 15. 10 ListView s similar to ListBox Represents a Windows list view control, which displays a collection of items that can be displayed using 1 of 4 different views as large icons, as small icons, or as small icons in a vertical list. For a list of all members of this type, see ListView Members.ListView Members System.Object System.MarshalByRefObject System.ComponentModel.Component System.Windows.Forms.Control System.Windows.Forms.ListView System.ObjectSystem.MarshalByRefObjectSystem.ComponentModel.ComponentSystem.Windows.Forms.Control Displays list of items Can select one or more items from list Displays icons to go along with items A ListView control allows you to display a list of items with item text and, optionally, an icon to identify the type of item. For example, the Windows Explorer list of files is similar in appearance to a ListView control. It displays a list of the files and folders currently selected in the tree. Each file and folder displays an icon associated with it to help identify the type of file or folder.
84
Jozef Goetz contribution, 2012 83 ListView.Items Property public ListView.ListViewItemCollection Items {get;}ListView.ListViewItemCollection Using the ListView.ListViewItemCollection returned by this property, you can add items, remove items, and obtain a count of itemsListView.ListViewItemCollection System.Object System.Windows.Forms.ListView.ListViewItemCollection System.Object this class represents the collection of items and stores them in a ListView control.ListView public class ListView.ListViewItemCollection : IList, ICollection, IEnumerable Methods of ListView.ListViewItemCollection: ListView.ListViewItemCollection Add - Adds an item to the collection of items Add where: public virtual ListViewItem Add( ListViewItem value );ListViewItem Insert -Inserts an item into the collection at the specified index Insert where: public ListViewItem Insert( int index, ListViewItem item );ListViewItemintListViewItem Remove removes the specified item from the collection. Remove where: public virtual void Remove( ListViewItem item );voidListViewItem
85
Jozef Goetz contribution, 2012 84 ListViewItem class The ListViewItem class defines the appearance, behavior, and data associated with an item that is displayed in the ListView control. ListViewItem objects System.Object System.Windows.Forms.ListViewItem System.Object can be displayed in the ListView control in 1 of 4 different views. LargeIcon (large icon displayed, items can be in multiple columns), SmallIcon (small icon displayed), List (small icons displayed, items appear in a single column) and Details (like List, but multiple columns of information can be displayed per item). Items can also have subitems that contain information that is related to the parent item. The 4 view style, details view, allows you to display the item and its subitems in a grid with column headers that can be used to identify the information being displayed in a subitem.
86
Jozef Goetz contribution, 2012 85 DirectoryInfo class Exposes instance methods for creating, moving, and enumerating through directories and subdirectories. For a list of all members of this type, see DirectoryInfo Members. DirectoryInfo Members System.Object System.MarshalByRefObject System.IO.FileSystemInfo System.IO.DirectoryInfo System.ObjectSystem.MarshalByRefObjectSystem.IO.FileSystemInfo
87
Jozef Goetz contribution, 2012 86 FileInfo class Provides instance methods for the creation, copying, deletion, moving, and opening of files, and aids in the creation of FileStream objects.FileStream For a list of all members of this type, see FileInfo Members.FileInfo Members System.Object System.MarshalByRefObject System.IO.FileSystemInfo System.IO.FileInfo System.ObjectSystem.MarshalByRefObjectSystem.IO.FileSystemInfo
88
Jozef Goetz contribution, 2012 87 15. 10 ListView s
89
Jozef Goetz contribution, 2012 88 15. 10 ListView s Fig. 15.30 Image Collection Editor window for an ImageList component. It can be dragged from the ToolBox.
90
2002 Prentice Hall. All rights reserved. Outline 89 ListViewTest.cs 1 // Fig. 13.24 ed1 or 15.31 ed3, (ed4): ListViewTest.cs 2 // Displaying directories and their contents in ListView. 3 // it scans the contents of the directory as it browses 4 using System; 5 using System.Drawing; 6 using System.Collections; 7 using System.ComponentModel; 8 using System.Windows.Forms; 9 using System.Data; 10 using System.IO; 11 12 public class ListViewTest : System.Windows.Forms.Form 13 { 14 // display labels for current location 15 // in directory tree 16 private System.Windows.Forms.Label currentLabel; 17 private System.Windows.Forms.Label displayLabel; //currentDirectory 18 19 // display contents of current directory 20 private System.Windows.Forms.ListView browserListView; 21 22 // specifies images for file icons and folder icons 23 private System.Windows.Forms.ImageList fileFolder; 24 25 // get current directory 26 string currentDirectory = 27 Directory.GetCurrentDirectory(); 28 29 [STAThread] 30 static void Main() 31 { 32 Application.Run( new ListViewTest() ); 33 } 34 Create Image ListLoad the current directory (a)
91
2002 Prentice Hall. All rights reserved. Outline 90 ListViewTest.cs 35 // browse directory user clicked or go up one level 36 private void browserListView_Click( 37 object sender, System.EventArgs e ) 38 { 39 // ensure item selected 40 if ( browserListView.SelectedItems.Count != 0 ) 41 { 42 // if first item selected, go up one level 43 if ( browserListView.Items[ 0 ].Selected ) 44 { 45 // create DirectoryInfo object for directory 46 DirectoryInfo directoryObject = 47 new DirectoryInfo( currentDirectory ); 48 49 // if directory has parent, load it 50 if ( directoryObject.Parent != null ) //test property Parent 51 LoadFilesInDirectory( 52 directoryObject.Parent.FullName ); 53 } 54 55 // selected directory or file is not as the 1 st item 56 else 57 { 58 // directory or file chosen 59 string chosen = 60 browserListView.SelectedItems[ 0 ].Text; //gives the text // of the selected item (1 st item in the collection) 61 62 // if item selected is directory 63 if ( Directory.Exists( currentDirectory + 64 "\\" + chosen ) ) 65 { Test to see if at rootReturn parent of current directory Check if selected item is directory Test if item is selected If first item selected go up one level Make directory information
92
2002 Prentice Hall. All rights reserved. Outline 91 ListViewTest.cs 66 // load subdirectory 67 // if in c:\, do not need '\', 68 // otherwise we do 69 if ( currentDirectory == "C:\\" ) 70 LoadFilesInDirectory( 71 currentDirectory + chosen ); // has “//” already 72 else 73 LoadFilesInDirectory( 74 currentDirectory + "\\" + chosen ); 75 } //end if 76 77 } // end else 78 79 // update displayLabel 80 displayLabel.Text = currentDirectory; 81 82 } // end if 83 84 } // end method browserListView_Click 85 86 // display files/subdirectories of current directory 87 public void LoadFilesInDirectory( 88 string currentDirectoryValue ) 89 { 90 // load directory information and display 91 try 92 { 93 // clear ListView and set first item 94 browserListView.Items.Clear(); 95 browserListView.Items.Add( "Go Up One Level" ); // 1 st item to display 96 Class to load files in current directory Update to display current directory Load subdirectory (a)(a)
93
2002 Prentice Hall. All rights reserved. Outline 92 ListViewTest.cs 97 // update current directory 98 currentDirectory = currentDirectoryValue; 99 DirectoryInfo newCurrentDirectory = // if no permission, an exception is thrown 100 new DirectoryInfo( currentDirectory ); //System.IO namespace 101 102 // put files and directories into arrays 103 DirectoryInfo[] directoryArray = 104 newCurrentDirectory.GetDirectories(); 105 106 FileInfo[] fileArray = 107 newCurrentDirectory.GetFiles(); 108 109 // add directory names to ListView 110 foreach ( DirectoryInfo dir in directoryArray ) 111 { 112 // add directory to ListView 113 ListViewItem newDirectoryItem = 114 browserListView.Items.Add( dir.Name ); 115 116 // set directory image 117 newDirectoryItem.ImageIndex = 0; //this value 0 was setup via line 153 // in the ImageList // Collection Editor – also see a few slides back 118 } // Fig. 14.30 119 120 // add file names to ListView 121 foreach ( FileInfo file in fileArray ) 122 { 123 // add file to ListView 124 ListViewItem newFileItem = 125 browserListView.Items.Add( file.Name ); 126 127 newFileItem.ImageIndex = 1; //this value 0 was setup via line 154 // in the ImageList // Collection Editor – also see a few slides back 128 } // Fig. 14.30 129 } // end try 130 Get subdirectories of current directory, different than a former program Get files of current directory Add directory to list Add file to list
94
2002 Prentice Hall. All rights reserved. Outline 93 131 // access denied 132 catch ( UnauthorizedAccessException exception ) 133 { 134 MessageBox.Show( 135 "Warning: Some fields may not be " + 136 "visible due to permission settings", 137 "Attention", 0, MessageBoxIcon.Warning ); 138 } 139 140 } // end method LoadFilesInDirectory 141 142 // handle load event when Form displayed for first time 143 private void ListViewTest_Load( 144 object sender, System.EventArgs e ) 145 { 146 // set image list 147 Image folderImage = Image.FromFile( 148 currentDirectory + "\\images\\folder.bmp" ); 149 150 Image fileImage = Image.FromFile( currentDirectory + 151 "\\images\\file.bmp" ); 152 // add to ImageList control 153 fileFolder.Images.Add( folderImage ); 154 fileFolder.Images.Add( fileImage ); 155 156 // load current directory into browserListView control 157 LoadFilesInDirectory( currentDirectory ); 158 displayLabel.Text = currentDirectory; 159 160 } // end method ListViewTest_Load 161 162 } // end class ListViewTest Security exception handler Load Images (a )
95
2002 Prentice Hall. All rights reserved. Outline 94 ListViewTest.cs Program Output (b) (c) (a)
96
Jozef Goetz contribution, 2012 95 Software Engineering Observation 15.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.
97
Jozef Goetz contribution, 2012 96 15.11 TabControl Creates tabbed windows to fit a large # of controls and group them similar to Panels and GroupBoxes Saves space using TabPage objects Windows called TabPage objects TabPages can contain controls TabPages have own Click event for when tab is clicked TabPage TabControl Controls in TabPage
98
Jozef Goetz contribution, 2012 97 15. 11 Tab Control s Fig. Tabbed pages in Visual Studio.NET. Tab pages
99
Jozef Goetz contribution, 2012 98 15. 11 Tab Control s Fig. 15.34Adding TabPage s to the TabControl First the TabControl was dragged and dropped onto a form in design mode.
100
Jozef Goetz contribution, 2012 99 15. 11 Tab Control s
101
2002 Prentice Hall. All rights reserved. Outline 100 UsingTabs.cs 1 // Fig. 15.36: UsingTabs.cs 2 // Using TabControl to display various font settings. 3 4 using System; 5 using System.Drawing; 6 using System.Collections; 7 using System.ComponentModel; 8 using System.Windows.Forms; 9 using System.Data; 10 11 public class UsingTabs : System.Windows.Forms.Form 12 { 13 // output label reflects text changes 14 private System.Windows.Forms.Label displayLabel; 15 16 // table control containing table pages colorTabPage, 17 // sizeTabPage, messageTabPage and aboutTabPage 18 private System.Windows.Forms.TabControl 19 optionsTabControl; 20 21 // table page containing color options 22 private System.Windows.Forms.TabPage colorTabPage; 23 private System.Windows.Forms.RadioButton 24 greenRadioButton; 25 private System.Windows.Forms.RadioButton redRadioButton; 26 private System.Windows.Forms.RadioButton 27 blackRadioButton; 28 Color tab Color buttons for color tab
102
2002 Prentice Hall. All rights reserved. Outline 101 UsingTabs.cs 29 // table page containing font size options 30 private System.Windows.Forms.TabPage sizeTabPage; 31 private System.Windows.Forms.RadioButton 32 size20RadioButton; 33 private System.Windows.Forms.RadioButton 34 size16RadioButton; 35 private System.Windows.Forms.RadioButton 36 size12RadioButton; 37 38 // table page containing text display options 39 private System.Windows.Forms.TabPage messageTabPage; 40 private System.Windows.Forms.RadioButton 41 goodByeRadioButton; 42 private System.Windows.Forms.RadioButton 43 helloRadioButton; 44 45 // table page containing about message 46 private System.Windows.Forms.TabPage aboutTabPage; 47 private System.Windows.Forms.Label messageLabel; 48 49 [STAThread] 50 static void Main() 51 { 52 Application.Run( new UsingTabs() ); 53 } 54 55 // event handler for black color radio button 56 private void blackRadioButton_CheckedChanged( 57 object sender, System.EventArgs e ) 58 { 59 displayLabel.ForeColor = Color.Black; 60 } 61 Size tab Size buttons Message tabAbout tabEvent handler
103
2002 Prentice Hall. All rights reserved. Outline 102 UsingTabs.cs 62 // event handler for red color radio button 63 private void redRadioButton_CheckedChanged( 64 object sender, System.EventArgs e ) 65 { 66 displayLabel.ForeColor = Color.Red; 67 } 68 69 // event handler for green color radio button 70 private void greenRadioButton_CheckedChanged( 71 object sender, System.EventArgs e ) 72 { 73 displayLabel.ForeColor = Color.Green; 74 } 75 76 // event handler for size 12 radio button 77 private void size12RadioButton_CheckedChanged( 78 object sender, System.EventArgs e ) 79 { 80 displayLabel.Font = 81 new Font( displayLabel.Font.Name, 12 ); 82 } 83 84 // event handler for size 16 radio button 85 private void size16RadioButton_CheckedChanged( 86 object sender, System.EventArgs e ) 87 { 88 displayLabel.Font = 89 new Font( displayLabel.Font.Name, 16 ); 90 } 91 Event handlers
104
2002 Prentice Hall. All rights reserved. Outline 103 UsingTabs.cs 92 // event handler for size 20 radio button 93 private void size20RadioButton_CheckedChanged( 94 object sender, System.EventArgs e ) 95 { 96 displayLabel.Font = 97 new Font( displayLabel.Font.Name, 20 ); 98 } 99 100 // event handler for message "Hello!" radio button 101 private void helloRadioButton_CheckedChanged( 102 object sender, System.EventArgs e ) 103 { 104 displayLabel.Text = "Hello!"; 105 } 106 107 // event handler for message "Goodbye!" radio button 108 private void goodByeRadioButton_CheckedChanged( 109 object sender, System.EventArgs e ) 110 { 111 displayLabel.Text = "Goodbye!"; 112 } 113 114 } // end class UsingTabs Event handlers
105
2002 Prentice Hall. All rights reserved. Outline 104 UsingTabs.cs Program Output
106
Jozef Goetz contribution, 2012 105 Software Engineering Observation 15.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.
107
Jozef Goetz contribution, 2012 106 15.12 Multiple Document Interface (MDI) Windows MDI Allows multiple windows Parent window –Application window –Can have many child windows Child window –Cannot be parent –Has exactly one parent –Cannot be moved outside parent –Functionality can be different than other child windows from same parent
108
Jozef Goetz contribution, 2012 107 15.12 Multiple Document Interface (MDI) Windows Fig. 15.37MDI parent and MDI child. MDI parent MDI child
109
Jozef Goetz contribution, 2012 108 15.12 Multiple Document Interface (MDI) Windows Fig. 15.38SDI and MDI forms. Single Document Interface (SDI)Multiple Document Interface (MDI)
110
Jozef Goetz contribution, 2012 109 15. 12 Multiple-Document Interface (MDI) Windows Users can edit multiple documents at once Usually more complex then single-document-interface (SDI) applications SDI apps have been created so far. The application window of an MDI program is called parent, and each window inside the app is referred to as a child window A max of 1 child (window) w. can be actived at once A child w. cannot be parents themselves and cannot be moved outside their parent A child’s functionality can be different from the functionality of others child windows of the parent E.g. one child might edit images, another – edit text or display network traffic
111
Jozef Goetz contribution, 2012 110 15. 12 Multiple-Document Interface (MDI) Windows Parent and child menus can be merged Based on MergeOrder property Child windows can be arranged in parent window: Tiled windows: completely fill parent, no overlap Either horizontal or vertical Cascaded windows: overlap, same size, display title bar ArrangeIcons: arranges icons for minimized windows (c) TileHorizontal (a) ArrangeIcons (b) Cascade (d) Tilevertical
112
Jozef Goetz contribution, 2012 111 15.12 Multiple Document Interface (MDI) Windows Parent’s icons: minimize, maximize and close Maximized child’s icons: minimize, restore and close Minimized child’s icons: restore, maximize and close Parent’s title bar displays maximized child Fig. 15.40 Minimized and maximized child windows.
113
Jozef Goetz contribution, 2012 112 15.12 Multiple Document Interface (MDI) Windows
114
Jozef Goetz contribution, 2012 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. ChildFormClass childForm = New ChildFormClass (); childForm.MdiParent = parentForm; childForm.Show(); In most cases, the parent Form creates the child, so the parentForm reference is this. 15.12 Multiple Document Interface (MDI) Windows (Cont.)
115
Jozef Goetz contribution, 2012 114 The list of open child windows Fig. 15.41 MenuStrip property MdiWindowListItem example is setup as windowToolStripMenuItem Separator bar and child windows 9 or more child windows enables the More Windows... option Child windows list Property MdiWindowListItem of class MenuStrip specifies which menu, if any, displays a list of open child windows.
116
Jozef Goetz contribution, 2012 The list of open child windows ©1992-2011 by Pearson Education, Inc. All Rights Reserved.
117
Jozef Goetz contribution, 2012 116 Fig. 15.42 | MdiLayout enumeration values. (c) TileHorizontal (a) ArrangeIcons (b) Cascade (d) Tilevertical
118
Jozef Goetz contribution, 2012 117 Outline UsingMDIForm.cs ( 1 of 6 ) Class UsingMDIForm (Fig. 15.43) demonstrates MDI windows. Fig. 15.43 | MDI parent-window class. (Part 1 of 6.)
119
Jozef Goetz contribution, 2012 118 UsingMDIForm.cs ( 2 of 6 ) Fig. 15.43 | MDI parent-window class. (Part 2 of 6.) Adding a new child Form with certain properties. See next slides. Adding a new child Form with certain properties.
120
Jozef Goetz contribution, 2012 119 Outline UsingMDIForm.cs ( 3 of 6 ) Fig. 15.43 | MDI parent-window class. (Part 3 of 6.) Adding a new child Form with certain properties.
121
Jozef Goetz contribution, 2012 120 Outline UsingMDIForm.cs ( 4 of 6 ) Fig. 15.43 | MDI parent-window class. (Part 4 of 6.) Setting the layout of child Form s. MDI containers allow you to organize the placement of its child windows. Method LayoutMdi takes a value of the MdiLayout enumeration
122
Jozef Goetz contribution, 2012 Fig. 15.43 | MDI parent-window class. (Part 5, 6 of 6.) a) Creating a child windowb) Viewing the child window c) Changing child window organizationd) Child windows in Cascade view
123
Jozef Goetz contribution, 2012 122 Outline ChildForm.cs ( 1 of 2 ) Define the MDI child class by right clicking the project in the Solution Explorer and selecting Add > Windows Form…. Name the new class ChildForm (Fig. 15.44). Fig. 15.44 | MDI child ChildForm. (Part 1 of 2.)
124
Jozef Goetz contribution, 2012 123 Outline ChildForm.cs ( 2 of 2 ) Fig. 15.44 | MDI child ChildForm. (Part 2 of 2.) Setting the title-bar text. Retrieving the specified image resource and displaying it.
125
Jozef Goetz contribution, 2012 124 15.13 Visual Inheritance Create Form by inheriting from another Form Derived Form inherits functionality of base Form Derived Form inherits visual aspects size, color, background etc. of base Form
126
Jozef Goetz contribution, 2012 125 Outline VisualInhe ritanceFor m.cs Right click the project name in the Solution Explorer and select Properties, then choose the Application tab. In the Output type drop-down list, change Windows Application to Class Library. Building the project produces the.dll.
127
Jozef Goetz contribution, 2012 126 Outline VisualInheritance TestForm.cs (1 of 2) Derived class cannot modify these controls. Derived class can modify this control.
128
Jozef Goetz contribution, 2012 Derived class cannot modify these controls. Derived class can modify this control.
129
Jozef Goetz contribution, 2012 The.NET Framework allows you to create custom controls. Custom controls appear in the user’s Toolbox. There are multiple ways to create a custom control, depending on the level of customization that you want 15.14 User-Defined Controls ©1992-2011 by Pearson Education, Inc. All Rights Reserved.
130
Jozef Goetz contribution, 2012 129 15.14 User-Defined Controls 2. User-defined control can inherit from class Control and Component System.Object System.Object System.MarshalByRefObject System.ComponentModel.Component System.Windows.Forms.Control System.Windows.Forms.ScrollableControl System.Windows.Forms.ContainerControl System.Windows.Forms.UserControlSystem.MarshalByRefObjectSystem.ComponentModel.ComponentSystem.Windows.Forms.ScrollableControlSystem.Windows.Forms.ContainerControl System.Object System.Object System.MarshalByRefObject System.ComponentModel.Component System.Windows.Forms.Control 1. Custom Control is a base class for the User Control classesSystem.MarshalByRefObjectSystem.ComponentModel.Component Ex: If existing control is a label. User Control class can change appearance of a label by inheriting from class label by overriding method OnPaint.
131
Jozef Goetz contribution, 2012 130 15.14 User-Defined Controls We will implement a UserControl composed of a label and a timer – whenever the timer generates an event, the label is updated to reflect the current time Timers are invisible components that reside on a form and generate Tick events at a set interval (it is set by the Interval property) System.Object System.Object System.MarshalByRefObject System.ComponentModel.Component System.Windows.Forms.TimerSystem.MarshalByRefObjectSystem.ComponentModel.Component For a list of all members of this type, see Timer Members.Timer Members
132
Jozef Goetz contribution, 2012 Timer s are non-visual components that generate Tick events at a set interval. The Timer ’s Interval property defines the number of milliseconds between events. Create a UserControl class for the project by selecting Project > Add User Control …. We name the file (and the class) ClockUserControl. Add a Label and a Timer to the UserControl. Set the Timer interval to 1000 milliseconds. clockTimer must be enabled by setting Enabled to true. Drag it the user control to the Form and run the app. p.621 – 2 nd last paragraph 15.14 User-Defined Controls (Cont.)
133
2002 Prentice Hall. All rights reserved. Outline 132 ClockUserControl.cs 1 // Fig. 15.49: ClockUserControl.cs 2 // User-defined control with a timer and a label. 3 4 using System; 5 using System.Collections; 6 using System.ComponentModel; 7 using System.Drawing; 8 using System.Data; 9 using System.Windows.Forms; 10 11 public class ClockUserControl 12 : System.Windows.Forms.UserControl 13 { 14 private System.Windows.Forms.Timer clockTimer; 15 private System.Windows.Forms.Label displayLabel; 16 17 // update label at every tick 18 private void clockTimer_Tick( 19 object sender, System.EventArgs e ) 20 { 21 // get current time (Now), convert to string 22 displayLabel.Text = DateTime.Now.ToLongTimeString(); 23 24 } // end method clockTimer_Tick 25 26 } // end class ClockUserControl TimerLabelUpdate label methodDisplay current time
134
Jozef Goetz contribution, 2012 133 15.14 User-Defined Controls
135
Jozef Goetz contribution, 2012 134 15.14 User-Defined Controls Fig. Custom-control creation. – to create it see p.621 bottom
136
Jozef Goetz contribution, 2012 15.14 User-Defined Controls (Cont.) Create a new Windows application. Right click the ToolBox and select Choose Items…. In the Choose Toolbox Items dialog, click Browse… Select the. dll file that you created. The item will then appear in the Choose Toolbox Items dialog (Fig. 15.52). Check this item and click OK to add the item to the Toolbox.
137
Jozef Goetz contribution, 2012 136 15.14 User-Defined Controls Fig. Custom control added to a Form. New Toolbox icon Newly inserted control
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.