Windows Forms controls for listing options

Source: Internet
Author: User

The controls that can provide a list of options have a ListBox, ComboBox, CheckedListBox, how to properly use and select these controls, and this is discussed below. First, the functions of these three controls are described separately:

ListBox

The listbox is used to display a list of items from which the user can select one or more items. If the total number of items exceeds the number of items that can be explicitly, the listbox automatically adds a scroll bar. When the Multicolumn property is true, the list box displays entries in multiple columns, and a horizontal scroll bar appears. When Multicolumn is false, the list box displays entries in a single column, and a vertical scroll bar appears. When Scrollalwaysvisible is true, scroll bars are displayed regardless of the number of entries. The SelectionMode property is used to set how many entries can be selected at one time.

Ways to change the ListBox control

The SelectedIndex property returns the integer that corresponds to the first selected item in the list box. You can change the selected item by changing the SelectedIndex value in your code, and the selected entry is highlighted on the window. If no item is selected, the value of SelectedIndex is-1, and if the first item is selected, the SelectedIndex value is 0. When multiple items are selected, the SelectedIndex value represents the first selected entry.

The SelectedItem property is similar to SelectedIndex, but it represents the entry itself, usually a string.

The Count property represents the number of items in the list, because SelectedIndex is zero-based, so the value of the Count property is typically 1 larger than the maximum possible value of SelectedIndex.

Add or Remove entries: Add, Insert, clear or remove. You can also add items to the list by starting with the Items property.

Focus: Determine the selected entry, set the scroll bar, add the delete entry.

ComboBox

The combobox control displays the data in the following drop-down box. The ComboBox is displayed by default in two parts: The top is a text box that allows the user to type a list item, and the following is a list box that displays a list of items.

Selectedindex,count, SelectedItem, Add, Remove, Insert, clear functions exactly like a ListBox .

CheckedListBox

The CheckedListBox has been extended to the listbox. It can almost complete all tasks that the ListBox can accomplish, and you can also display a check mark next to the list item. The difference between the two is that CheckedListBox only supports Drawmode.normal mode, and CheckedListBox only 0 or 1 are not selected.

CheckedListBox adds a good entry in the String Collection editor at design time, or it can be added dynamically using the Items property.

Determine the selection of CheckedListBox

When displaying data in CheckedListBox, all data can be looped through the CheckedItems property, and the Getitemchecked method can be used to determine which items are selected. The Getitemchecked method parameter returns a BOOL value with the item index as a parameter. The SelectedItems and SelectedIndices properties are not used to determine which item is selected, but to represent the highlighted item.

The method for determining which items are selected in CheckedListBox is as follows:

  1. Starting from 0, the loop accesses the CheckedItems combination. Note that this method provides items that are selected in the list of items instead of the entire list. Therefore, if the first item is not selected and the second item is selected, the following code displays the text similar to "Checked Item 1 = mylistitem 2".
  2. /DetermineifThere is any itemschecked.if(CheckedListBox1.CheckedItems.Count! =0){   //If So, the loop through all checked items and print results.   strings ="";  for(intx =0; X <= CheckedListBox1.CheckedItems.Count-1; X + +) {s= S +"Checked Item"+ (x+1). ToString () +" = "+ checkedlistbox1.checkeditems[x]. ToString () +"\ n"; }messagebox.show (s);}
  3. The Items property loops through all items, calls the Getitemchecked method for each item, or confirms the selected item:
  4. inti;strings; s="Checked items:\n" ; for(i =0; I <= (checkedlistbox1.items.count-1); i++){   if(checkedlistbox1.getitemchecked (i)) {s= S +"Item"+ (i+1). ToString () +" = "+ Checkedlistbox1.items[i]. ToString () +"\ n"; }}messagebox.show (s);
When to use a ComboBox rather than a ListBox

ComboBox and ListBox have similar behavior and can be interchanged in some cases. Suggestions for use are as follows:

Use a ComboBox when you want to provide a set of recommended options, or if you want to limit the input or determine the list of options, use the listbox. The ComboBox contains a text box, so you can enter options that are not in the list, except when its DropDownStyle property is set to DropDownList, when the first letter you type matches an entry, the entry is automatically selected.

In addition, the ComboBox is more space-saving, which is well understood. However, when DropDownStyle is set to simple, the full list is always displayed, and the ComboBox occupies more space than the listbox.

Sort the contents of the Combobox,listbox,checkedlistbox

When a Windows Forms control is bound to data, sorting is not supported, and to sort the data, you must use a data source that supports sorting and then sort the data source.

If you are not a data-bound control, you can sort by the following methods:

    • Set the sorted property to True.

is not very simple ...

How to bind a ComboBox and a listbox to data

Binding a ComboBox and a listbox to data makes it easy to browse the database, add new data, edit existing data, and more.

The process of binding a ComboBox and a ListBox is as follows:

    1. Set the DataSource property to the data source object. Possible sources of data include BindingSource, table, and so on.
    2. If you are binding to a table, you need to set the DisplayMember property to the name of the column that you want to display.

Windows Forms controls for listing options

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.