Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use listbox method in C #

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to use the listbox method in c #". In the daily operation, I believe that many people have doubts about how to use the listbox method in c #. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about how to use the listbox method in c #! Next, please follow the editor to study!

About ListBox

ListBox is a list control in WinForm, it provides a list of items (a set of data items), users can select one or more items, when there are too many list items, ListBox will automatically add a scroll bar, so that users can scroll through all options. ListBox can set the contents of the list in advance, or bind other controls or databases to automatically update entries and display the data one by one.

ListBox Common Properties

* list index value, which refers to the sequence number of the entry in the list, starting with 0, such as 0mem1pl 2p3e. When adding, deleting, inserting and other operations to the list, the sort of items changes, and so does the index.

* the selection index refers to the sequence number of the selected items, which also starts from 0. When operations such as adding or deleting selected items are carried out, the number changes and the index also changes. It is as if my name is Gongliang, a lunatic, sorted by pinyin. The student number in the class is 66, while among the 10 students in the class surnamed Qiao, I am No. 3.

The difference between the two is as follows (with a color background as selected):

The common attribute description Items refers to the collection of all items in the list, which is an array collection. Through this attribute, you can add, remove, or get the contents of the list. MultiColumn is used to set or get a value (bool value), indicating whether multiple columns are allowed to be displayed. True represents multiple columns, and false represents a single column. The default is false. CoumnWidth is used to get the width of the current column of the list or to set the width of the current column of the list SelectionMode

Sets the selection method for list entries.

SelectionMode.None indicates that selection is not allowed

SelectionMode.One indicates that users are only allowed to select one item

SelectionMode.MultiExtended means multiple items are allowed to be selected, but the selected items must be contiguous (adjacent)

SelectionMode.MultiSimple means multiple items are allowed to be selected, and multiple entries can be selected at will. The figure below is as follows

SelectedIndex

* get the index of the selected item

When no item is selected, the return value is 1

When selected, the attribute value is the index of the selected item

When multiple selections, it represents the index of the first selected item, you can also use SelectedIndex [I] to get other selected indexes, or you can make an item selected. I is the sequence number of the selected item starting at 0.

SelectedIndeces is used to get a collection of 0-based indexes of selected items, generally only for multiple items. Similar to SelectedIndex, but SelectedIndeces only has the ability to get indexes. SelectedItem gets the currently selected item in the list. Note that what you get is the text content of the selected item in the list, while SelectedIndex and SelectedIndeces get only the selected item index (int). SelectedItems

Gets the collection of selected items, using SelectedItems [I] to get the text content of the selected item, and I is the index of the selected item collection.

SelectedItems is similar to SelectedIndex, but one is to get the text field and the other is to get the index.

Sorted is used to set or get whether the list is sorted alphabetically (bool). Text gets or searches the text of the currently selected item of the list control. ItemsCount is used to get the number of entries in the current list. ListBox adds, inserts or deletes content

This is done by using the Items property, create a new window in visual studio, drag a list control, and the control name is lixtBox1

ListBox1.Items.Add ("item text to be added"); / / listBox1.Items.Insert (I, "item text to be added") will be added after the list; / / A new entry listBox1.Items.Remove ("put the text content of the item you want to remove here") will be inserted after the entry with index I; / / delete is done with text content, not index listBox1.Items.Clear () / / clear all entries in the list listBox1.ClearSelected (); / / deselect all selected items, that is, become unchecked ListBox deletes multiple items

A lot of friends wrestle here, and vs always prompts the array to go out of bounds. Because ListBox is deleted by specifying the text of an item, and can only be deleted item by item, and the index will change after deleting an item, the novice may be frustrated. The code for two ways of deletion is provided below for beginners' reference.

For (int iSuppli listBox1.SelectedItems.CountMutual 1myi > = 0; iWhat -) {/ / to delete listBox1.Items.Remove (listBox1.SelectedItems [I]) from back to front; / / SelectedItems directly gets the text of the selected item} / / if you are a novice, do not understand the above code, don't worry, skip here, take a look at the following content first, and then we will introduce for (int itemlistBox1.SelectedItems.CountedItems) in more detail later. ) {/ / also delete listBox1.Items.Remove (listBox1.Items [listBox1.SelectedIndices [I]]); / / get the index first, and then get the text content} get the number of collections int I = 0bot itemslistBox1.Items.Count; / / get the number of all entries in the list I = listBox1.SelectedIndices.Count / / get the number of selected items I = listBox1.SelectedItems.Count; / / get the number of selected items / / listBox1.SelectedIndex cannot get the number! Please refer to the property sheet for the key word "collection". You can get the properties of the collection before you can get the quantity to get the contents of all the selected items.

Create a new TextBox control with name as textBox1, with the following code

TextBox1.Clear (); textBox1.Text = "the content of the selected item is\ r\ n"; for (int item0

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report