Thursday, April 12, 2018

Tips add Items in Listview dynamically at Runtime in VB6.0

Tips to add Items in ListView dynamically at Runtime in VB6

The ListView control is similar to table with multiple list contain in column wise in Visual Basic 6.0.
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
First of All, We have to assign 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 the link "Add Column Header in Listview in Visual Basic 6.0 Tips".

 Step 2: General Settings in Propertiy Pages
For view data in grid lines with table format. Then, go to  "Proprties" window and go to "View" combo in "General" tab and select '3 – lvwReport' option from it. For grid lines, 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.


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

 Step 3: Add Items through Code
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


 Step 4: Run Application to view Output
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.

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.


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.

0 comments: