none
How can i Add Icons to listview subitmes? RRS feed

  • Question

  • Good evening

    i have this listview1 above

    i want to add icons in Number Column 

    so how can i do that?

    i want whenever i enter any number through textbox the number column will add the same icon to all numbers

    can you advice?

    Tuesday, May 8, 2018 5:26 PM

All replies

  • The easiest thing is to associate the ListView.SmallImageList with an ImageList and then set the ImageKey or ImageIndex for each ListViewItem.  This will place the icon before the first column.

    If that is not acceptable then you will have to get into custom drawing the ListViewItems which is more complex.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, May 8, 2018 5:34 PM
    Moderator
  • Good evening

    i have this listview1 above

    i want to add icons in Number Column 

    so how can i do that?

    i want whenever i enter any number through textbox the number column will add the same icon to all numbers

    can you advice?

    Hi

    Here is a simple Listview example that shows Images in a column. The image paths are set for my system, if you want to try this example you would need to adjust those paths for your setup.

    ' LISTVIEW EXAMPLE
    ' COPY ITEMS FROM LV1 to LV2
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim listView1, listView2 As New ListView()
      Dim b As New Button
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CreateMyListView()
    
        'no checkboxes
        With listView2
    	  .Columns.Add("Item Column", -2, HorizontalAlignment.Left)
    	  .Columns.Add("Column 2", -2, HorizontalAlignment.Left)
    	  .Columns.Add("Column 3", -2, HorizontalAlignment.Left)
    	  .Columns.Add("Column 4", -2, HorizontalAlignment.Center)
    
    	  .Bounds = New Rectangle(New Point(330, 50), New Size(300, 200))
    	  .View = View.Details
    	  .Size = New Size(400, 200)
    	End With
    	With b
    	  .Text = "Copy"
    	  .Location = New Point(10, 4)
    	  .AutoSize = True
    	End With
    	Controls.AddRange({listView1, listView2, b})
    	AddHandler b.Click, AddressOf B_Click
      End Sub
    
      Private Sub B_Click(sender As Object, e As EventArgs)
    	listView2.Items.Clear()
    	For Each i As ListViewItem In listView1.Items
          If i.Checked Then
            listView2.Items.Add(CType(i.Clone, ListViewItem))
          End If
        Next
      End Sub
    
      Private Sub CreateMyListView()
        ' Create new ListView.
        listView1.Bounds = New Rectangle(New Point(10, 50), New Size(300, 200))
    
        ' Set to details.
        listView1.View = View.Details
        ' Allow item edit.
        listView1.LabelEdit = True
        ' Allow rearrange columns.
        listView1.AllowColumnReorder = True
        ' check boxes.
        listView1.CheckBoxes = True
        ' Select whole row.
        listView1.FullRowSelect = True
        ' Show grid lines.
        listView1.GridLines = True
        ' Sort ascending.
        listView1.Sorting = SortOrder.Ascending
    
        ' make some test data
        Dim item1 As New ListViewItem("item1", 0)
        item1.Checked = True
        item1.SubItems.Add("111")
        item1.SubItems.Add("222")
        item1.SubItems.Add("333")
        Dim item2 As New ListViewItem("item2", 1)
        item2.SubItems.Add("4444")
        item2.SubItems.Add("5555")
        item2.SubItems.Add("6666")
        Dim item3 As New ListViewItem("item3", 0)
        item3.Checked = True
        item3.SubItems.Add("77")
        item3.SubItems.Add("88")
        item3.SubItems.Add("99")
    
        ' Columns for items and subitems.
        ' Width of -2 for auto-size.
        listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left)
        listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left)
        listView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left)
        listView1.Columns.Add("Column 4", -2, HorizontalAlignment.Center)
    
        'Add some items to the ListView.
        listView1.Items.AddRange(New ListViewItem() {item1, item2, item3})
    
        ' Create two ImageList objects.
        Dim imageListSmall As New ImageList()
        Dim imageListLarge As New ImageList()
    
        ' Initialize ImageList
        imageListSmall.Images.Add(Image.FromFile(Application.StartupPath & "\Images\42small.png"))
        imageListSmall.Images.Add(Image.FromFile(Application.StartupPath & "\Images\43small.png"))
        imageListLarge.Images.Add(Image.FromFile(Application.StartupPath & "\Images\42.png"))
        imageListLarge.Images.Add(Image.FromFile(Application.StartupPath & "\Images\43.png"))
    
        'Assign ImageList to ListView.
        listView1.LargeImageList = imageListLarge
        listView1.SmallImageList = imageListSmall
    
        listView2.LargeImageList = imageListLarge
        listView2.SmallImageList = imageListSmall
    
      End Sub
    End Class
    


    Regards Les, Livingston, Scotland

    Tuesday, May 8, 2018 5:54 PM
  • Good evening

    i have this listview1 above

    i want to add icons in Number Column 

    so how can i do that?

    i want whenever i enter any number through textbox the number column will add the same icon to all numbers

    can you advice?

    Hi

    Here is a simple Listview example that shows Images in a column. The image paths are set for my system, if you want to try this example you would need to adjust those paths for your setup.

    ' LISTVIEW EXAMPLE
    ' COPY ITEMS FROM LV1 to LV2
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim listView1, listView2 As New ListView()
      Dim b As New Button
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CreateMyListView()
    
        'no checkboxes
        With listView2
    	  .Columns.Add("Item Column", -2, HorizontalAlignment.Left)
    	  .Columns.Add("Column 2", -2, HorizontalAlignment.Left)
    	  .Columns.Add("Column 3", -2, HorizontalAlignment.Left)
    	  .Columns.Add("Column 4", -2, HorizontalAlignment.Center)
    
    	  .Bounds = New Rectangle(New Point(330, 50), New Size(300, 200))
    	  .View = View.Details
    	  .Size = New Size(400, 200)
    	End With
    	With b
    	  .Text = "Copy"
    	  .Location = New Point(10, 4)
    	  .AutoSize = True
    	End With
    	Controls.AddRange({listView1, listView2, b})
    	AddHandler b.Click, AddressOf B_Click
      End Sub
    
      Private Sub B_Click(sender As Object, e As EventArgs)
    	listView2.Items.Clear()
    	For Each i As ListViewItem In listView1.Items
          If i.Checked Then
            listView2.Items.Add(CType(i.Clone, ListViewItem))
          End If
        Next
      End Sub
    
      Private Sub CreateMyListView()
        ' Create new ListView.
        listView1.Bounds = New Rectangle(New Point(10, 50), New Size(300, 200))
    
        ' Set to details.
        listView1.View = View.Details
        ' Allow item edit.
        listView1.LabelEdit = True
        ' Allow rearrange columns.
        listView1.AllowColumnReorder = True
        ' check boxes.
        listView1.CheckBoxes = True
        ' Select whole row.
        listView1.FullRowSelect = True
        ' Show grid lines.
        listView1.GridLines = True
        ' Sort ascending.
        listView1.Sorting = SortOrder.Ascending
    
        ' make some test data
        Dim item1 As New ListViewItem("item1", 0)
        item1.Checked = True
        item1.SubItems.Add("111")
        item1.SubItems.Add("222")
        item1.SubItems.Add("333")
        Dim item2 As New ListViewItem("item2", 1)
        item2.SubItems.Add("4444")
        item2.SubItems.Add("5555")
        item2.SubItems.Add("6666")
        Dim item3 As New ListViewItem("item3", 0)
        item3.Checked = True
        item3.SubItems.Add("77")
        item3.SubItems.Add("88")
        item3.SubItems.Add("99")
    
        ' Columns for items and subitems.
        ' Width of -2 for auto-size.
        listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left)
        listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left)
        listView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left)
        listView1.Columns.Add("Column 4", -2, HorizontalAlignment.Center)
    
        'Add some items to the ListView.
        listView1.Items.AddRange(New ListViewItem() {item1, item2, item3})
    
        ' Create two ImageList objects.
        Dim imageListSmall As New ImageList()
        Dim imageListLarge As New ImageList()
    
        ' Initialize ImageList
        imageListSmall.Images.Add(Image.FromFile(Application.StartupPath & "\Images\42small.png"))
        imageListSmall.Images.Add(Image.FromFile(Application.StartupPath & "\Images\43small.png"))
        imageListLarge.Images.Add(Image.FromFile(Application.StartupPath & "\Images\42.png"))
        imageListLarge.Images.Add(Image.FromFile(Application.StartupPath & "\Images\43.png"))
    
        'Assign ImageList to ListView.
        listView1.LargeImageList = imageListLarge
        listView1.SmallImageList = imageListSmall
    
        listView2.LargeImageList = imageListLarge
        listView2.SmallImageList = imageListSmall
    
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    thanks for your quick responce

    where should i paste these codes?

    in form1 load?

    or in viewlist ?

    one more thing

    lets say my picture patch is in desktop

    and the icon name is mmm

    what should i change in your path?

    Tuesday, May 8, 2018 7:42 PM
  • Hi

    If you are using Windows 10, you can easily get a path string by right click on the file and choosing 'Copy as Path'


    Regards Les, Livingston, Scotland

    Tuesday, May 8, 2018 8:10 PM
  • Most of that code is unnecessary if you configure the ImageList and ListView in the designer.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, May 8, 2018 8:12 PM
    Moderator
  • Most of that code is unnecessary if you configure the ImageList and ListView in the designer.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    when i paste the code i got 102 error msg in my form code

    by the way i tried to insert image list. then i click on the listview and choosed  small image list

    but it insert the image on the column header ( which i dont want ) . i need it to be in subitem and whenever i insert a new data it will appear

    you got my point?

    Tuesday, May 8, 2018 8:15 PM
  • Most of that code is unnecessary if you configure the ImageList and ListView in the designer.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    when i paste the code i got 102 error msg in my form code

    by the way i tried to insert image list. then i click on the listview and choosed  small image list

    but it insert the image on the column header ( which i dont want ) . i need it to be in subitem and whenever i insert a new data it will appear

    you got my point?

    As I said in my original reply, that is what would happen and if you need something more complex then you will have to use custom drawing code within the DrawItem and/or DrawSubItem event handlers.  This is much more complex than just accepting the image at the first column using the default behavior.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Tuesday, May 8, 2018 8:26 PM
    Moderator
  • Imports System.Net
    Imports System.Net.Mail
    
    
    Public Class Form1
    
    
    
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            OpenFileDialog1.ShowDialog()
            Label18.Visible = True
            Label18.Text = OpenFileDialog1.FileName
    
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ListView1.AllowColumnReorder = True
    
    
    
    
        End Sub
    
        Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged
            Label34.Text = TextBox5.Text & " " & ComboBox1.Text
    
    
            If TextBox5.Text = "1" Then
                ComboBox1.Text = "RD"
                RadioButton1.Checked = True
            End If
            If TextBox5.Text = "5"
                ComboBox1.Text = "Account Close"
                RadioButton2.Checked = True
                Label34.Text = TextBox5.Text & " " & ComboBox1.Text
    
    
    
    
            End If
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
            Label34.Text = TextBox5.Text & " " & ComboBox1.Text
    
            If ComboBox1.Text = "RD" Then
                TextBox5.Text = "1"
                RadioButton1.Checked = True
            Else
                ComboBox1.Text = "Closed"
                TextBox5.Text = "5"
                RadioButton2.Checked = True
    
    
    
    
            End If
    
        End Sub
    
        Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
            If Len(TextBox2.Text) = 4 Then TextBox10.Focus()
        End Sub
    
        Private Sub TextBox10_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox10.TextChanged
            If Len(TextBox10.Text) = 4 Then TextBox11.Focus()
    
        End Sub
    
        Private Sub TextBox11_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox11.TextChanged
            If Len(TextBox11.Text) = 4 Then TextBox12.Focus()
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim testMessage As New MailMessage
            Dim fromAddress As New MailAddress(TextBox100.Text)
            Dim objSmtpClient As New SmtpClient("smtp.gmail.com", 587) 'Try Port 25 or port 465 if 587 doesn't work. Each port and server is unique to the email server.
            objSmtpClient.UseDefaultCredentials = False
            objSmtpClient.Credentials = New NetworkCredential(TextBox100.Text, MaskedTextBox1.Text)
            objSmtpClient.EnableSsl = True
    
            testMessage.From = fromAddress 'Gmail overwrites this I think.
            testMessage.To.Add(TextBox200.Text)
            testMessage.Subject = TextBox300.Text
            testMessage.Priority = MailPriority.Normal
            testMessage.Body = RichTextBox1.Text
            testMessage.IsBodyHtml = False
    
            objSmtpClient.Send(testMessage)
    
        End Sub
    
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            For i = 0 To ListView1.Items.Count - 1
                If ListView1.Items(i).Text = txtfind.Text Then
                    ListView1.Items(i).Selected = True
    
    
    
    
                Else
                    ListView1.Items(i).Selected = False
                End If
            Next
            ListView1.Focus()
    
        End Sub
    
        Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
            For i = 1 To ListView1.SelectedItems.Count
    
            Next
    
            ListView1.Items.Remove(ListView1.SelectedItems(0))
    
        End Sub
    
        Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    
    
        End Sub
    
        Private Sub DfToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    
    
        End Sub
    
    
        Private Sub TextBox9_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    
    
        End Sub
    
        Private Sub TextBox12_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox12.TextChanged
            TextBox8.Text = (TextBox2.Text & TextBox10.Text & TextBox11.Text & TextBox12.Text)
    
        End Sub
    
        Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
            Dim add As New ListViewItem(TextBox1.Text$)
            add.SubItems.Add(DateTimePicker1.Text$)
            add.SubItems.Add(TextBox3.Text$)
            add.SubItems.Add(TextBox13.Text$)
            add.SubItems.Add(TextBox8.Text$)
    
    
            ListView3.Items.Add(add)
    
            TextBox1.Text = " "
            TextBox3.Text = " "
            TextBox13.Text = " "
            TextBox2.Text = " "
            TextBox10.Text = " "
            TextBox11.Text = " "
            TextBox12.Text = " "
            TextBox4.Text = " "
    
        End Sub
    
        Private Sub TextBox7_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    
    
    
        End Sub
    
        Private Sub DateTimePicker4_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker4.ValueChanged
            Label45.Visible = True
        End Sub
    
        Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
            If RadioButton3.Checked = True Then
                DateTimePicker4.Enabled = False
    
                Label33.Text = "No"
                Label45.Visible = False
    
            Else
                DateTimePicker4.Enabled = True
    
                Label33.Text = "Yes"
            End If
    
        End Sub
    
        Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged
    
        End Sub
    
        Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
    
            With Me.ListView1
    
            End With
            Dim i As Integer
            For Each item As ListViewItem In ListView1.SelectedItems
                i = item.Index
            Next
    
            Dim innercounter As Integer = 0
            For Each subItem As ListViewItem.ListViewSubItem In ListView1.Items(i).SubItems
                Dim myString As String = ListView1.Items(i).SubItems(innercounter).Text
                Select Case innercounter
                    Case 0
                        Label9.Text = myString
    
                    Case 1
                        Label16.Text = myString
                    Case 2
                        Label24.Text = myString
                    Case 3
                        Label25.Text = myString
                    Case 4
                        Label26.Text = myString
                    Case 5
                        Label35.Text = myString
                    Case 6
                        Label36.Text = myString
                    Case 7
                        Label37.Text = myString
                    Case 8
                        Label38.Text = myString
                    Case 9
                        Label39.Text = myString
                    Case 10
                        Label40.Text = myString
                    Case 11
                        Label41.Text = myString
                    Case 12
                        Label42.Text = myString
                    Case 13
                        Label43.Text = myString
    
    
                End Select
                innercounter += 1
            Next
    
    
    
    
        End Sub
    
        Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
            Dim Items(ListView3.Items.Count - 1) As ListViewItem
            For i As Integer = 0 To ListView3.Items.Count - 1
                Items(i) = CType(ListView3.Items(i).Clone, ListViewItem)
            Next
            ListView2.Items.AddRange(Items)
            ListView3.Items.Clear()
    
    
    
    
    
        End Sub
    
        Private Sub ListView2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView2.SelectedIndexChanged
    
    
            With Me.ListView2
    
            End With
            Dim i As Integer
            For Each item As ListViewItem In ListView2.SelectedItems
                i = item.Index
            Next
    
            Dim innercounter As Integer = 0
            For Each subItem As ListViewItem.ListViewSubItem In ListView2.Items(i).SubItems
                Dim myString As String = ListView2.Items(i).SubItems(innercounter).Text
                Select Case innercounter
                    Case 0
                        Label28.Text = myString
    
                    Case 1
                        Label27.Text = myString
                    Case 2
                        Label29.Text = myString
                    Case 3
                        Label30.Text = myString
                    Case 4
                        Label31.Text = myString
    
    
                End Select
                innercounter += 1
            Next
    
    
        End Sub
    
        Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
            If RadioButton2.Checked = True Then
                Label32.Text = "No"
            Else
                Label32.Text = "Yes"
    
            End If
        End Sub
    
        Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
            Dim add As New ListViewItem(Label28.Text$)
            add.SubItems.Add(Label27.Text$)
            add.SubItems.Add(Label29.Text$)
            add.SubItems.Add(Label30.Text$)
            add.SubItems.Add(Label31.Text$)
            add.SubItems.Add(TextBox5.Text & ComboBox1.Text$)
            add.SubItems.Add(Label32.Text$)
            add.SubItems.Add(DateTimePicker3.Text$)
            add.SubItems.Add(Label33.Text$)
            add.SubItems.Add(DateTimePicker4.Text$)
            add.SubItems.Add(Label45.Text$)
            add.SubItems.Add(DateTimePicker2.Text$)
            add.SubItems.Add(Label22.Text$)
            add.SubItems.Add(Label18.Text$)
    
            ListView1.Items.Add(add)
            ComboBox1.Text = " "
            TextBox5.Text = " "
    
        End Sub
    
        Private Sub Label28_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label28.Click
    
        End Sub
    
        Private Sub RadioButton4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton4.CheckedChanged
            If RadioButton4.Checked = True Then
    
    
                Label22.Enabled = False
    
            Else
                Label22.Enabled = True
            End If
    
    
        End Sub
    
        Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
            If CheckBox1.Checked = True Then
    
                Label44.Visible = False
    
    
            Else
                Label44.Visible = True
    
    
            End If
        End Sub
    
        Private Sub DateTimePicker2_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker2.ValueChanged
            Label22.Visible = True
    
        End Sub
    
        Private Sub Label45_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label45.Click
    
        End Sub
    
        Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    
    
        End Sub
    
        Private Sub Button5_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs)
    
        End Sub
    
        Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
            For Each item As ListViewItem In ListView3.SelectedItems
                ListView2.Items.Add(item.Clone())
                For i = ListView3.SelectedItems.Count - 1 To 0 Step -1
                    ListView3.Items.Remove(ListView3.SelectedItems(i))
    
    
                Next
    
            Next
        End Sub
    
        Private Sub txtfind_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtfind.TextChanged
    
        End Sub
    
        Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
            Dim hala123 As ListViewItem
            hala123 = ListView1.SelectedItems(0)
            TextBox7.Text = hala123.Text
            TextBox9.Text = hala123.SubItems(1).Text
            TextBox14.Text = hala123.SubItems(2).Text
            TextBox15.Text = hala123.SubItems(3).Text
            TextBox17.Text = hala123.SubItems(5).Text
            DateTimePicker3.Text = hala123.SubItems(7).Text
            DateTimePicker4.Text = hala123.SubItems(9).Text
            DateTimePicker2.Text = hala123.SubItems(11).Text
            Label33.Text = hala123.SubItems(8).Text
            If Label33.Text = "No" Then
                RadioButton3.Checked = True
            Else
                RadioButton4.Checked = True
    
            End If
    
    
    
        End Sub
    
        Private Sub TextBox17_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox17.TextChanged
         
    
                If TextBox17.Text = "5Account Close" Then
                    TextBox5.Text = 5
            Else : TextBox5.Text = 1
    
                End If
        End Sub
    
        Private Sub Label46_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label46.Click
            If Label46.Text = "Yes" Then
                RadioButton4.Checked = True
    
            End If
        End Sub
    
        Private Sub Label33_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label33.Click
            
        End Sub
    
        Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
            Dim hala123 As ListViewItem
            hala123 = ListView1.SelectedItems(0)
    
    
            TextBox8.Text = hala123.SubItems(4).Text
            TextBox3.Text = hala123.SubItems(2).Text
            TextBox1.Text = hala123.Text
            TextBox13.Text = hala123.SubItems(3).Text
            DateTimePicker1.Text = hala123.SubItems(1).Text
    
    
        End Sub
    
        Private Sub DateTimePicker3_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker3.ValueChanged
            Label44.Visible = True
    
        End Sub
    
        Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged
    
        End Sub
    
        Private Sub TabPage1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage1.Click
    
        End Sub
    
        Private Sub TextBox8_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox8.TextChanged
            TextBox2.Text = TextBox8.Text
    
    
    
    
        End Sub
    
        Private Sub RectangleShape1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RectangleShape1.Click
            MsgBox("hi")
        End Sub
    
        Private Sub Label60_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label60.Click
            MsgBox("hi")
    
        End Sub
    
        Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
            MsgBox("hi")
        End Sub
    End Class
    
    

    what is above is my form1 currently and i already have 3 listview

    how my form will be if i want to add your code?  please help because actually im little confuse 

    Tuesday, May 8, 2018 9:26 PM
  • I believe the Item is 9 and to the right of it are sub items. I can not find a method for adding Icons to sub items. However I can provide code that can display an Icon for an Item but not sub items of an item.

    You can see the Icons displayed for the items in the image.

    As to doing this when some TextBox does something I have no idea how you do that so I have no idea what events you use. Therefore you will have to determine what event is used to update the Icons for an Item.

    Option Strict On
    
    Public Class Form1
    
        Dim imageListSmall As New ImageList()
        Dim imageListLarge As New ImageList()
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
    
            Dim ImgFile = IO.Directory.GetFiles(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test Icon"))
            imageListSmall.Images.Add(Image.FromFile(ImgFile(0)))
            imageListLarge.Images.Add(Image.FromFile(ImgFile(0)))
    
            ListView1.FullRowSelect = True
            ListView1.MultiSelect = True
            ListView1.GridLines = True
            ListView1.LabelEdit = True
            ListView1.UseCompatibleStateImageBehavior = False
            ListView1.View = View.Details
            ListView1.AllowColumnReorder = True
            ListView1.GridLines = True
            ListView1.Sorting = SortOrder.Ascending
            ListView1.FullRowSelect = False
            ListView1.MultiSelect = True
    
            ListView1.Sorting = SortOrder.Ascending
            ListView1.Columns.Add("Information area 1", 200, HorizontalAlignment.Left)
            ListView1.Columns.Add("Information area 2", 200, HorizontalAlignment.Left)
            ListView1.Columns.Add("Hot Keys", 200, HorizontalAlignment.Left)
            ListView1.Columns.Add("Information area 4", 200, HorizontalAlignment.Left)
            ListView1.Columns.Add("Information area 5", 200, HorizontalAlignment.Left)
    
            Dim item1 As New ListViewItem("Sub Information 1")
            item1.SubItems.Add("Sub Information 1 - 0")
            item1.SubItems.Add("Hot Key 1")
            item1.SubItems.Add("Sub Information 1 - 2")
            item1.SubItems.Add("Sub Information 1 - 3")
            ListView1.Items.AddRange(New ListViewItem() {item1})
    
            Dim item2 As New ListViewItem("Sub Information 2")
            item2.SubItems.Add("Sub Information 2 - 0")
            item2.SubItems.Add("Hot Key 2")
            item2.SubItems.Add("Sub Information 2 - 2")
            item2.SubItems.Add("Sub Information 2 - 3")
            ListView1.Items.AddRange(New ListViewItem() {item2})
    
            Dim item3 As New ListViewItem("Sub Information 3")
            item3.SubItems.Add("Sub Information 3 - 0")
            item3.SubItems.Add("Hot Key 3")
            item3.SubItems.Add("Sub Information 3 - 2")
            item3.SubItems.Add("Sub Information 3 - 3")
            ListView1.Items.AddRange(New ListViewItem() {item3})
    
            ListView1.SmallImageList = imageListSmall
            ListView1.LargeImageList = imageListLarge
    
            For i = 0 To ListView1.Items.Count - 1
                Dim lv As ListViewItem = ListView1.Items(i)
                lv.ImageIndex = 0
            Next
        End Sub
    
    End Class
    


    La vida loca

    Wednesday, May 9, 2018 12:05 AM
  •  Normally the first column which you are pointing out above is the ListViewItem and the items in the columns off to the right of that are the SubItems of it.  So,  if you only want images in the first column,  just add your images to an ImageList as Reed and set the SmallImageList property of the ListView to that ImageList mentioned.  You can set the Index or Key for the image when you add it to the ImageList and just use the Index or Key to specify which image to use for which ListViewItem.

     

     However,  if you really want to show images in theListViewItem's SubItems,  you can take a look at the two links below where I have posted a few examples of drawing images in the SubItems.  That requires handling painting the SubItems yourself.

    how to draw image to subitem in listview in VB ?

    VB.Net Display Image In Listview Subitems With Details Setting...


    If you say it can`t be done then i`ll try it

    • Proposed as answer by Mr. Monkeyboy Thursday, May 10, 2018 3:32 AM
    Wednesday, May 9, 2018 12:49 AM
  • Good evening

    i have this listview1 above

    i want to add icons in Number Column 

    so how can i do that?

    i want whenever i enter any number through textbox the number column will add the same icon to all numbers

    can you advice?

    Hi anglefbi,

    I put one ImageList and ListView in the Form, and left-click imageList control to add some image or icon into ImageList in ImageList's images property.

    Then you can take a look the following, you can put these code on your Form_load event.

      Private Sub FrmListView_Load(sender As Object, e As EventArgs) Handles MyBase.Load
           
            ListView2.SmallImageList = ImageList1
            ListView2.FullRowSelect = True
            ListView2.GridLines = True
            ListView2.View = View.Details
            ListView2.Columns.Add("number", 70)
            ListView2.Columns.Add("Name", 70)
            ListView2.Columns.Add("ID", 70)
    
            For I As Integer = 0 To 15
                Dim lvi As ListViewItem = New ListViewItem(New String() {"aaaaa", "bbbb", "cccc"})
                lvi.ImageIndex = 0
                ListView2.Items.Add(lvi)
            Next
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Stanly Fan Thursday, May 10, 2018 9:30 AM
    Wednesday, May 9, 2018 2:35 AM
    Moderator
  • Good evening

    i have this listview1 above

    i want to add icons in Number Column 

    so how can i do that?

    i want whenever i enter any number through textbox the number column will add the same icon to all numbers

    can you advice?

    Hi anglefbi,

    I put one ImageList and ListView in the Form, and left-click imageList control to add some image or icon into ImageList in ImageList's images property.

    Then you can take a look the following, you can put these code on your Form_load event.

      Private Sub FrmListView_Load(sender As Object, e As EventArgs) Handles MyBase.Load
           
            ListView2.SmallImageList = ImageList1
            ListView2.FullRowSelect = True
            ListView2.GridLines = True
            ListView2.View = View.Details
            ListView2.Columns.Add("number", 70)
            ListView2.Columns.Add("Name", 70)
            ListView2.Columns.Add("ID", 70)
    
            For I As Integer = 0 To 15
                Dim lvi As ListViewItem = New ListViewItem(New String() {"aaaaa", "bbbb", "cccc"})
                lvi.ImageIndex = 0
                ListView2.Items.Add(lvi)
            Next
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    How many times does the OP need this same example after they say it is not what they want?

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, May 9, 2018 11:15 AM
    Moderator
  • Here is an example of what you are actually trying to do.  Note that there is much to take into consideration to duplicate the default functionality and you may need to make some adjustments to suit your tastes.

    Also note that this requires that the OwnerDraw property of the ListView be set to True.

    Public Class Form1
        'store images for each number column value
        Private ItemImages As New Dictionary(Of String, Image)
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'load images for each number column value
            ' replace SystemIcons with your images loaded with Bitmap.FromFile() or using embedded resources or imagelist items
            ItemImages("9") = SystemIcons.Application.ToBitmap
            ItemImages("10") = SystemIcons.Error.ToBitmap
            ItemImages("11") = SystemIcons.Exclamation.ToBitmap
        End Sub
    
        Private Sub ListView1_DrawColumnHeader(sender As Object, e As DrawListViewColumnHeaderEventArgs) Handles ListView1.DrawColumnHeader
            'perform default drawing of column headers
            e.DrawDefault = True
        End Sub
    
        Private Sub ListView1_DrawSubItem(sender As Object, e As DrawListViewSubItemEventArgs) Handles ListView1.DrawSubItem
            'get the bounds of the text
            Dim textSize = TextRenderer.MeasureText(e.SubItem.Text, e.SubItem.Font)
            Dim textBounds = New Rectangle(e.Bounds.Left, e.Bounds.Top, textSize.Width, textSize.Height)
    
            'handle the column that will have the image
            If e.ColumnIndex = 0 Then
                If e.Item.Selected Then
                    'draw text in selected state if applicable
                    Using brsh As New SolidBrush(SystemColors.Highlight)
                        e.Graphics.FillRectangle(brsh, textBounds)
                    End Using
                    Using brsh As New SolidBrush(SystemColors.HighlightText)
                        e.Graphics.DrawString(e.SubItem.Text, e.SubItem.Font, brsh, textBounds)
                    End Using
                Else
                    'otherwise draw text with listitem settings
                    e.DrawBackground()
                    Using brsh As New SolidBrush(e.Item.ForeColor)
                        e.Graphics.DrawString(e.SubItem.Text, e.SubItem.Font, brsh, textBounds)
                    End Using
                End If
                'draw the image if it exists for the text
                If ItemImages.ContainsKey(e.SubItem.Text) Then
                    Dim img = ItemImages(e.SubItem.Text)
                    Dim dst = New Rectangle(e.Bounds.Width - img.Width * (e.Bounds.Height / img.Width),
                                            e.Bounds.Top, e.Bounds.Height, e.Bounds.Height)
                    e.Graphics.DrawImage(img, dst, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel)
                End If
                'draw the focus rectangle if applicable
                If e.Item.Focused Then e.DrawFocusRectangle(textBounds)
            Else
                'draw all other sub items
                e.DrawBackground()
                Using brsh As New SolidBrush(e.Item.ForeColor)
                    e.Graphics.DrawString(e.SubItem.Text, e.SubItem.Font, brsh, textBounds)
                End Using
            End If
        End Sub
    End Class


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"



    Wednesday, May 9, 2018 12:09 PM
    Moderator