Tips Add Items in Listview in VB6.0 Dynamically at Runtime

The ListView control is similar to table with multiple list contain in column wise in Visual Basic 6.0. You can add Items in Listview in VB6.0 in Static or Dynamic way. Dynamic way means you can add data in Listview during runtime. You can easily Show Database record in Listview.

In ListView, the items can be displayed Following View Type:

Views Type: Details, LargeIcon, List, and SmallIcon. This control shows the View property that represents the current view.

Step 1: Add Column Headers in Listview

First of All, We have to assign Column name or add Columns Header to the ListView. The columns can be add at design time as well as run time. You can apply through adding ColumnHeader objects.

The columns will only be displayed only when this control is in Details mode. For more detail follow "Tips to easily Add Column Header in Listview in Visual Basic 6.0".


Step 2: Column Header Settings through ListView Property

For view data in grid lines with table format.

1.
Go to  "Proprties" window then go to "View" combo in "General" tab and select '3 – lvwReport' option from it.

2.
For Grid lines in ListView, check on "Gridlines" check box from "General" tab as shown In following image.

Image of ListView Propery Pages Settings
ListView Property Pages Settings

3 – lvwReport : It will show the data in table format with details.

'FullRowSelect' Checkbox : When you select any item, it will select full row. Whether clicking an item selects all its related sub-items.

'Gridlines' Checkbox : This will show rows and column containing grid lines.

Display ListView data in TextBox simple tips

3.
After applying above given setting click on "OK" button.

Step 3: Add Items in ListView through Code


4.
Now, come to Form and drag 2 "Label" from ToolBox and set Caption as 'Name:' and 'Address:'. Then drag "TextBox" to Form from ToolBox. And set "TextBox" Name as 'Name_Txt', 'Address_Txt'. And drag "CommandButton" to Form1 and set "Command1" Caption as 'Add Data' and double click on "Command1" and type code in click event as describe bellow.

We define Listview1 Name as "MyListdata"

VB6.0 Code:

Private Sub Command1_Click()

 Dim ids As Integer
 ids = 0
 ids = MyListdata.ListItems.Count + 1
 'Ids: Increment ListView Row Index for Next Record
 MyListdata.ListItems.Add , , ids
 MyListdata.ListItems(ids).ListSubItems.Add , , Name_Txt.Text
 MyListdata.ListItems(ids).ListSubItems.Add , , Address_Txt.Text

 MsgBox " Record Add Successfully"
 'Msgbox: Show message after save Record.

End Sub

Get Listview checked item in another Listview

 Step 4: Run Application to View Output

5. After completion of all given above process, run your application.

Image of Add Items In ListView Form
Add Items In ListView Form

As you can see in above image, when we type name and address in "Textbox" and press "Add Data" button. This process will show or Add Data in ListView with its related Header name through given coding.

Show Files List with File's Icons in Listview using ImageList in Vb6.0

Copy code as mention above and paste it on click events of Command button.

ListItems.Add : This will add data in first column in different rows.

ListSubItems.Add : This will add data in second or more than one column in different rows. The 'SubItems' define the next column data for process.

Simply edit or delete selected Listview data

If you are new or beginner in programming with Visual Basic, then follow the steps just like I mentioned above. And as it is, I have coded it step by step.

Now, The understanding tips to Add Items in Listview dynamically at runtime in VB6.0 code with SKOTechLearn.

Login form with ADODC MS-Access connection


0 comments: