1 / 53

Chapter 14 – Graphical User Interfaces Part 2

Chapter 14 – Graphical User Interfaces Part 2. Outline 14.1 Introduction 14.2 Menus 14.3 Menus 14.4 Menus 14.5 LinkLabels 14.6 ListBoxes 14.7 CheckedListBoxes 14.8 ComboBoxes. 14.1 Introduction. Continues study of Graphical User Interface Explores: Menus LinkLabels

tatianaj
Download Presentation

Chapter 14 – Graphical User Interfaces Part 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 14 – Graphical User Interfaces Part 2 Outline14.1 Introduction14.2 Menus 14.3 Menus 14.4 Menus14.5 LinkLabels14.6 ListBoxes14.7 CheckedListBoxes14.8 ComboBoxes

  2. 14.1 Introduction • Continues study of Graphical User Interface • Explores: • Menus • LinkLabels • ListBox • CheckedListBox • ComboBoxes • TreeView control • Tab controls • Multiple-document interface windows

  3. 14.2 Menus • Group related commands together • Contain: • Commands • Submenus • Exit uses Application class to quit • Color options mutually exclusive • Every option has its own event handler • Font style options use Xor operator

  4. 14.2 Menus

  5. 14.2 Menus

  6. 14.2 Menus

  7. 1 // Fig. 14.7: MenuTestForm.cs 2 // Using Menus to change font colors and styles. 3 using System; 4 using System.Drawing; 5 using System.Windows.Forms; 6 7 // our Form contains a Menu that changes the font color 8 // and style of the text displayed in Label 9 public partial class MenuTestForm : Form 10 { 11 // default constructor 12 public MenuTestForm() 13 { 14 InitializeComponent(); 15 } // end constructor 16 17 // display MessageBox when About ToolStripMenuItem is selected 18 private void aboutToolStripMenuItem_Click( object sender, EventArgs e ) 19 { 20 MessageBox.Show( 21 "This is an example\nof using menus.", 22 "About", MessageBoxButtons.OK, MessageBoxIcon.Information ); 23 } // end method aboutToolStripMenuItem_Click 24 25 // exit program when Exit ToolStripMenuItem is selected 26 private void exitToolStripMenuItem_Click( object sender, EventArgs e ) 27 { 28 Application.Exit(); 29 } // end method exitToolStripMenuItem MenuTest.cs

  8. 31 // reset checkmarks for Color ToolStripMenuItems 32 private void ClearColor() 33 { 34 // clear all checkmarks 35 blackToolStripMenuItem.Checked = false; 36 blueToolStripMenuItem.Checked = false; 37 redToolStripMenuItem.Checked = false; 38 greenToolStripMenuItem.Checked = false; 39 } // end method ClearColor 40 41 // update Menu state and color display black 42 private void blackToolStripMenuItem_Click( object sender, EventArgs e ) 43 { 44 // reset checkmarks for Color ToolStripMenuItems 45 ClearColor(); 46 47 // set Color to Black 48 displayLabel.ForeColor = Color.Black; 49 blackToolStripMenuItem.Checked = true; 50 } // end method blackToolStripMenuItem_Click 51 52 // update Menu state and color display blue 53 private void blueToolStripMenuItem_Click( object sender, EventArgs e ) 54 { 55 // reset checkmarks for Color ToolStripMenuItems 56 ClearColor(); 57 58 // set Color to Blue 59 displayLabel.ForeColor = Color.Blue; 60 blueToolStripMenuItem.Checked = true; 61 } // end method blueToolStripMenuItem_Click MenuTest.cs

  9. 63 // update Menu state and color display red 64 private void redToolStripMenuItem_Click( object sender, EventArgs e ) 65 { 66 // reset checkmarks for Color ToolStripMenuItems 67 ClearColor(); 68 69 // set Color to Red 70 displayLabel.ForeColor = Color.Red; 71 redToolStripMenuItem.Checked = true; 72 } // end method redToolStripMenuItem_Click 73 74 // update Menu state and color display green 75 private void greenToolStripMenuItem_Click( object sender, EventArgs e ) 76 { 77 // reset checkmarks for Color ToolStripMenuItems 78 ClearColor(); 79 80 // set Color to Green 81 displayLabel.ForeColor = Color.Green; 82 greenToolStripMenuItem.Checked = true; 83 } // end method greenToolStripMenuItem_Click 84 85 // reset checkmarks for Font ToolStripMenuItems 86 private void ClearFont() 87 { 88 // clear all checkmarks 89 timesToolStripMenuItem.Checked = false; 90 courierToolStripMenuItem.Checked = false; 91 comicToolStripMenuItem.Checked = false; 92 } // end method ClearFont MenuTest.cs

  10. 94 // update Menu state and set Font to Times New Roman 95 private void timesToolStripMenuItem_Click( object sender, EventArgs e ) 96 { 97 // reset checkmarks for Font ToolStripMenuItems 98 ClearFont(); 99 100 // set Times New Roman font 101 timesToolStripMenuItem.Checked = true; 102 displayLabel.Font = new Font( 103 "Times New Roman", 14, displayLabel.Font.Style ); 104 } // end method timesToolStripMenuItem_Click 105 106 // update Menu state and set Font to Courier 107 private void courierToolStripMenuItem_Click( 108 object sender, EventArgs e ) 109 { 110 // reset checkmarks for Font ToolStripMenuItems 111 ClearFont(); 112 113 // set Courier font 114 courierToolStripMenuItem.Checked = true; 115 displayLabel.Font = new Font( 116 "Courier", 14, displayLabel.Font.Style ); 117 } // end method courierToolStripMenuItem_Click MenuTest.cs

  11. 119 // update Menu state and set Font to Comic Sans MS 120 private void comicToolStripMenuItem_Click( object sender, EventArgs e ) 121 { 122 // reset checkmarks for Font ToolStripMenuItems 123 ClearFont(); 124 125 // set Comic Sans font 126 comicToolStripMenuItem.Checked = true; 127 displayLabel.Font = new Font( 128 "Comic Sans MS", 14, displayLabel.Font.Style ); 129 } // end method comicToolStripMenuItem_Click 130 131 // toggle checkmark and toggle bold style 132 private void boldToolStripMenuItem_Click( object sender, EventArgs e ) 133 { 134 // toggle checkmark 135 boldToolStripMenuItem.Checked = !boldToolStripMenuItem.Checked; 136 137 // use logical exlusive OR to toggle bold, keep all other styles 138 displayLabel.Font = new Font( 139 displayLabel.Font.FontFamily, 14, 140 displayLabel.Font.Style ^ FontStyle.Bold ); 141 } // end method boldToolStripMenuItem_Click 142 143 // toggle checkmark and toggle italic style 144 private void italicToolStripMenuItem_Click( 145 object sender, EventArgs e ) 146 { 147 // toggle checkmark 148 italicToolStripMenuItem.Checked = !italicToolStripMenuItem.Checked; 149 150 // use logical exclusive OR to toggle italic, keep all other styles 151 displayLabel.Font = new Font( 152 displayLabel.Font.FontFamily, 14, 153 displayLabel.Font.Style ^ FontStyle.Italic ); 154 } // end method italicToolStripMenuItem_Click 155 } // end class MenuTestForm MenuTest.cs

  12. MenuTest.csProgram Output

  13. 14.3 MonthCalendar Control • The MonthCalendar control displays a monthly calendar on the Form. • The user can select a date from the currently displayed month. • When a date is selected, it is highlighted. • Multiple dates can be selected. • The default event for this control is DateChanged.

  14. 14.3 MonthCalendar Control

  15. 14.4 DateTimePicker Control • The DateTimePicker control is similar to the MonthCalendar control • But displays the calendar when a down arrow is selected

  16. 1 // Fig. 14.11: DateTimePickerForm.cs 2 // Using a DateTimePicker to select a drop off time. 3 using System; 4 using System.Windows.Forms; 5 6 public partial class DateTimePickerForm : Form 7 { 8 // default constructor 9 public DateTimePickerForm() 10 { 11 InitializeComponent(); 12 } // end constructor 13 14 private void dateTimePickerDropOff_ValueChanged( 15 object sender, EventArgs e ) 16 { 17 DateTime dropOffDate = dateTimePickerDropOff.Value; 18 19 // add extra time when items are dropped off around Sunday 20 if ( dropOffDate.DayOfWeek == DayOfWeek.Friday || 21 dropOffDate.DayOfWeek == DayOfWeek.Saturday || 22 dropOffDate.DayOfWeek == DayOfWeek.Sunday ) 23 24 //estimate three days for delivery 25 outputLabel.Text = dropOffDate.AddDays( 3 ).ToLongDateString(); 26 else 27 // otherwise estimate only two days for delivery 28 outputLabel.Text = dropOffDate.AddDays( 2 ).ToLongDateString(); 29 } // end method dateTimePickerDropOff_ValueChanged DateTimePickerForm.cs

  17. 30 31 private void DateTimePickerForm_Load( object sender, EventArgs e ) 32 { 33 // user cannot select days before today 34 dateTimePickerDropOff.MinDate = DateTime.Today; 35 36 // user can only select days of this year 37 dateTimePickerDropOff.MaxDate = DateTime.Today.AddYears( 1 ); 38 } // end method DateTimePickerForm_Load 39 } // end class DateTimePickerForm DateTimePickerForm.cs

  18. 14.4 DateTimePicker Control

  19. 14.5 LinkLabels • 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

  20. LinkLabel on a form Hand image displayed when mouse cursor over a LinkLabel 14.5 LinkLabels Fig. 14.5 LinkLabel control in the design phase and in running program.

  21. 14.5 LinkLabels

  22. C drive link Deitel websitelink Notepad link C driveevent handler Start method to open other programs 1 // Fig. 13.7: 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 publicclass LinkLabelTest : System.Windows.Forms.Form 12 { 13 // linklabels to C: drive, www.deitel.com and Notepad 14private System.Windows.Forms.LinkLabel driveLinkLabel; 15private System.Windows.Forms.LinkLabel deitelLinkLabel; 16private System.Windows.Forms.LinkLabel notepadLinkLabel; 17 18 [STAThread] 19 staticvoid Main() 20 { 21 Application.Run( new LinkLabelTest() ); 22 } 23 24 // browse C:\ drive 25privatevoid driveLinkLabel_LinkClicked( object sender, 26 System.Windows.Forms.LinkLabelLinkClickedEventArgs e ) 27 { 28 driveLinkLabel.LinkVisited = true; 29 System.Diagnostics.Process.Start( "C:\\" ); 30 } 31 LinkLabelTest.cs

  23. Deitel websiteevent handler Notepad event handler 32 // load www.deitel.com in Web broswer 33privatevoid 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" ); 39 } 40 41 // run application Notepad 42privatevoid 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 LinkLabelTest.cs

  24. Click on first LinkLabel to look at contents of C drive LinkLabelTest.csProgram Output

  25. Click on second LinkLabel to go to the Web Site LinkLabelTest.csProgram Output

  26. Click the third LinkLabel to open notepad LinkLabelTest.csProgram Output

  27. 14.6,7 ListBoxes and CheckedListBoxes • ListBoxes • Allow users to view and select from items on a list • Static objects • 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 returnsindex of selected item • Property GetSelected returns true if property at given index is selected • Use Add method to add to Items collection • myListBox.Items.Add(“myListItem”)

  28. 14.6,7 ListBoxes and CheckedListBoxes • CheckedListBoxes • Extends ListBox by placing check boxes next to items • Can select more than one object at one time

  29. 14.6 ListBoxes • Class ListBoxTest • Allows users to add and remove items from ListBox • Uses event handlers to add to, remove from and clear list

  30. 14.7 CheckedListBoxes • 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

  31. ListBox Selected Items Scroll bars appear if necessary Checked item CheckedListBox 14.6,7 ListBoxes and CheckListBoxes Fig. 13.8 ListBox and CheckedListBox on a form.

  32. 14.6,7 ListBoxes and CheckListBoxes

  33. 14.6,7 ListBoxes and CheckListBoxes Fig. 14.10 String Collection Editor.

  34. 1 // Fig. 14.18: ListBoxTestForm.cs 2 // Program to add, remove and clear ListBox items 3 using System; 4 using System.Windows.Forms; 5 6 // Form uses a TextBox and Buttons to add, 7 // remove, and clear ListBox items 8 public partial class ListBoxTestForm : Form 9 { 10 // default constructor 11 public ListBoxTestForm() 12 { 13 InitializeComponent(); 14 } // end constructor 15 16 // add new item to ListBox (text from input TextBox) 17 // and clear input TextBox 18 private void addButton_Click( object sender, EventArgs e ) 19 { 20 displayListBox.Items.Add( inputTextBox.Text ); 21 inputTextBox.Clear(); 22 } // end method addButton_Click 23 ListBoxTest.cs

  35. 24 // remove item if one is selected 25 private void removeButton_Click( object sender, EventArgs e ) 26 { 27 // check if item is selected, remove if selected 28 if ( displayListBox.SelectedIndex != -1 ) 29 displayListBox.Items.RemoveAt( displayListBox.SelectedIndex ); 30 } // end method removeButton_Click 31 32 // clear all items in ListBox 33 private void clearButton_Click( object sender, EventArgs e ) 34 { 35 displayListBox.Items.Clear(); 36 } // end method clearButton_Click 37 38 // exit application 39 private void exitButton_Click( object sender, EventArgs e ) 40 { 41 Application.Exit(); 42 } // end method exitButton_Click 43 } // end class ListBoxTestForm ListBoxTest.cs

  36. ListBoxTest.csProgram Output

  37. 14.6,7 ListBoxes and CheckListBoxes

  38. 1 // Fig. 14.20: CheckedListBoxTestForm.cs 2 // Using the checked ListBox to add items to a display ListBox 3 using System; 4 using System.Windows.Forms; 5 6 // Form uses a checked ListBox to add items to a display ListBox 7 public partial class CheckedListBoxTestForm : Form 8 { 9 // default constructor 10 public CheckedListBoxTestForm() 11 { 12 InitializeComponent(); 13 } // end constructor 14 15 // item about to change 16 // add or remove from display ListBox 17 private void inputCheckedListBox_ItemCheck( 18 object sender, ItemCheckEventArgs e ) 19 { 20 // obtain reference of selected item 21 string item = inputCheckedListBox.SelectedItem.ToString(); 22 23 // if item checked add to ListBox 24 // otherwise remove from ListBox 25 if ( e.NewValue == CheckState.Checked ) 26 displayListBox.Items.Add( item ); 27 else 28 displayListBox.Items.Remove( item ); 29 } // end method inputCheckedListBox_ItemCheck 30 } // end class CheckedListBoxTestForm CheckedListBoxTest.cs

  39. CheckedListBoxTest.csProgram Output

  40. 14.8 ComboBoxes • Combine TextBox and drop-down list • Add method adds object to collection • Properties: • DropDownStyle: determines type of ComboBox • Items: returns objects in the list • SelectedItem: returns object selected • SelectedIndex: returns index of selected item

  41. 14.8 ComboBoxes Fig. 14.14 Demonstrating a ComboBox.

  42. 14.8 ComboBoxes

  43. 1 // Fig. 14.23: ComboBoxTestForm.cs 2 // Using ComboBox to select a shape to draw. 3 using System; 4 using System.Drawing; 5 using System.Windows.Forms; 6 7 // Form uses a ComboBox to select different shapes to draw 8 public partial class ComboBoxTestForm : Form 9 { 10 // default constructor 11 public ComboBoxTestForm() 12 { 13 InitializeComponent(); 14 } // end constructor 15 16 // get index of selected shape, draw shape 17 private void imageComboBox_SelectedIndexChanged( 18 object sender, EventArgs e ) 19 { 20 // create graphics object, Pen and SolidBrush 21 Graphics myGraphics = base.CreateGraphics(); 22 23 // create Pen using color DarkRed 24 Pen myPen = new Pen( Color.DarkRed ); 25 26 // create SolidBrush using color DarkRed 27 SolidBrush mySolidBrush = new SolidBrush( Color.DarkRed ); 28 29 // clear drawing area setting it to color white 30 myGraphics.Clear( Color.White ); 31 ComboBoxTest.cs

  44. 32 // find index, draw proper shape 33 switch ( imageComboBox.SelectedIndex ) 34 { 35 case 0: // case Circle is selected 36 myGraphics.DrawEllipse( myPen, 50, 50, 150, 150 ); 37 break; 38 case 1: // case Rectangle is selected 39 myGraphics.DrawRectangle( myPen, 50, 50, 150, 150 ); 40 break; 41 case 2: // case Ellipse is selected 42 myGraphics.DrawEllipse( myPen, 50, 85, 150, 115 ); 43 break; 44 case 3: // case Pie is selected 45 myGraphics.DrawPie( myPen, 50, 50, 150, 150, 0, 45 ); 46 break; 47 case 4: // case Filled Circle is selected 48 myGraphics.FillEllipse( mySolidBrush, 50, 50, 150, 150 ); 49 break; 50 case 5: // case Filled Rectangle is selected 51 myGraphics.FillRectangle( mySolidBrush, 50, 50, 150, 150 ); 52 break; 53 case 6: // case Filled Ellipse is selected 54 myGraphics.FillEllipse( mySolidBrush, 50, 85, 150, 115 ); 55 break; 56 case 7: // case Filled Pie is selected 57 myGraphics.FillPie( mySolidBrush, 50, 50, 150, 150, 0, 45 ); 58 break; 59 } // end switch 60 61 myGraphics.Dispose(); // release the Graphics object 62 } // end method imageComboBox_SelectedIndexChanged 63 } // end class ComboBoxTestForm ComboBoxTest.cs

  45. ComboBoxTest.csProgram Output

  46. ComboBoxTest.csProgram Output

  47. DataSource Property • Gets or sets the data source for this ListControl • An object that implements the IList or IListSource interfaces, such as a DataSet or an Array.

More Related