How to Add Images in a ListView Using VB.Net
Submitted by janobe on Monday, August 19, 2019 - 17:15.
This time, I will teach you how to add images in a ListView using VB.Net.The ListView is an object that can handle the data that will be displayed in a list form. So, this program will illustrate how to display a list of data with images in a ListView by adding it programmatically. Let’s begin.
The complete source code is included. You can download it and run it on your computer.
For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below.
Creating Application
Step 1
Open Microsoft Visual Studio 2015 and create a new windows form application.Step 2
Add a ListView inside the form and do the form just like shown below.Step 3
Press F7 to open the code editor. In the code editor, declare an item in the listview control
and an image list
to control an image object.
- Dim lstviewItem As ListViewItem
- Dim lstviewItemImageList As ImageList = New ImageList()
Step 4
Create a method for populating the data with images in the listview.- Private Sub load_data()
- Try
- 'set the properties of the listview
- ListView1.View = View.Details
- ListView1.Columns.Add("Pictures")
- ListView1.Columns(0).Width = 500
- 'declare a string array
- Dim imageList() As String
- 'add a values in the array
- imageList = New String() {"meme 1.png", "meme 2.jpg", "meme 3.png", "meme 4.jpg", "meme 5.jpg", "meme 6.png"}
- 'display an array values in the listview
- For Each images As String In imageList
- lstviewItem = New ListViewItem(Application.StartupPath & "/memes/" & images)
- lstviewItemImageList.ImageSize = New Size(100, 100)
- ListView1.SmallImageList = lstviewItemImageList
- lstviewItem.ImageIndex = lstviewItemImageList.Images.Add(Image.FromFile(lstviewItem.Text), Color.Transparent)
- ListView1.Items.Add(lstviewItem)
- Next
- Catch ex As Exception
- 'catching error
- MessageBox.Show("error : " + ex.Message)
- End Try
- End Sub
Step 5
Write the following code execute the method that you have created in the first load of the form.- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- 'display the data in the listview in the first load of the form
- load_data()
- End Sub
Add new comment
- 7227 views