VB.NET MS Access - Images and Text

July 14, 2017 Oclemy VB.NET ListView 1 minute, 27 seconds

ListView in VB.NET and .NET general is powerful component that can render lists or tables of data.

It's customizable and powerful and can be used with a variety of data source through data binding.

However, we can also add data programmatcially by creating ListViewItems and adding them.

This means that we can create our rows using custom logic. Moreover ListView is flexible and allows us even render binary data like images in our rows.

In this class we render both images and text in our ListView.

Here we explore vb.net ListView.How to load and show images and text from our file system into our listview.

Cheers.

Imports System.IO

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'SET LISTVIEW PROPERTIES
        ListView1.View = View.Details
        ListView1.FullRowSelect = True

        'CONSTRUCT COLUMNS
        ListView1.Columns.Add("Name", 250)

    End Sub

    Private Sub populate()
        Dim imgs As ImageList = New ImageList()
        imgs.ImageSize = New Size(50, 50)

        'our files img
        Dim files As String() = New String() {}
        files = Directory.GetFiles("D:/Imgs")

        Try
            For Each f In files
                imgs.Images.Add(Image.FromFile(f))
            Next

        Catch ex As Exception

        End Try

        ListView1.SmallImageList = imgs
        ListView1.Items.Add("Michael carrick", 0)
        ListView1.Items.Add("Diego Costa", 1)
        ListView1.Items.Add("David De Gea", 2)
        ListView1.Items.Add("Eden Hazard", 3)
        ListView1.Items.Add("Anders Herera", 4)
        ListView1.Items.Add("Oscar", 5)
        ListView1.Items.Add("Aaron Ramsey", 6)

    End Sub

    Private Sub populateBtn_Click(sender As Object, e As EventArgs) Handles populateBtn.Click
        populate()

    End Sub

    Private Sub clearBtn_Click(sender As Object, e As EventArgs) Handles clearBtn.Click
        ListView1.Items.Clear()
    End Sub

    Private Sub ListView1_MouseClick(sender As Object, e As MouseEventArgs) Handles ListView1.MouseClick
        MessageBox.Show(ListView1.SelectedItems(0).SubItems(0).Text)

    End Sub
End Class

Best Regards.

Comments