Chapter 14 Graphical User Interfaces Part 2 Outline

  • Slides: 40
Download presentation
Chapter 14 – Graphical User Interfaces Part 2 Outline 14. 9 Tree. Views 14.

Chapter 14 – Graphical User Interfaces Part 2 Outline 14. 9 Tree. Views 14. 10 List. Views 14. 11 Tab. Control 14. 12 Multiple Document Interface (MDI) Timer Class 2007 Dr. Natheer Khasawneh. All rights reserved. 1

2 14. 9 Tree. Views • • Displays nodes hierarchically Parent nodes have children

2 14. 9 Tree. Views • • Displays nodes hierarchically Parent nodes have children The first parent node is called the root Use Add method to add nodes 2007 Dr. Natheer Khasawneh. All rights reserved.

3 14. 9 Tree. View Click to expand node, displaying child nodes Root node

3 14. 9 Tree. View Click to expand node, displaying child nodes Root node Click to collapse node, hiding child nodes Fig. 14. 24 Displaying a sample tree in a Tree. View. 2007 Dr. Natheer Khasawneh. All rights reserved. Child nodes

4 14. 9 Tree. View 2007 Dr. Natheer Khasawneh. All rights reserved.

4 14. 9 Tree. View 2007 Dr. Natheer Khasawneh. All rights reserved.

14. 9 Tree. Node 2007 Dr. Natheer Khasawneh. All rights reserved. 5

14. 9 Tree. Node 2007 Dr. Natheer Khasawneh. All rights reserved. 5

6 14. 9 Tree. View Fig. 14. 27 Tree. Node Editor. 2007 Dr. Natheer

6 14. 9 Tree. View Fig. 14. 27 Tree. Node Editor. 2007 Dr. Natheer Khasawneh. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 18 19 20 21 22 23 24 25 // Fig. 14. 28: Tree. View. Directory. Structure. Form. cs // Using Tree. View to display directory structure. using System; using System. Windows. Forms; using System. IO; Outline 7 // Form uses Tree. View to display directory structure public partial class Tree. View. Directory. Structure. Form : Form { string substring. Directory; // store last part of full path name // default constructor public Tree. View. Directory. Structure. Form () { Initialize. Component(); } // end constructor // populate current node with subdirectories public void Populate. Tree. View( string directory. Value, Tree. Node parent. Node ) { // array stores all subdirectories in the directory string[] directory. Array = Directory. Get. Directories ( directory. Value ); Tree. View. Directory. Stru cture. Form. cs 2007 Dr. Natheer Khasawneh.

26 27 28 29 30 31 32 33 34 35 36 37 38 39

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 // populate current node with subdirectories try { // check to see if any subdirectories are present if ( directory. Array. Length != 0 ) { // for every subdirectory, create new Tree. Node, // add as a child of current node and recursively // populate child nodes with subdirectories foreach ( string directory in directory. Array ) { // obtain last part of path name from the full path name // by finding the last occurence of "" and returning the // part of the path name that comes after this occurence substring. Directory = directory. Substring( directory. Last. Index. Of( '\' ) + 1, directory. Length - directory. Last. Index. Of( '\' ) - 1 ); // create Tree. Node for current directory Tree. Node my. Node = new Tree. Node( substring. Directory ); // add current directory node to parent node parent. Nodes. Add( my. Node ); Outline 8 Tree. View. Directory. Stru cture. Form. cs // recursively populate every subdirectory Populate. Tree. View( directory, my. Node ); } // end foreach } // end if } //end try // catch exception catch ( Unauthorized. Access. Exception ) { parent. Nodes. Add( "Access denied" ); } // end catch } // end method Populate. Tree. View 2007 Dr. Natheer Khasawneh.

63 // handles enter. Button click event 64 private void enter. Button_Click( object sender,

63 // handles enter. Button click event 64 private void enter. Button_Click( object sender, Event. Args e ) 65 { 66 // clear all nodes 67 directory. Tree. View. Nodes. Clear (); 68 69 // check if the directory entered by user exists 70 // if it does then fill in the Tree. View, 71 // if not display error Message. Box 72 if ( Directory. Exists( input. Text. Box. Text ) ) 73 { 74 // add full path name to directory. Tree. View 75 directory. Tree. View. Nodes. Add ( input. Text. Box. Text ); 76 77 // insert subfolders 78 Populate. Tree. View( 79 input. Text. Box. Text, directory. Tree. View. Nodes[ 0 ] ); 80 } 81 // display error Message. Box if directory not found 82 else 83 Message. Box. Show( input. Text. Box. Text + " could not be found. ", 84 "Directory Not Found", Message. Box. Buttons. OK, 85 Message. Box. Icon. Error ); 86 } // end method enter. Button_Click 87 } // end class Tree. View. Directory. Structure. Form Outline 9 Tree. View. Directory. Stru cture. Form. cs 2007 Dr. Natheer Khasawneh.

