Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Graphical User Interfaces Part 2 Outline ListBoxes and CheckedListBoxes ListBoxes CheckedListBoxes ComboBoxes.

Similar presentations


Presentation on theme: "1 Graphical User Interfaces Part 2 Outline ListBoxes and CheckedListBoxes ListBoxes CheckedListBoxes ComboBoxes."— Presentation transcript:

1 1 Graphical User Interfaces Part 2 Outline ListBoxes and CheckedListBoxes ListBoxes CheckedListBoxes ComboBoxes

2 2 Introduction Continues study of Graphical User Interface Explores: –ListBox –CheckedListBox –ComboBoxes

3 3 ListBox es and CheckedListBox es ListBoxe s –Allow users to view and select from items on a list –Static objects – user cannot enter new items in the list Properities –Items returns all objects in list –Property SelectedItem returns current selected item –Property SelectedIndex returns index of selected item –SelectionMode property determines number of items that can be selected –SelectedItems and SelectedIndices –Sorted (default is false)

4 4 ListBox es and CheckedListBox es Common Event –SelectedIndexChanged (default) Common Method –Property GetSelected returns true if property at given index is selected –Add method adds to Items collection myListBox. Items.Add (“myListItem”) Alternatively can add items visually Examine Items property in the Properties window Click the ellipsis to open String Collection Editor

5 5 ListBox es and CheckListBox es Alternatively can add items visually 1.Examine Items property in the Properties window 2.Click the ellipsis to open String Collection Editor

6 6 CheckedListBox es CheckedListBox derives from class ListBox –Extends ListBox by placing check boxes next to items –Can add to, remove from or clear list –Can select multiple items from the list at one time Properties –CurrentValue and NewValue return state of object selected –CheckedItems and CheckedIndices return the objects and indices of selected items respectively

7 7 ListBox es and CheckedListBox es ListBox Selected Items Checked item CheckedListBox Scroll bars appear if necessary

8 8 ListBoxTest.cs Program Output

9 9 // add new item (text from input box) // and clear input box private void addButton_Click ( object sender, System.EventArgs e ) { displayListBox.Items.Add( inputTextBox.Text ); inputTextBox.Clear(); } // remove item if one selected private void removeButton_Click (object sender, System.EventArgs e ) { // remove only if item selected if ( displayListBox.SelectedIndex != -1 ) displayListBox.Items.RemoveAt(displayListBox.SelectedIndex ); } // clear all items private void clearButton_Click ( object sender, System.EventArgs e ) { displayListBox.Items.Clear(); } // exit application private void exitButton_Click ( object sender, System.EventArgs e ) { Application.Exit(); } Add event handler Add method Remove method Clear methodTest if item is selected Exit

10 10

11 11 // item about to change, add or remove from displayListBox private void inputCheckedListBox_ItemCheck ( object sender, System.Windows.Forms.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_Click ItemCheck event handler Add ItemRemove Item

12 12 ComboBox es Combine TextBox and drop-down list –Appears as a text box with a down arrow to its right –By default, user can enter text into the text box Properties: –Items: returns objects in the list –DropDownStyle: determines type of ComboBox Simple – text is editable and list portion is always visible DropDown – text is editable and down arrow (default) DropDownList – text is not editable and down arrow –SelectedItem : returns object selected –SelectedIndex : returns index of selected item –Sorted Event : SelectedIndexChanged (default)

13 13 ComboBox es Demonstrating a ComboBox.

14 14 Notes Use ComboBox to save space on a GUI –Disadvantage: unlike with ListBOx, the user cannot see available itesms without scrolling Make lists editable only if the program is designed to accept user-submitted elements. Otherwise, user might enter a custom item and then be unable to use it.


Download ppt "1 Graphical User Interfaces Part 2 Outline ListBoxes and CheckedListBoxes ListBoxes CheckedListBoxes ComboBoxes."

Similar presentations


Ads by Google