Search This Blog

Monday, March 22, 2010

How to use a ListView for the Complete Noob.

How to use a ListView for the Complete Noob.

This tutorial will go through two stages. The first stage will show how to get the listview onto your form and the different ways to set up your listview. The second stage will show how to add basic functionality to your listview.

Stage 1: The Setup

To get the ListView control onto your control toolbar, you must go to Project -> Components and select "Microsoft Windows Common Controls 6.0 (SP6)"



It will then add a bunch of controls to your toolbox.



After you add one to your form, right click on it and go to Properties so you can setup your ListView



These are the settings i'm going to use for this tutorial:



lvwReport is the standard view that everyone recognizes as a ListView. The other views you do see every day, you just don't recognize them. Your desktop is a ListView, when you open your folders and view the icons -- those are listviews.

putting LabelEdit to manual will stop users from being able to change the text on the ListView.

FullRowSelect makes it so the selected item highlights along the whole listview instead of just one column.

you should play with the different properties so you know what they all do.

Now you need to set up your columns. Still in the properties, click the Columns tab.



Add some columns and set the width and alignment to your liking. You can run your project to see what the columns look like.



Stage 2: The Code

When adding an item to a listview, you need to get the count of the listview and use it as the index. If you don't, it will add your item to the top of the listview instead of the bottom. here is the code to add text to your columns:



Static x As Integer
Dim i As Integer
With ListView1
'get how many items are in the listview so we
'can add items to the bottom, instead of the top
i = .ListItems.Count
'add text to our leftmost column
.ListItems.Add i + 1, , "Item " & x
'add text to the rest of the columns
.ListItems(i + 1).SubItems(1) = "Stashio " & x
.ListItems(i + 1).SubItems(2) = "Tacosalad " & x
End With

x = x + 1

Let me step through the code here.

Static x As Integer
Dim i As Integer
Just declaring some variables. Static is just a way of declaring a static variable locally instead of globally. it's more efficient in memory.

With ListView1
..
End With
is just for cleaner code, stops me from typing "ListView1" a bunch of times.

i = .ListItems.Count
this gets the number of items in the listview and stores it into variable i.

.ListItems.Add i + 1, , "Item " & x
ListView1.ListItems.Add [index], [key], [text]

we are adding text at index i + 1 (which would be 1 plus our Count)
key we don't need in this situation, but you are able to assign each line a unique key.
text is what we want to add.. I just threw in a string and an incrementing variable.

a few more pieces of code:

how to remove an item by index:

ListView1.ListItems.Remove [Index to remove]

Remove selected item:

With ListView1
If .ListItems.Count > 0 Then
.ListItems.Remove .SelectedItem.Index
End If
End With

how to clear a listview:

ListView1.ListItems.Clear



Thus ends the beginner tutorial for the ListView.

No comments:

Post a Comment