DotNet Tutorials

V4 Dot Net Tutorials

Server Intellect Cloud Hosting

 Using the ListView control in WinForms .NET 3.5 VB

This tutorial will show you how to use the ListView control, and how to add items to it as well as remove. VB version.

This tutorial will show how we can use the ListView control to display a list of items on a Windows Form, under different headers. The ListView control is very flexible, and is similar to the Windows File Explorer. We are able to use the ListView to output information to the user under several headings.
In this example, we will add a ListView to our form and use buttons to add and then manipulate the data we add - removing and clearing. We will also show how we can change the view of the ListView.

To start, we add a ListView control, four buttons, and a group of 5 Radio Buttons. Our Windows Form Designer generated code looks like this:

Private Sub InitializeComponent()
Me.ListView1 = New System.Windows.Forms.ListView
Me.ColumnHeader1 = New System.Windows.Forms.ColumnHeader
Me.ColumnHeader2 = New System.Windows.Forms.ColumnHeader
Me.ColumnHeader3 = New System.Windows.Forms.ColumnHeader
Me.Label1 = New System.Windows.Forms.Label
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.Button3 = New System.Windows.Forms.Button
Me.Button4 = New System.Windows.Forms.Button
Me.GroupBox1 = New System.Windows.Forms.GroupBox
Me.RadioButton1 = New System.Windows.Forms.RadioButton
Me.RadioButton2 = New System.Windows.Forms.RadioButton
Me.RadioButton3 = New System.Windows.Forms.RadioButton
Me.RadioButton4 = New System.Windows.Forms.RadioButton
Me.RadioButton5 = New System.Windows.Forms.RadioButton
Me.GroupBox1.SuspendLayout()
Me.SuspendLayout()
'
'ListView1
'
Me.ListView1.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.ColumnHeader1, Me.ColumnHeader2, Me.ColumnHeader3})
Me.ListView1.Location = New System.Drawing.Point(12, 12)
Me.ListView1.Name = "ListView1"
Me.ListView1.Size = New System.Drawing.Size(185, 113)
Me.ListView1.TabIndex = 0
Me.ListView1.UseCompatibleStateImageBehavior = False
Me.ListView1.View = System.Windows.Forms.View.Details
'
'ColumnHeader1
'
Me.ColumnHeader1.Text = "Name"
'
'ColumnHeader2
'
Me.ColumnHeader2.Text = "Age"
'
'ColumnHeader3
'
Me.ColumnHeader3.Text = "City"
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(5, 242)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(296, 13)
Me.Label1.TabIndex = 1
Me.Label1.Text = "For more .NET Tutorials, please visit www.dotnettutorials.com"
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(12, 131)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(75, 23)
Me.Button1.TabIndex = 2
Me.Button1.Text = "Add New"
Me.Button1.UseVisualStyleBackColor = True
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(12, 160)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(75, 23)
Me.Button2.TabIndex = 3
Me.Button2.Text = "Remove"
Me.Button2.UseVisualStyleBackColor = True
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(122, 131)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(75, 23)
Me.Button3.TabIndex = 4
Me.Button3.Text = "Clear"
Me.Button3.UseVisualStyleBackColor = True
'
'Button4
'
Me.Button4.Location = New System.Drawing.Point(241, 216)
Me.Button4.Name = "Button4"
Me.Button4.Size = New System.Drawing.Size(75, 23)
Me.Button4.TabIndex = 5
Me.Button4.Text = "Exit"
Me.Button4.UseVisualStyleBackColor = True
'
'GroupBox1
'
Me.GroupBox1.Controls.Add(Me.RadioButton5)
Me.GroupBox1.Controls.Add(Me.RadioButton4)
Me.GroupBox1.Controls.Add(Me.RadioButton3)
Me.GroupBox1.Controls.Add(Me.RadioButton2)
Me.GroupBox1.Controls.Add(Me.RadioButton1)
Me.GroupBox1.Location = New System.Drawing.Point(205, 12)
Me.GroupBox1.Name = "GroupBox1"
Me.GroupBox1.Size = New System.Drawing.Size(111, 142)
Me.GroupBox1.TabIndex = 6
Me.GroupBox1.TabStop = False
Me.GroupBox1.Text = "GroupBox1"
'
'RadioButton1
'
Me.RadioButton1.AutoSize = True
Me.RadioButton1.Location = New System.Drawing.Point(11, 19)
Me.RadioButton1.Name = "RadioButton1"
Me.RadioButton1.Size = New System.Drawing.Size(81, 17)
Me.RadioButton1.TabIndex = 0
Me.RadioButton1.TabStop = True
Me.RadioButton1.Text = "Large Icons"
Me.RadioButton1.UseVisualStyleBackColor = True
'
'RadioButton2
'
Me.RadioButton2.AutoSize = True
Me.RadioButton2.Location = New System.Drawing.Point(11, 43)
Me.RadioButton2.Name = "RadioButton2"
Me.RadioButton2.Size = New System.Drawing.Size(79, 17)
Me.RadioButton2.TabIndex = 1
Me.RadioButton2.TabStop = True
Me.RadioButton2.Text = "Small Icons"
Me.RadioButton2.UseVisualStyleBackColor = True
'
'RadioButton3
'
Me.RadioButton3.AutoSize = True
Me.RadioButton3.Location = New System.Drawing.Point(11, 67)
Me.RadioButton3.Name = "RadioButton3"
Me.RadioButton3.Size = New System.Drawing.Size(41, 17)
Me.RadioButton3.TabIndex = 2
Me.RadioButton3.TabStop = True
Me.RadioButton3.Text = "List"
Me.RadioButton3.UseVisualStyleBackColor = True
'
'RadioButton4
'
Me.RadioButton4.AutoSize = True
Me.RadioButton4.Location = New System.Drawing.Point(11, 90)
Me.RadioButton4.Name = "RadioButton4"
Me.RadioButton4.Size = New System.Drawing.Size(42, 17)
Me.RadioButton4.TabIndex = 3
Me.RadioButton4.TabStop = True
Me.RadioButton4.Text = "Tile"
Me.RadioButton4.UseVisualStyleBackColor = True
'
'RadioButton5
'
Me.RadioButton5.AutoSize = True
Me.RadioButton5.Location = New System.Drawing.Point(11, 114)
Me.RadioButton5.Name = "RadioButton5"
Me.RadioButton5.Size = New System.Drawing.Size(57, 17)
Me.RadioButton5.TabIndex = 4
Me.RadioButton5.TabStop = True
Me.RadioButton5.Text = "Details"
Me.RadioButton5.UseVisualStyleBackColor = True
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(328, 264)
Me.Controls.Add(Me.GroupBox1)
Me.Controls.Add(Me.Button4)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.ListView1)
Me.Name = "Form1"
Me.Text = "ListView Control VB"
Me.GroupBox1.ResumeLayout(False)
Me.GroupBox1.PerformLayout()
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