Outline 10 Tree. View. Director y. Structure. Test. c s Program Output 2007 Dr.

Outline 10 Tree. View. Director y. Structure. Test. c s Program Output 2007 Dr. Natheer Khasawneh.

11 14. 10 List. Views • Displays list of items – Can select one

11 14. 10 List. Views • Displays list of items – Can select one or more items from list – Displays icons to go along with items 2007 Dr. Natheer Khasawneh. All rights reserved.

14. 10 List. Views 2007 Dr. Natheer Khasawneh. All rights reserved. 12

14. 10 List. Views 2007 Dr. Natheer Khasawneh. All rights reserved. 12

13 14. 10 List. Views Fig. 14. 30 Image Collection Editor window for an

13 14. 10 List. Views Fig. 14. 30 Image Collection Editor window for an Image. List component. 2007 Dr. Natheer Khasawneh. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 ); 37 // Fig. 14. 31: List. View. Test. Form. cs // Displaying directories and their contents in List. View. using System; using System. Drawing; using System. Windows. Forms; using System. IO; Outline 14 // Form contains a List. View which displays // folders and files in a directory public partial class List. View. Test. Form : Form { // store current directory string current. Directory = Directory. Get. Current. Directory (); // default constructor public List. View. Test. Form() { Initialize. Component(); } // end constructor // browse directory user clicked or go up one level private void browser. List. View_Click( object sender, Event. Args e ) { // ensure an item is selected if ( browser. List. View. Selected. Items. Count != 0 ) { // if first item selected, go up one level if ( browser. List. View. Items[ 0 ]. Selected ) { // create Directory. Info object for directory Directory. Info directory. Object = new Directory. Info( current. Directory ); List. View. Test. cs // if directory has parent, load it if ( directory. Object. Parent != null ) Load. Files. In. Directory( directory. Object. Parent. Full. Name } // end if 2007 Dr. Natheer Khasawneh.

39 40 41 42 43 44 45 46 47 48 do 49 50 51

39 40 41 42 43 44 45 46 47 48 do 49 50 51 52 53 54 55 56 57 58 59 60 61 // selected directory or file else { // directory or file chosen string chosen = browser. List. View. Selected. Items [ 0 ]. Text; Outline 15 // if item selected is directory, load selected directory if ( Directory. Exists( current. Directory + @"" + chosen ) ) { // if currently in C: , do not need ''; otherwise we if ( current. Directory == @"C: " ) Load. Files. In. Directory( current. Directory + chosen ); else Load. Files. In. Directory( current. Directory + @"" + chosen ); } // end if } // end else List. View. Test. cs // update display. Label. Text = current. Directory; } // end if } // end method browser. List. View_Click 2007 Dr. Natheer Khasawneh.

62 63 64 65 66 67 68 69 70 71 72 73 74 75

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 // display files/subdirectories of current directory public void Load. Files. In. Directory( string current. Directory. Value ) { // load directory information and display try { // clear List. View and set first item browser. List. View. Items. Clear (); browser. List. View. Items. Add ( "Go Up One Level" ); Outline 16 // update current directory current. Directory = current. Directory. Value; Directory. Info new. Current. Directory = new Directory. Info( current. Directory ); // put files and directories into arrays Directory. Info[] directory. Array = new. Current. Directory. Get. Directories (); File. Info[] file. Array = new. Current. Directory. Get. Files (); List. View. Test. cs // add directory names to List. View foreach ( Directory. Info dir in directory. Array ) { // add directory to List. View. Item new. Directory. Item = browser. List. View. Items. Add ( dir. Name ); new. Directory. Item. Image. Index = 0; } // end foreach // set directory image 2007 Dr. Natheer Khasawneh.

92 // add file names to List. View 93 foreach ( File. Info file

