Chapter 14 – Graphical User Interfaces Part 2 Outline 14.1 Introduction 14.2 Menus 14.3 Menus 14.4 14.5 14.6 14.7 14.8 Menus LinkLabels ListBoxes CheckedListBoxes ComboBoxes 2007 Dr. Natheer Khasaneh 1 2 14.1 Introduction • Continues study of Graphical User Interface • Explores: – – – – – – Menus LinkLabels ListBox CheckedListBox ComboBoxes TreeView control – Tab controls – Multiple-document interface windows 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 2007 Dr. Natheer Khasaneh 4 14.2 Menus 2007 Dr. Natheer Khasaneh 5 14.2 Menus 2007 Dr. Natheer Khasaneh 6 14.2 Menus 2007 Dr. Natheer Khasaneh 7 2007 Dr. Natheer Khasaneh 8 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 // Fig. 14.7: MenuTestForm.cs // Using Menus to change font colors and styles. using System; using System.Drawing; using System.Windows.Forms; Outline MenuTest.cs // our Form contains a Menu that changes the font color // and style of the text displayed in Label public partial class MenuTestForm : Form { // default constructor public MenuTestForm() { InitializeComponent(); } // end constructor // display MessageBox when About ToolStripMenuItem is selected private void aboutToolStripMenuItem_Click( object sender, EventArgs e ) { MessageBox.Show( "This is an example\nof using menus.", "About", MessageBoxButtons.OK, MessageBoxIcon.Information ); } // end method aboutToolStripMenuItem_Click // exit program when Exit ToolStripMenuItem is selected private void exitToolStripMenuItem_Click( object sender, EventArgs e ) { Application.Exit(); } // end method exitToolStripMenuItem 2007 Dr. Natheer Khasawneh 9 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 // reset checkmarks for Color ToolStripMenuItems private void ClearColor() { // clear all checkmarks blackToolStripMenuItem.Checked = false; blueToolStripMenuItem.Checked = false; redToolStripMenuItem.Checked = false; greenToolStripMenuItem.Checked = false; } // end method ClearColor Outline MenuTest.cs // update Menu state and color display black private void blackToolStripMenuItem_Click( object sender, EventArgs e ) { // reset checkmarks for Color ToolStripMenuItems ClearColor(); // set Color to Black displayLabel.ForeColor = Color.Black; blackToolStripMenuItem.Checked = true; } // end method blackToolStripMenuItem_Click // update Menu state and color display blue private void blueToolStripMenuItem_Click( object sender, EventArgs e ) { // reset checkmarks for Color ToolStripMenuItems ClearColor(); // set Color to Blue displayLabel.ForeColor = Color.Blue; blueToolStripMenuItem.Checked = true; } // end method blueToolStripMenuItem_Click 2007 Dr. Natheer Khasawneh 10 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 92 Outline // update Menu state and color display red private void redToolStripMenuItem_Click( object sender, EventArgs e ) { // reset checkmarks for Color ToolStripMenuItems MenuTest.cs ClearColor(); // set Color to Red displayLabel.ForeColor = Color.Red; redToolStripMenuItem.Checked = true; } // end method redToolStripMenuItem_Click // update Menu state and color display green private void greenToolStripMenuItem_Click( object sender, EventArgs e ) { // reset checkmarks for Color ToolStripMenuItems ClearColor(); // set Color to Green displayLabel.ForeColor = Color.Green; greenToolStripMenuItem.Checked = true; } // end method greenToolStripMenuItem_Click // reset checkmarks for Font ToolStripMenuItems private void ClearFont() { // clear all checkmarks timesToolStripMenuItem.Checked = false; courierToolStripMenuItem.Checked = false; comicToolStripMenuItem.Checked = false; } // end method ClearFont 2007 Dr. Natheer Khasawneh 11 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 Outline MenuTest.cs 2007 Dr. Natheer Khasawneh 12 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 Outline MenuTest.cs 2007 Dr. Natheer Khasawneh 13 Outline MenuTest.cs Program Output 2007 Dr. Natheer Khasawneh 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. 2007 Dr. Natheer Khasaneh 15 14.3 MonthCalendar Control 2007 Dr. Natheer Khasaneh 16 MonthCalendar properties and an event Description MonthCalendar Properties FirstDayOfWeek Sets which day of the week is the first displayed for each week in the calendar. MaxDate The last date that can be selected. MaxSelectionCount The maximum number of dates that can be selected at once. MinDate The first date that can be selected. MonthlyBoldedDates An array of dates that will displayed in bold in the calendar. SelectionEnd The last of the dates selected by the user. SelectionRange The dates selected by the user. SelectionStart The first of the dates selected by the user. Common MonthCalendar Event DateChanged 2007 Dr. Natheer Khasaneh Generated when a date is selected in the calendar. 17 14.4 DateTimePicker Control • The DateTimePicker control is similar to the MonthCalendar control • But displays the calendar when a down arrow is selected 2007 Dr. Natheer Khasaneh Figure 14.10. DateTimePicker properties and an event. DateTimePicker properties and an event Description DateTimePicker Properties CalendarForeColor Sets the text color for the calendar. CalendarMonthBackground Sets the calendar's background color. CustomFormat Sets the custom format string for the user's options. Format Sets the format of the date and/or time used for the user's options. MaxDate The maximum date and time that can be selected. MinDate The minimum date and time that can be selected. ShowCheckBox Indicates if a CheckBox should be displayed to the left of the selected date and time. ShowUpDown Used to indicate that the control should have up and down Buttons. This is helpful for instances when the DateTimePicker is used to select a timethe Buttons can be used to increase or decrease hour, minute and second values. Value The data selected by the user. Common DateTimePicker Event ValueChanged 2007 Dr. Natheer Khasaneh Generated when the Value property changes, including when the user selects a new date or time. 18 19 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 Outline DateTimePickerForm.cs 2007 Dr. Natheer Khasawneh 20 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 Outline DateTimePickerForm.cs 2007 Dr. Natheer Khasawneh 21 14.4 DateTimePicker Control 2007 Dr. Natheer Khasaneh 22 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 2007 Dr. Natheer Khasaneh 23 14.5 LinkLabels LinkLabel on a form Hand image displayed when mouse cursor over a LinkLabel Fig. 14.5 LinkLabel control in the design phase and in running program. 2007 Dr. Natheer Khasaneh 24 14.5 LinkLabels LinkLabel p ro p e rtie s De sc rip tion / De le g a te a nd Eve nt Arg um e nts a nd events Common Properties ActiveLinkColor Specifies the color of the active link when clicked. Default is red. LinkArea Specifies which portion of text in the LinkLabel is treated as part of the link. LinkBehavior Specifies the link’s behavior, such as how the link appears when the mouse is placed over it. LinkColor Specifies the original color of all links before they have been visited. Default is blue. Links Lists the LinkLabel.Link objects, which are the links contained in the LinkLabel. LinkVisited If True, link appears as if it were visited (its color is changed to that specified by property VisitedLinkColor). Default False. Text Specifies the text to appear on the control. UseMnemonic If True, & character in Text property acts as a shortcut (similar to the Alt shortcut in menus). VisitedLinkColor Specifies the color of visited links. Default is Color.Purple. Common Event (Delegate LinkLabelLinkClickedEventHandler, event arguments LinkLabelLinkClickedEventArgs) LinkClicked Generated when link is clicked. Default when control is doubleclicked in designer. 2007 Dr. Natheer Khasaneh 25 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 Outline // Fig. 13.7: LinkLabelTest.cs // Using LinkLabels to create hyperlinks. using using using using using using System; System.Drawing; System.Collections; System.ComponentModel; System.Windows.Forms; System.Data; LinkLabelTest.cs public class LinkLabelTest : System.Windows.Forms.Form { // linklabels to C: drive, www.deitel.com and Notepad private System.Windows.Forms.LinkLabel driveLinkLabel; private System.Windows.Forms.LinkLabel deitelLinkLabel; private System.Windows.Forms.LinkLabel notepadLinkLabel; [STAThread] static void Main() { Application.Run( new LinkLabelTest() ); } // browse C:\ drive private void driveLinkLabel_LinkClicked( object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e ) { driveLinkLabel.LinkVisited = true; System.Diagnostics.Process.Start( "C:\\" ); } C drive link Deitel website link Notepad link C drive event handler Start method to open other programs 2007 Dr. Natheer Khasawneh 26 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 // load www.deitel.com in Web broswer private void deitelLinkLabel_LinkClicked( object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e ) { deitelLinkLabel.LinkVisited = true; System.Diagnostics.Process.Start( "IExplore", "http://www.deitel.com" ); } // run application Notepad private void notepadLinkLabel_LinkClicked( object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e ) { notepadLinkLabel.LinkVisited = true; Outline LinkLabelTest.cs Deitel website event handler Notepad event handler // program called as if in run // menu and full path not needed System.Diagnostics.Process.Start( "notepad" ); } } // end class LinkLabelTest 2007 Dr. Natheer Khasawneh 27 Outline LinkLabelTest.cs Program Output Click on first LinkLabel to look at contents of C drive 2007 Dr. Natheer Khasawneh 28 Outline LinkLabelTest.cs Program Output Click on second LinkLabel to go to the Web Site 2007 Dr. Natheer Khasawneh 29 Outline LinkLabelTest.cs Program Output Click the third LinkLabel to open notepad 2007 Dr. Natheer Khasawneh 30 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 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”) 2007 Dr. Natheer Khasaneh 31 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 2007 Dr. Natheer Khasaneh 32 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 2007 Dr. Natheer Khasaneh 33 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 2007 Dr. Natheer Khasaneh 34 14.6,7 ListBoxes and CheckListBoxes ListBox Selected Items Scroll bars appear if necessary Checked item CheckedListBox Fig. 13.8 ListBox and CheckedListBox on a form. 2007 Dr. Natheer Khasaneh 35 14.6,7 ListBoxes and CheckListBoxes ListBox p ro p e rtie s, De sc rip tion / De le g a te a nd Eve nt Arg um e nts m ethod s a nd e ve nts Common Properties Items Lists the collection of items within the ListBox. MultiColumn Indicates whether the ListBox can break a list into multiple columns. Multiple columns are used to make vertical scroll bars unnecessary. SelectedIndex Returns the index of the currently selected item. If the user selects multiple items, this method arbitrarily returns one of the selected indices; if no items have been selected, the method returns -1. SelectedIndices Returns a collection of the indices of all currently selected items. SelectedItem Returns a reference to the currently selected item (if multiple items are selected, it returns the item with the lowest index number). SelectedItems Returns a collection of the currently selected item(s). SelectionMode Determines the number of items that can be selected and the means through which multiple items can be selected. Values None, One, MultiSimple (multiple selection allowed) and MultiExtended (multiple selection allowed via a combination of arrow keys, mouse clicks and Shift and Control buttons). Sorted Indicates whether items appear in alphabetical order. True causes alphabetization; default is False. Common Method GetSelected 2007 Dr. Natheer Khasaneh Takes an index, and returns True if the corresponding item is selected. 36 14.6,7 ListBoxes and CheckListBoxes Fig. 14.10 String Collection Editor. 2007 Dr. Natheer Khasaneh 37 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 // Fig. 14.18: ListBoxTestForm.cs // Program to add, remove and clear ListBox items using System; using System.Windows.Forms; Outline ListBoxTest.cs // Form uses a TextBox and Buttons to add, // remove, and clear ListBox items public partial class ListBoxTestForm : Form { // default constructor public ListBoxTestForm() { InitializeComponent(); } // end constructor // add new item to ListBox (text from input TextBox) // and clear input TextBox private void addButton_Click( object sender, EventArgs e ) { displayListBox.Items.Add( inputTextBox.Text ); inputTextBox.Clear(); } // end method addButton_Click 2007 Dr. Natheer Khasawneh 38 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 Outline ListBoxTest.cs 2007 Dr. Natheer Khasawneh 39 Outline ListBoxTest.cs Program Output 2007 Dr. Natheer Khasawneh 40 14.6,7 ListBoxes and CheckListBoxes CheckedListBox De sc rip tio n / De le g a te a nd Eve nt Arg um e nts p ro p e rtie s, m e tho d s a nd e ve nts Common Properties (All the ListBox properties and events are inherited by CheckedListBox.) CheckedItems The collection of items that are checked. Not the same as the selected items, which are highlighted (but not necessarily checked). CheckedIndices Returns indices for the items that are checked. Not the same as the selected indices. SelectionMode Can only have values One (allows multiple selection) or None (does not allow multiple selection). Common Methods GetItemChecked Takes an index and returns true if corresponding item checked. Common Events (Delegate ItemCheckEventHandler, event arguments ItemCheckEventArgs) ItemCheck Raised when an item is checked or unchecked. ItemCheckEventArgs Properties CurrentValue Whether current item is checked or unchecked. Values Checked, Unchecked or Indeterminate. Index Index of item that changed. NewValue New state of item. Fig. 13.12 CheckedListBox p ro p e rtie s, m e tho d s a nd e ve nts. 2007 Dr. Natheer Khasaneh 41 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 // Fig. 14.20: CheckedListBoxTestForm.cs // Using the checked ListBox to add items to a display ListBox using System; using System.Windows.Forms; // Form uses a checked ListBox to add items to a display ListBox public partial class CheckedListBoxTestForm : Form { // default constructor public CheckedListBoxTestForm() { InitializeComponent(); } // end constructor Outline CheckedListBoxTe st.cs // item about to change // add or remove from display ListBox private void inputCheckedListBox_ItemCheck( object sender, ItemCheckEventArgs e ) { // obtain reference of selected item string item = inputCheckedListBox.SelectedItem.ToString(); // if item checked add to ListBox // otherwise remove from ListBox if ( e.NewValue == CheckState.Checked ) displayListBox.Items.Add( item ); else displayListBox.Items.Remove( item ); } // end method inputCheckedListBox_ItemCheck } // end class CheckedListBoxTestForm 2007 Dr. Natheer Khasawneh 42 Outline CheckedListBoxTe st.cs Program Output 2007 Dr. Natheer Khasawneh 43 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 2007 Dr. Natheer Khasaneh 44 14.8 ComboBoxes Fig. 14.14 Demonstrating a ComboBox. 2007 Dr. Natheer Khasaneh 45 14.8 ComboBoxes ComboBox events and Description / Delegate and Event Arguments properties Common Properties DropDownStyle Determines the type of combo box. Value Simple means that the text portion is editable and the list portion is always visible. Value DropDown (the default) means that the text portion is editable but an arrow button must be clicked to see the list portion. Value DropDownList means that the text portion is not editable and the arrow button must be clicked to see the list portion. Items Collection of items in the ComboBox control. MaxDropDownItems Maximum number of items to display in the drop-down list (between 1 and 100). If value is exceeded, a scroll bar appears. SelectedIndex Returns index of currently selected item. If there is no currently selected item, -1 is returned. SelectedItem Returns reference to currently selected item. Sorted If true, items appear in alphabetical order. Default false. Common Events (Delegate EventHandler, event arguments EventArgs) SelectedIndexChanged Raised when selected index changes (i.e., a check box has been checked or unchecked). Default when control double-clicked in designer. Fig. 14.15 ComboBox properties and events. 2007 Dr. Natheer Khasaneh 46 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 // Fig. 14.23: ComboBoxTestForm.cs // Using ComboBox to select a shape to draw. using System; using System.Drawing; using System.Windows.Forms; Outline ComboBoxTest.cs // Form uses a ComboBox to select different shapes to draw public partial class ComboBoxTestForm : Form { // default constructor public ComboBoxTestForm() { InitializeComponent(); } // end constructor // get index of selected shape, draw shape private void imageComboBox_SelectedIndexChanged( object sender, EventArgs e ) { // create graphics object, Pen and SolidBrush Graphics myGraphics = base.CreateGraphics(); // create Pen using color DarkRed Pen myPen = new Pen( Color.DarkRed ); // create SolidBrush using color DarkRed SolidBrush mySolidBrush = new SolidBrush( Color.DarkRed ); // clear drawing area setting it to color white myGraphics.Clear( Color.White ); 2007 Dr. Natheer Khasawneh 47 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 Outline ComboBoxTest.cs 2007 Dr. Natheer Khasawneh 48 Outline ComboBoxTest.cs Program Output 2007 Dr. Natheer Khasawneh 49 Outline ComboBoxTest.cs Program Output 2007 Dr. Natheer Khasawneh 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. 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. 2007 Dr. Natheer Khasaneh 52 using using using using System; System.Windows.Forms ; System.Drawing ; System.Collections ; namespace MyListControlSample { Outline ComboBoxTest.cs 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; } } 2007 Dr. Natheer Khasawneh 53 public class ListBoxSample3:Form { private ListBox ListBox1 = new ListBox(); private TextBox textBox1 = new TextBox() ; [STAThread] static void Main() { Application.Run(new ListBoxSample3()) ; } Outline ComboBoxTest.cs 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(); } 2007 Dr. Natheer Khasawneh