This is the code that is generated by Visual Studio when we design our form. Yours may look different.

Once we have added all of our controls to the form, we want to click on the Smart Tag of the ListView and choose Edit Columns. For this example, we are going to use Name, Age, and City.

Once we have done this, we can start with the code-behind. Although the first thing we want to code is our Add button. Choose one of the buttons and set the text to Add, then double-click it to create the event handler. Then we can use the following code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim newItem As ListViewItem
newItem = ListView1.Items.Add(InputBox("Name:", "Please enter name", "name", 200, 200))
With newItem
.SubItems.Add(InputBox("Age:", "Please enter age", "age", 200, 200))
.SubItems.Add(InputBox("City:", "Please enter city", "city", 200, 200))
End With
End Sub

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

This code is for the Add button. We are simply adding to the ListView columns - Name is the main element, and Age and City are the SubItems of that element. For each item, we use the InputBox to gather information from the user.
Next, we can add code to the other buttons. One will be used to remove all items from the ListView, another to remove the selected item(s) from the ListView, and the third button will be the Exit button:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
For Each toDelete As ListViewItem In ListView1.SelectedItems
toDelete.Remove()
Next toDelete
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
ListView1.Items.Clear()
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Close()
End Sub

Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!

Note that these are very simple methods, but can be powerful tools.
Finally, we want to add code to our RadioButtons. We will use them to change the view of the ListView. The ListView control has 5 built-in views, which we will use to switch between:

Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
ListView1.View = View.LargeIcon
End Sub

Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
ListView1.View = View.SmallIcon
End Sub

Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
ListView1.View = View.List
End Sub

Private Sub RadioButton4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton4.CheckedChanged
ListView1.View = View.Tile
End Sub

Private Sub RadioButton5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton5.CheckedChanged
ListView1.View = View.Details
End Sub

Looking for the C#.NET 2008 Version? Click Here!

Looking for more .NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!