92 // add file names to List. View 93 foreach ( File. Info file in file. Array ) 94 { 95 // add file to List. View 96 List. View. Item new. File. Item = 97 browser. List. View. Items. Add ( file. Name ); 98 99 new. File. Item. Image. Index = 1; // set file image 100 } // end foreach 101 } // end try 102 103 // access denied 104 catch ( Unauthorized. Access. Exception ) 105 { 106 Message. Box. Show( "Warning: Some fields may not be " + 107 "visible due to permission settings" , 108 "Attention", 0, Message. Box. Icon. Warning ); 109 } // end catch 110 } // end method Load. Files. In. Directory 111 112 // handle load event when Form displayed for first time 113 private void List. View. Test. Form_Load( object sender, Event. Args e ) 114 { 115 // set Image list 116 Image folder. Image = Image. From. File( 117 current. Directory + @"imagesfolder. bmp" ); 118 119 Image file. Image = Image. From. File( 120 current. Directory + @"imagesfile. bmp" ); 121 122 file. Folder. Images. Add( folder. Image ); 123 file. Folder. Images. Add( file. Image ); 124 125 // load current directory into browser. List. View 126 Load. Files. In. Directory( current. Directory ); 127 display. Label. Text = current. Directory; 128 } // end method List. View. Test. Form_Load 129 } // end class List. View. Test. Form Outline 17 List. View. Test. cs 2007 Dr. Natheer Khasawneh.

Outline 18 List. View. Test. cs Program Output 2007 Dr. Natheer Khasawneh.

Outline 18 List. View. Test. cs Program Output 2007 Dr. Natheer Khasawneh.

19 14. 11 Tab. Control • Creates tabbed windows • Windows called Tab. Page

19 14. 11 Tab. Control • Creates tabbed windows • Windows called Tab. Page objects – Tab. Pages can have controls – Tabpages have own Click event for when tab is clicked 2007 Dr. Natheer Khasawneh. All rights reserved.

20 Tab pages 14. 11 Tab Controls Fig. 14. 32 Tabbed pages in Visual

20 Tab pages 14. 11 Tab Controls Fig. 14. 32 Tabbed pages in Visual Studio. NET. 2007 Dr. Natheer Khasawneh. All rights reserved.

21 14. 11 Tab Controls Tab. Page Tab. Controls in Tab. Page Fig. 14.

21 14. 11 Tab Controls Tab. Page Tab. Controls in Tab. Page Fig. 14. 33 Example Tab. Control with Tab. Pages. 2007 Dr. Natheer Khasawneh. All rights reserved.

22 14. 11 Tab Controls Fig. 14. 35 Adding Tab. Pages to the Tab.

22 14. 11 Tab Controls Fig. 14. 35 Adding Tab. Pages to the Tab. Control. 2007 Dr. Natheer Khasawneh. All rights reserved.

23 14. 11 Tab Controls 2007 Dr. Natheer Khasawneh. All rights reserved.

23 14. 11 Tab Controls 2007 Dr. Natheer Khasawneh. All rights reserved.

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 // Fig. 14. 36: Using. Tabs. Form. cs // Using Tab. Control to display various font settings. using System; using System. Drawing; using System. Windows. Forms; Outline 24 // Form uses Tabs and Radio. Buttons to display various font settings public partial class Using. Tabs. Form : Form { // default constructor public Using. Tabs. Form() { Initialize. Component(); } // end constructor // event handler for Black Radio. Button private void black. Radio. Button_Checked. Changed ( object sender, Event. Args e ) { display. Label. Fore. Color = Color. Black; // change font color to black } // end method black. Radio. Button_Checked. Changed Using. Tabs. cs // event handler for Red Radio. Button private void red. Radio. Button_Checked. Changed ( object sender, Event. Args e ) { display. Label. Fore. Color = Color. Red; // change font color to red } // end method red. Radio. Button_Checked. Changed // event handler for Green Radio. Button private void green. Radio. Button_Checked. Changed ( object sender, Event. Args e ) { display. Label. Fore. Color = Color. Green; // change font color to green } // end method green. Radio. Button_Checked. Changed 2007 Dr. Natheer Khasawneh.

37 38 39 40 41 42 43 44 45 46 47 48 49 50

