Windows Forms control and windows Forms control used to list options

Source: Internet
Author: User

Windows Forms control and windows Forms control used to list options

Controls that provide the option list include ListBox, ComboBox, and CheckedListBox. The following describes how to correctly use and select these controls. The functions of these three controls are described as follows:

ListBox

ListBox is used to display the item list. You can select one or more items. If the total number of projects exceeds the number of projects that can be explicitly displayed, The ListBox automatically adds a scroll bar. When the MultiColumn attribute is true, the list box displays entries in multiple columns and a horizontal scroll bar appears. When MultiColumn is set to false, the list box displays entries in a single column and a vertical scroll bar appears. When ScrollAlwaysVisible is true, the scroll bar is displayed regardless of the number of entries. The SelectionMode attribute is used to set the number of entries that can be selected at a time.

How to change the ListBox control

The SelectedIndex attribute returns the integer corresponding to the first selected entry in the list box. You can change the selected items by changing the SelectedIndex value in the Code. The selected items are highlighted in the window. If no item is selected, the value of SelectedIndex is-1. If the first item is selected, the value of SelectedIndex is 0. When multiple items are selected, the SelectedIndex value indicates the first selected entry.

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

The Count attribute indicates the number of items in the list. Because SelectedIndex starts from scratch, the value of the Count attribute is usually 1 larger than the maximum possible value of SelectedIndex.

Add or delete entries: Add, Insert, Clear, or Remove. You can also add Items to the list by using the Items attribute.

Key: Determine the selected entries, set the scroll bar, and add and delete entries.

ComboBox

The ComboBox control displays data in a drop-down box. By default, ComboBox is displayed in two parts: the top is a text box that allows users to type list items; the bottom is a list box that displays a list of items.

The functions of SelectedIndex, Count, SelectedItem, Add, Remove, Insert, and Clear are the same.ListBoxExactly the same.

CheckedListBox

CheckedListBox extends ListBox. It can complete almost all tasks that ListBox can complete, and display a check mark next to the list item. The difference between the two is that CheckedListBox only supports the DrawMode. Normal mode, and CheckedListBox can only have 0 or 1 items not selected.

When designing CheckedListBoxString Collection EditorYou can also add entries dynamically using the Items attribute.

Determine the selection items of CheckedListBox

When you use CheckedListBox to display data, you can use the CheckedItems attribute to cyclically access all data, and you can use the GetItemChecked method to determine which items are selected. The GetItemChecked method parameter takes the project index as the parameter and returns the bool value. The SelectedItems and SelectedIndices attributes are not used to determine the selected items, but are used to indicate the highlighted items.

To determine the selected project in CheckedListBox, follow these steps:

When to use ComboBox instead of ListBox

ComboBox and ListBox have similar behaviors and can be exchanged in some cases. The usage suggestions are as follows:

Use ComboBox when you want to provide a set of recommended options. If you want to restrict the input or confirm the Option List, use ListBox. ComboBox contains a text box, so you can enter options not in the list, except when its DropDownStyle attribute is set to DropDownList, when the first letter you typed matches a certain entry, this entry is automatically selected.

In addition, ComboBox is space-saving, which is easy to understand. However, when DropDownStyle is set to Simple, the complete list is always displayed. In this case, ComboBox occupies more space than ListBox.

Sort ComboBox, ListBox, and CheckedListBox content

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

If it is not a data binding control, you can sort it as follows:

  • Set the Sorted attribute to true.

Isn't it easy ......

How to bind ComboBox and ListBox to data

By binding ComboBox and ListBox to data, you can easily browse databases, add new data, and edit existing data.

The process of binding ComboBox and ListBox is the same, as shown below:

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.