The listview control can be used to display a list of items with icons or a list with subitems. The folder window in the Windows operating system resource manager is the best application example. As shown in, "my computer" uses the listview control to display all the drive letters of the local machine:
Note: You can use the "View" menu to observe the other three display modes. These are one of the view modes of listview.
Next, let's take a look at the basic application of the listview control. In the toolbox of VB.net, the icons of the listview control are shown in Figure 2:
I. view attributes
The listview control is a list control that can display icons or subitems. Its most important attribute is the view attribute, which determines which view mode to display the control items, the four view modes are as follows:
1. larticon: Large icon view mode. A large icon is displayed next to the item text. If the width of the control is sufficient, the items are arranged in parallel with the drive letters in the same column, if the number is not complete, the new line is automatically wrapped in a new line.
2. smallicon: the small icon view mode, which is the same as the large icon mode, but displays small icons.
3. List: In list view mode, small icons are displayed, but items are vertically arranged and only single columns are displayed.
4. Details: Details View mode is the most abundant option. It not only allows you to view items, but also allows you to view any subitem specified for each item. Items are displayed in the grid. They are vertically arranged and their sub-items are displayed in the column (with column headers ). Correspondingly, only the control attributes that play a role in the details view mode are gridlines and fullrowselect. The gridlines attribute indicates whether grid lines are displayed between rows and columns that contain items and their subitems. The fullrowselect attribute indicates whether to select all its sub-items (that is, the entire row is selected) When you click an item. As shown in figure 3, the gridlines and fullrowselect attributes are set to true:
The headerstyle attribute in the listview control also works in the details view mode. The headerstyle attribute indicates the column header style. It has the following three display styles:
1. clickable: the column header is similar to a button. You can perform operations (such as sorting) When you click it ).
2. nonclickable: the column header does not respond to mouse clicks.
3. None: the column header is not displayed in the report view.
Ii. Items attributes
In addition, the most important attribute of the listview control is the items attribute, which contains all items of the control. The selecteditems attribute is the set of items currently selected by the control, and the selectedindices attribute associated with it is the index set of the selected items in the control, as shown in the following example, the selected indexes are obtained in the selecteditems and selectedindices attributes respectively. The listview control and a button control are dragged and dropped on the form, and several columns and rows have been added to the listview control,CodeAs follows:
Private sub button#click (byval sender as system. Object, byval e as system. eventargs) handles button1.click
Dim I as integer = 0
Dim STR as string = ""
For I = 0 to listview1.selecteditems. Count-1
STR = STR & "selected index =" & listview1.selecteditems. Item (I). Index. tostring &";"
STR = STR & "index in the control =" & listview1.selectedindices. Item (I). tostring & vbcr
Next
MessageBox. Show (STR)
End sub
Figure 4 shows the effect after running:
Both attributes can obtain the index of all options in the control. In actual application, we can flexibly choose to use one of them to simplify the code.
Iii. Other important attributes of listview
The listview control also has some important attributes that are commonly used.
1. Activation attributes
The activation attribute specifies how the user activates the items in the listview control. It includes three optional values:
Oneclick: the user must click to activate the item. The cursor changes to a hand pointer, and the item text changes the color when you move the mouse pointer over the item.
Standard: You must double-click to activate the item. No feedback is provided when you move the cursor over an item.
Twoclick: You must double-click the item to activate the item. The item text changes the color when you move the mouse pointer over the item.
The items in activating listview are different from those in selecting only items. When an item is activated, it is usually processed in the itemactivate event.Program. For example, when activating an item, you may open a file or display a dialog box that allows users to edit the item. Generally, you can double-click an item to activate it. If the activation attribute is set to oneclick, click this item once to activate it. Set the activation attribute to twoclick, which is different from the standard double-click because the interval between two clicks can be arbitrary.
Note: If you set the activation attribute to itemactivation. oneclick or itemactivation. twoclick, tag editing is not allowed regardless of the value of labeledit.
Setting the activation attribute actually determines how to trigger the itemactivate event. If we need to perform additional operations when activating the item, for example, a dialog box for association is displayed, you can write the appropriate code in the itemactivate event, as shown in the following code:
Private sub listviewincluitemactivate (byval sender as object, byval e as system. eventargs) handles listview1.itemactivate
MessageBox. Show ("what do you want to do") 'You can add the required operations here.
End sub
After the code is run, we activate the options, as shown in Figure 5:
2. labeledit attributes
The labeledit attribute indicates whether the user can edit the label of an item in the control.
When the labeledit attribute is set to true, you can click the item text, select it, and click the item text again to place the label text in the edit mode. Then, you can modify or replace the text tag of an item. Before and after editing the item text, you can use the beforelabeledit and afterlabeledit events to execute tasks. If this attribute is set to true, the text of the subitem cannot be modified. To change the text of a subitem, you can double-click the subitem in the control to display the dialog box.
3. labelwrap attributes
The labelwrap attribute indicates whether to wrap the item label when the item is displayed as an icon in the control.
If labelwrap is set to true, the item text is changed to the next line of the text if needed. If the text is longer than two lines, the text will be shortened. If this item is selected, all item text is displayed. If labelwrap is set to false, all item text is displayed in a single row. The entire item text is displayed when labelwrap is set to false. This attribute is used only when the view attribute is set to largeicon or smallicon.
4. multiselect attributes
The multiselect attribute indicates whether multiple items can be selected. When the multiselect attribute is set to true, you can select multiple items in the listview control.
5. scrollable attributes
The scrollable attribute indicates whether to display the scroll bar when there is not enough space to display all items.
6. Sorting attributes
The sorting attribute indicates the sorting order of items in the control.
The sorting attribute allows you to specify whether items are sorted in the listview control. By default, sorting is not performed. When the sorting attribute is set to ascending or descending, items in listview are automatically alphabetically in ascending order (when the attribute is set to ascending) or in descending order (when the attribute is set to descending) sort. You can use this property to automatically sort the items displayed in the listview control, making it easier for you to find items when a large number of items are available. If you want to sort items by yourself without using the sorting attribute, you can use the listviewitemsorter attribute together with the sort method.
7. largeimagelist attributes
The largeimagelist attribute sets the imagelist that is used when an item is displayed with an uppercase icon in the control.
8. smallimagelist attributes
The smallimagelist attribute sets the imagelist used when an item is displayed as a small icon in the control.
We have learned about some of the main attributes of listview. Let's see how to use it.
4. add items to the listview Control
1. You can use the listviewitem Set editor to add items to the control.
You can select the items attribute in the Properties window of the listview control.
2. You can also use the items. Add () method of the listview control to add new items to it. The format of the add () method is as follows:
Add (text, imageindex)
Text indicates the text displayed for the item to be added.
Imageindex is an optional parameter that indicates the icon index in the corresponding imagelist
Example:
Add a listview control, an imagelist control, and a button control to the form. Then add the following code in the Code Editor:
Private sub button#click (byval sender as system. Object, byval e as system. eventargs) handles button1.click
Dim mitem as listviewitem
Dim I as integer = 0
Listview1.largeimagelist = me. imagelist1
Listview1.view = view. largeicon
For I = 0 to 3
Listview1.items. Add ("item" & I, I)
Next
End sub
After running, click the button, as shown in figure 6:
You can also use the following code to add an item. The running effect is the same:
Private sub button#click (byval sender as system. Object, byval e as system. eventargs) handles button1.click
Listview1.largeimagelist = me. imagelist1
Listview1.view = view. largeicon
Dim item0 as new listviewitem ("item 0", 0)
Dim Item1 as new listviewitem ("item 1", 1)
Dim item2 as new listviewitem ("item 2", 2)
Dim item3 as new listviewitem ("item 3", 3)
Listview1.largeimagelist = me. imagelist1
Listview1.items. addrange (New listviewitem () {item0, Item1, item2, item3 })
End sub
5. Add a column title for the listview Control
When we use the Details View Mode of the listview control, we must add the corresponding column title to the control to display all its items.
1. Add a column title in view mode
Generally, when we display a list, the column title should be fixed, so we can set the column title in view mode in advance, which is more intuitive.
Select the listview control, find the columns attribute in Its Properties window, and click the omitting symbol, as shown in figure 7:
Then, add and delete the column title in the pop-up set editor. You can edit the text, width, and text alignment of the corresponding column on the right of the editor, as shown in Figure 8:
After adding the column title, remember to set the view attribute of the listview control to details to display the column title.
2. Use code to add a column title
We can use columns. add method to dynamically add column headers and Use columns. the clear () method is used to clear all column headers. As shown in the following code, four column headers are dynamically added. The width of each column header is 50,
Private sub button#click (byval sender as system. Object, byval e as system. eventargs) handles button1.click
Listview1.view = view. Details
Listview1.gridlines = true
Listview1.columns. Clear ()
Listview1.columns. Add ("column 1", 50, horizontalalignment. Left)
Listview1.columns. Add ("column 2", 50, horizontalalignment. Left)
Listview1.columns. Add ("column 3", 50, horizontalalignment. Left)
Listview1.columns. Add ("column 4", 50, horizontalalignment. Center)
Listview1.refresh ()
End sub
After running, click the button, as shown in figure 9:
In this chapter, we mainly understand the basic usage of the listview control. I believe that you will also use it to display the list in your own program. Please keep an eye on our clever reader network. In the next chapter, we will see how to use the listview control to simulate the folder Display Effect in the Windows operating system.
Very detailed, reposted by others, origin: VB.net entry-Use of listview controls-Webmaster College
Http://edu.cnzz.cn/NewsInfo/10927_2.aspx