Net-informations.com
SiteMap  | About    

ListView Control

List views displays a collection of items that can be displayed using one of five different views, such as LargeIcon, Details , SmallIcon, List and Tile.

vb.net-listview.jpg

ListView provides a large number of properties that provide flexibility in appearance and behavior. The View property allows you to change the way in which items are displayed. and the SelectionMode property determines how many items in the list can be selected at a time.

The following Vb.Net program first set its view property as Details and GridLines property as true and FullRowSelect as true.

  ListView1.View = View.Details
  ListView1.GridLines = True

ListView1.FullRowSelect = True

After that it fills column header and then the column values.

  ListView1.Columns.Add("ProductName", 100)

Finally in the button click event, it will display the selected row values in a message box.




Public Class Form1

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		''Set view property
		ListView1.View = View.Details
		ListView1.GridLines = True
		ListView1.FullRowSelect = True

		'Add column header
		ListView1.Columns.Add("ProductName", 100)
		ListView1.Columns.Add("Price", 70)
		ListView1.Columns.Add("Quantity", 70)

		'Add items in the listview
		Dim arr(3) As String
		Dim itm As ListViewItem

		'Add first item
		arr(0) = "product_1"
		arr(1) = "100"
		arr(2) = "10"
		itm = New ListViewItem(arr)
		ListView1.Items.Add(itm)

		'Add second item
		arr(0) = "product_2"
		arr(1) = "200"
		arr(2) = "20"
		itm = New ListViewItem(arr)
		ListView1.Items.Add(itm)

	End Sub

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

		Dim productName As String
		Dim price As String
		Dim quantity As String

		productName = ListView1.SelectedItems.Item(0).SubItems(0).Text
		price = ListView1.SelectedItems.Item(0).SubItems(1).Text
		quantity = ListView1.SelectedItems.Item(0).SubItems(2).Text

		MsgBox(productName & " , " & price & " , " & quantity)

	End Sub
End Class

 VB.Net GUI - Related Contents