37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 // event handler for 12 point Radio. Button private void size 12 Radio. Button_Checked. Changed( object sender, Event. Args e ) { // change font size to 12 display. Label. Font = new Font( display. Label. Font. Name, 12 ); } // end method size 12 Radio. Button_Checked. Changed Outline // event handler for 16 point Radio. Button private void size 16 Radio. Button_Checked. Changed( object sender, Event. Args e ) { // change font size to 16 display. Label. Font = new Font( display. Label. Font. Name, 16 ); } // end method size 16 Radio. Button_Checked. Changed // event handler for 20 point Radio. Button private void size 20 Radio. Button_Checked. Changed( object sender, Event. Args e ) { // change font size to 20 display. Label. Font = new Font( display. Label. Font. Name, 20 ); } // end method size 20 Radio. Button_Checked. Changed Using. Tabs. cs // event handler for Hello! Radio. Button private void hello. Radio. Button_Checked. Changed ( object sender, Event. Args e ) { display. Label. Text = "Hello!"; // change text to Hello! } // end method hello. Radio. Button_Checked. Changed // event handler for Goodbye! Radio. Button private void goodbye. Radio. Button_Checked. Changed ( object sender, Event. Args e ) { display. Label. Text = "Goodbye!"; // change text to Goodbye! } // end method goodbye. Radio. Button_Checked. Changed 2007 Dr. Natheer Khasawneh. 25

Outline Using. Tabs. cs Program Output 2007 Dr. Natheer Khasawneh. 26

Outline Using. Tabs. cs Program Output 2007 Dr. Natheer Khasawneh. 26

14. 12 Multiple-Document Interface Windows • Users can edit multiple documents at once •

14. 12 Multiple-Document Interface Windows • Users can edit multiple documents at once • Usually more complex than single-documentinterface applications • Application window called parent, others child • Parent and child menus can be merged – Based on Merge. Order 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 – Arrange. Icons: arranges icons for minimized windows 2007 Dr. Natheer Khasawneh. All rights reserved. 27

14. 12 Multiple Document Interface (MDI) Windows MDI parent MDI child Fig. 14. 37

14. 12 Multiple Document Interface (MDI) Windows MDI parent MDI child Fig. 14. 37 MDI parent and MDI child. 2007 Dr. Natheer Khasawneh. All rights reserved. 28

14. 12 Multiple Document Interface (MDI) Windows Single Document Interface (SDI) Fig. 14. 38

14. 12 Multiple Document Interface (MDI) Windows Single Document Interface (SDI) Fig. 14. 38 SDI and MDI forms. 2007 Dr. Natheer Khasawneh. All rights reserved. Multiple Document Interface (MDI) 29

14. 12 Multiple Document Interface (MDI) Windows 2007 Dr. Natheer Khasawneh. All rights reserved.

14. 12 Multiple Document Interface (MDI) Windows 2007 Dr. Natheer Khasawneh. All rights reserved. 30

14. 12 Multiple Document Interface (MDI) Windows Parent’s icons: minimize, maximize and close Minimized

14. 12 Multiple Document Interface (MDI) Windows Parent’s icons: minimize, maximize and close Minimized child’s icons: restore, maximize and close Fig. 14. 40 Minimized and maximized child windows. 2007 Dr. Natheer Khasawneh. All rights reserved. Maximized child’s icons: minimize, restore and close Parent’s title bar displays maximized child 31

14. 12 Multiple Document Interface (MDI) Windows Separator bar and child windows Child windows

14. 12 Multiple Document Interface (MDI) Windows Separator bar and child windows Child windows list Fig. 14. 41 Using Menu. Item property Mdi. List. 2007 Dr. Natheer Khasawneh. All rights reserved. 9 or more child windows enables the More Windows. . . option 32

14. 12 Multiple Document Interface (MDI) Windows Arrange. Icons Fig. 14. 42 Layout. Mdi

14. 12 Multiple Document Interface (MDI) Windows Arrange. Icons Fig. 14. 42 Layout. Mdi enumeration values (Part 1). 2007 Dr. Natheer Khasawneh. All rights reserved. Cascade 33

14. 12 Multiple Document Interface (MDI) Windows Tile. Horizontal Fig. 14. 43 Layout. Mdi

14. 12 Multiple Document Interface (MDI) Windows Tile. Horizontal Fig. 14. 43 Layout. Mdi enumeration values (Part 2). 2007 Dr. Natheer Khasawneh. All rights reserved. Tile. Vertical 34

