Download Source Code -How to Add Images in a ListView Using VB.Net

Loading...

How to Add Images in a ListView Using VB.Net

Submitted by: 
janobe
Language: 
Visual Basic .NET
Visitors have accessed this post 2 times.

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.

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application.
ps1

Step 2

Add a ListView inside the form and do the form just like shown below.
ps2

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.

  1.  
  2. Dim lstviewItem As ListViewItem
  3. Dim lstviewItemImageList As ImageList = New ImageList()

Step 4

Create a method for populating the data with images in the listview.

  1.  
  2. Private Sub load_data()
  3. Try
  4. 'set the properties of the listview
  5. ListView1.View = View.Details
  6. ListView1.Columns.Add("Pictures")
  7. ListView1.Columns(0).Width = 500
  8.  
  9. 'declare a string array
  10. Dim imageList() As String
  11. 'add a values in the array
  12. imageList = New String() {"meme 1.png", "meme 2.jpg", "meme 3.png", "meme 4.jpg", "meme 5.jpg", "meme 6.png"}
  13.  
  14. 'display an array values in the listview
  15. For Each images As String In imageList
  16. lstviewItem = New ListViewItem(Application.StartupPath & "/memes/" & images)
  17. lstviewItemImageList.ImageSize = New Size(100, 100)
  18. ListView1.SmallImageList = lstviewItemImageList
  19. lstviewItem.ImageIndex = lstviewItemImageList.Images.Add(Image.FromFile(lstviewItem.Text), Color.Transparent)
  20. ListView1.Items.Add(lstviewItem)
  21. Next
  22.  
  23. Catch ex As Exception
  24. 'catching error
  25. MessageBox.Show("error : " + ex.Message)
  26. End Try
  27. End Sub

Step 5

Write the following code execute the method that you have created in the first load of the form.

  1.  
  2. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  3. 'display the data in the listview in the first load of the form
  4. load_data()
  5. End Sub

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.

Tags: 
adding images in a listview
listview control
vb
vb.net

Add new comment

(Visited 1 times, 1 visits today)
Loading...