The applications form, shown in Figure 4.31, contains a ListView control whose items can be displayed in all possible views, depending on the status of the RadioButton controls in the List Style section on the right side of the form.The controls headers and their widths were set at design time through the ColumnHeader Collection Editor, as explained earlier.
Vba Listview Control Code Is ShownTo populate the ListView control, click the Populate List button, whose code is shown next.The code creates a new ListViewItem object for each item to be added.Then it calls the Add method of the SubItems collection to add the items subitems (contact, phone, and fax numbers). After the ListViewItem has been set up, its added to the control via the Add method of its Items collection. Figure 4.31 ListView Sample Project demonstrates the basic members of the ListView control. Listing 4.46 shows the statements that insert the first two items in the list. The remaining items are added by using similar statements, which need not be repeated here. The sample data I used in the ListViewDemo application came from the Northwind sample database. Listing 4.46: Populating a ListView Control 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Dim LItem As New ListViewItem ( ) LItem. Text Alfreds Futterkiste LItem. SubItems. Add ( Anders Maria ) LItem. SubItems. Add ( 030-0074321 ) LItem. SubItems. Add ( 030-0076545 ) LItem. ImageIndex 0 ListView1. Items. Add ( LItem ) LItem New ListViewItem ( ) LItem. Text Around the Horn LItem. SubItems. Add ( Hardy Thomas ) LItem. SubItems. Add ( (171) 555-7788 ) LItem. SubItems. Add ( (171) 555-6750 ) LItem. Items. Add ( LItem ) Enumerating the List The Enumerate List button scans all the items in the list and displays them along with their subitems in the Immediate window. To scan the list, you must set up a loop that enumerates all the items in the Items collection. ![]() The complete code for the Enumerate List button is shown in Listing 4.47. Listing 4.47: Enumerating Items and SubItems 1 2 3 4 5 6 7 8 9 10 11 12 13 Private Sub bttnEnumerate Click (... Handles bttnEnumerate. Click Dim i, j As Integer Dim LItem As ListViewItem For i 0 To ListView1. Items. Count - 1 LItem ListView1. Items ( i ) Debug. WriteLine ( LItem. Text ) For j 0 To LItem. SubItems. Count - 1 Debug. WriteLine ( ListView1. Columns ( j ). Text Litem. SubItems ( j ). Text ) Next Next End Sub Notice that each item may have a different number of subitems. The output of this code in the Immediate window is shown next. The subitems appear under the corresponding item, and they are indented by three spaces: 1 2 3 4 5 6 7 8 9 10 Alfred s Futterkiste Company Alfred s Futterkiste Contact Anders Maria Telephone 030 - 0074321 FAX 030 - 0076545 Around the Horn Company Around the Horn Contact Hardy Thomas Telephone ( 171 ) 555 - 7788 FAX ( 171 ) 555 - 6750 The code in Listing 4.47 uses a For...Next loop to iterate through the items of the control. You can also set up a For Each...Next loop, as shown here: 1 2 3 4 Dim LI As ListViewItem For Each LI In ListView1. Items access the current item through the LI variable Next Related lessons: ListViews Items Collection and The SubItems Collection in VB 2008 Objects versus Object Variables in Visual Basic 2008 The CustomExplorer Project using ListView and TreeView Controls VB2008 VB.NET ListView Control Visual Basic 2008 VB.NET ListView, TreeView, and ImageList Controls in Visual Basic 2008 VB.NET ListView Items and Subitems Visual Basic 2008 Chapter: Windows Controls Previous Post: ListViews Items Collection and The SubItems Collection in VB 2008 Next Post: Sorting the ListView Control Visual Basic 2008 Primary Sidebar Chapters Getting Started with Visual Basic 2008 Using Variables and Data Types Programming Fundamentals Windows Controls Working with Forms Custom Classes Working with Objects Building Custom Windows Controls Handling Strings, Characters and Dates Storing Data in Collections Accessing Folders and Files Serialization and XML Querying Collections and XML with LINQ Drawing and Painting Secondary Sidebar Copyright 2020 W3computing.com Privacy Policy Home Visual Basic 2008 Systems Analysis SQL Server 2012 Getting Started with Visual Basic 2008 Using Variables and Data Types Programming Fundamentals Windows Controls Working with Forms Custom Classes Working with Objects Building Custom Windows Controls Handling Strings, Characters and Dates Storing Data in Collections Accessing Folders and Files Serialization and XML Querying Collections and XML with LINQ Drawing and Painting.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |