Download presentation
Presentation is loading. Please wait.
1
2007 Dr. Natheer Khasaneh 1 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
2
2007 Dr. Natheer Khasaneh 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
2007 Dr. Natheer Khasaneh 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
2007 Dr. Natheer Khasaneh 4 14.2 Menus
5
2007 Dr. Natheer Khasaneh 5 14.2 Menus
6
2007 Dr. Natheer Khasaneh 6 14.2 Menus
7
2007 Dr. Natheer Khasaneh 7
8
2007 Dr. Natheer Khasawneh Outline 8 MenuTest.cs 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
9
2007 Dr. Natheer Khasawneh Outline 9 MenuTest.cs 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
10
2007 Dr. Natheer Khasawneh Outline 10 MenuTest.cs 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
11
2007 Dr. Natheer Khasawneh Outline 11 MenuTest.cs 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
12
2007 Dr. Natheer Khasawneh Outline 12 MenuTest.cs 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
13
2007 Dr. Natheer Khasawneh Outline 13 MenuTest.cs Program Output
14
2007 Dr. Natheer Khasaneh 14 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.
15
2007 Dr. Natheer Khasaneh 15 14.3 MonthCalendar Control
16
2007 Dr. Natheer Khasaneh 16
17
2007 Dr. Natheer Khasaneh 17 14.4 DateTimePicker Control The DateTimePicker control is similar to the MonthCalendar control But displays the calendar when a down arrow is selected
18
2007 Dr. Natheer Khasaneh 18
19
2007 Dr. Natheer Khasawneh Outline 19 DateTimePickerForm.cs 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
20
2007 Dr. Natheer Khasawneh Outline 20 DateTimePickerForm.cs 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
21
2007 Dr. Natheer Khasaneh 21 14.4 DateTimePicker Control
22
2007 Dr. Natheer Khasaneh 22 14.5 LinkLabel s 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
23
2007 Dr. Natheer Khasaneh 23 14.5 LinkLabel s Fig. 14.5 LinkLabel control in the design phase and in running program. LinkLabel on a form Hand image displayed when mouse cursor over a LinkLabel
24
2007 Dr. Natheer Khasaneh 24 14.5 LinkLabel s
25
2007 Dr. Natheer Khasawneh Outline 25 LinkLabelTest.cs 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 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:\\" ); 30 } 31 C drive linkNotepad linkDeitel website link C drive event handler Start method to open other programs
26
2007 Dr. Natheer Khasawneh Outline 26 LinkLabelTest.cs 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" ); 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
27
2007 Dr. Natheer Khasawneh Outline 27 LinkLabelTest.cs Program Output Click on first LinkLabel to look at contents of C drive
28
2007 Dr. Natheer Khasawneh Outline 28 LinkLabelTest.cs Program Output Click on second LinkLabel to go to the Web Site
29
2007 Dr. Natheer Khasawneh Outline 29 LinkLabelTest.cs Program Output Click the third LinkLabel to open notepad
30
2007 Dr. Natheer Khasaneh 30 14.6,7 ListBox es and CheckedListBox es ListBoxe s –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 returns index 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”)
31
2007 Dr. Natheer Khasaneh 31 14.6,7 ListBox es and CheckedListBox es CheckedListBoxe s –Extends ListBox by placing check boxes next to items –Can select more than one object at one time
32
2007 Dr. Natheer Khasaneh 32 14.6 ListBox es Class ListBoxTest –Allows users to add and remove items from ListBox –Uses event handlers to add to, remove from and clear list
33
2007 Dr. Natheer Khasaneh 33 14.7 CheckedListBox es 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
34
2007 Dr. Natheer Khasaneh 34 14.6,7 ListBox es and CheckListBox es Fig. 13.8 ListBox and CheckedListBox on a form. ListBox Selected Items Checked item CheckedListBox Scroll bars appear if necessary
35
2007 Dr. Natheer Khasaneh 35 14.6,7 ListBox es and CheckListBox es
36
2007 Dr. Natheer Khasaneh 36 14.6,7 ListBox es and CheckListBox es Fig. 14.10 String Collection Editor.
37
2007 Dr. Natheer Khasawneh Outline 37 ListBoxTest.cs 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
38
2007 Dr. Natheer Khasawneh Outline 38 ListBoxTest.cs 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
39
2007 Dr. Natheer Khasawneh Outline 39 ListBoxTest.cs Program Output
40
2007 Dr. Natheer Khasaneh 40 14.6,7 ListBox es and CheckListBox es
41
2007 Dr. Natheer Khasawneh Outline 41 CheckedListBoxTe st.cs 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
42
2007 Dr. Natheer Khasawneh Outline 42 CheckedListBoxTe st.cs Program Output
43
2007 Dr. Natheer Khasaneh 43 14.8 ComboBox es 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
44
2007 Dr. Natheer Khasaneh 44 14.8 ComboBox es Fig. 14.14Demonstrating a ComboBox.
45
2007 Dr. Natheer Khasaneh 45 14.8 ComboBox es
46
2007 Dr. Natheer Khasawneh Outline 46 ComboBoxTest.cs 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
47
2007 Dr. Natheer Khasawneh Outline 47 ComboBoxTest.cs 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
48
2007 Dr. Natheer Khasawneh Outline 48 ComboBoxTest.cs Program Output
49
2007 Dr. Natheer Khasawneh Outline 49 ComboBoxTest.cs Program Output
50
2007 Dr. Natheer Khasaneh 50 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.
51
2007 Dr. Natheer Khasaneh 51 There are two ways to fill the ComboBox and ListBox controls. You can add objects to the ComboBox by using the Add method. You can also add objects to a ComboBox by using the DataSource, DisplayMember, and ValueMember properties to fill the ComboBox.
52
2007 Dr. Natheer Khasawneh Outline 52 ComboBoxTest.cs using System; using System.Windows.Forms ; using System.Drawing ; using System.Collections ; namespace MyListControlSample { public class USState { private string myShortName ; private string myLongName ; public USState(string strLongName, string strShortName) { this.myShortName = strShortName; this.myLongName = strLongName; } public string ShortName { get { return myShortName; } public string LongName { get { return myLongName ; } public override string ToString() { return this.ShortName + " - " + this.LongName; }
53
2007 Dr. Natheer Khasawneh Outline 53 ComboBoxTest.cs public class ListBoxSample3:Form { private ListBox ListBox1 = new ListBox(); private TextBox textBox1 = new TextBox() ; [STAThread] static void Main() { Application.Run(new ListBoxSample3()) ; } public ListBoxSample3() { this.ClientSize = new Size(292, 181) ; this.Text = "ListBox Sample3" ; ListBox1.Location = new Point(24, 16) ; ListBox1.Name = "ListBox1" ; ListBox1.Size = new Size(232, 130) ; textBox1.Location = new Point(24, 160) ; textBox1.Name = "textBox1" ; textBox1.Size = new Size(240, 24) ; this.Controls.AddRange(new Control[] {ListBox1, textBox1}) ; // Populates the list box using DataSource. // DisplayMember is used to display just the long name of each state. ArrayList USStates = new ArrayList() ; USStates.Add(new USState("Alabama", "AL")); USStates.Add(new USState("Washington", "WA")) ; USStates.Add(new USState("West Virginia", "WV")); USStates.Add(new USState("Wisconsin", "WI")) ; USStates.Add(new USState("Wyoming", "WY")); ListBox1.SelectedValueChanged += new EventHandler(ListBox1_SelectedValueChanged); ListBox1.DataSource = USStates ; ListBox1.DisplayMember = "LongName" ; ListBox1.ValueMember = "ShortName" ; } private void InitializeComponent() { } private void ListBox1_SelectedValueChanged(object sender, EventArgs e) { if (ListBox1.SelectedIndex != -1) textBox1.Text = ListBox1.SelectedValue.ToString(); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.