1 2 3 4 5 6 7 8 9 10 11 12 13 14

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 // Fig. 14. 43: Using. MDIForm. cs // Demonstrating use of MDI parent and child windows. using System; using System. Windows. Forms; Outline // Form demonstrates the use of MDI parent and child windows public partial class Using. MDIForm : Form { // default constructor public Using. MDIForm() { Initialize. Component(); } // end constructor // create Child 1 window when child 1 Tool. Strip Menu. Item is clicked private void child 1 Tool. Strip. Menu. Item_Click( object sender, Event. Args e ) { // create new child Child. Form form. Child = new Child. Form( "Child 1", @"imagescsharphtp 1. jpg" ); form. Child. Mdi. Parent = this; // set parent form. Child. Show(); // display child } // end method child 1 Tool. Strip. Menu. Item_Click // create Child 2 window when child 2 Tool. Strip. Menu. Item is clicked private void child 2 Tool. Strip. Menu. Item_Click( object sender, Event. Args e ) { // create new child Child. Form form. Child = new Child. Form( "Child 2", @"imagesvbnethtp 2. jpg" ); form. Child. Mdi. Parent = this; // set parent form. Child. Show(); // display child } // end method child 2 Tool. Strip. Menu. Item_Click Using. MDI. cs 2007 Dr. Natheer Khasawneh. 35

37 // create Child 3 window when child 3 Tool. Strip. Menu. Item is

37 // create Child 3 window when child 3 Tool. Strip. Menu. Item is clicked 38 private void child 3 Tool. Strip. Menu. Item_Click( 39 object sender, Event. Args e ) 40 { 41 // create new child 42 Child form. Child = 43 new Child( "Child 3", @"imagespythonhtp 1. jpg" ); 44 form. Child. Mdi. Parent = this; // set parent 45 form. Child. Show(); // display child 46 } // end method child 3 Tool. Strip. Menu. Item_Click 47 48 // exit application 49 private void exit. Tool. Strip. Menu. Item_Click ( object sender, Event. Args e ) 50 { 51 Application. Exit(); 52 } // end method exit. Tool. Strip. Menu. Item_Click 53 54 // set Cascade layout 55 private void cascade. Tool. Strip. Menu. Item_Click ( 56 object sender, Event. Args e ) 57 { 58 this. Layout. Mdi( Mdi. Layout. Cascade ); 59 } // end method cascade. Tool. Strip. Menu. Item_Click 60 61 // set Tile. Horizontal layout 62 private void tile. Horizontal. Tool. Strip. Menu. Item_Click ( 63 object sender, Event. Args e ) 64 { 65 this. Layout. Mdi( Mdi. Layout. Tile. Horizontal ); 66 } // end method tile. Horizontal. Tool. Strip. Menu. Item 67 68 // set Tile. Vertical layout 69 private void tile. Vertical. Tool. Strip. Menu. Item_Click ( 70 object sender, Event. Args e ) 71 { 72 this. Layout. Mdi( Mdi. Layout. Tile. Vertical ); 73 } // end method tile. Vertical. Tool. Strip. Menu. Item_Click 74 } // end class Using. MDIForm Outline Using. MDI. cs 2007 Dr. Natheer Khasawneh. 36

Outline Using. MDI. cs Program Output 2007 Dr. Natheer Khasawneh. 37

Outline Using. MDI. cs Program Output 2007 Dr. Natheer Khasawneh. 37

1 // Fig. 14. 44: Child. Form. cs 2 // Child window of MDI

1 // Fig. 14. 44: Child. Form. cs 2 // Child window of MDI parent. 3 using System; 4 using System. Drawing; 5 using System. Windows. Forms; 6 using System. IO; 7 8 public partial class Child. Form : Form 9 { 10 public Child. Form( string title, string file. Name ) 11 { 12 // Required for Windows Form Designer support 13 Initialize. Component(); 14 15 Text = title; // set title text 16 17 // set image to display in picture. Box 18 pic. Display. Image = Image. From. File( 19 Directory. Get. Current. Directory () + file. Name ); 20 } // end constructor 21 } // end class Child. Form Outline Child. cs 2007 Dr. Natheer Khasawneh. 38

Timer Class • Implements a timer that raises an event at userdefined intervals. •

Timer Class • Implements a timer that raises an event at userdefined intervals. • This timer is optimized for use in Windows Forms applications and must be used in a window. • This Windows timer is designed for a singlethreaded environment where UI threads are used to perform processing 2007 Dr. Natheer Khasawneh. All rights reserved. 39

Timer Class 2007 Dr. Natheer Khasawneh. All rights reserved. 40

Timer Class 2007 Dr. Natheer Khasawneh. All rights reserved. 40