none
VB.NET image on subitem Listview RRS feed

  • Question

  • HI friends,

    I'm trying to add an image to subitem of a listview.

    Until now I did it in this way:
    on form1 load:

    ListView1.Columns(0).DisplayIndex = ListView1.Columns.Count - 1

    and on button click I added items to listview.

     Dim it As New ListViewItem("OK",ImageList1.Images.Count - 2)
                        it.SubItems.Add("hello")
                        ListView1.Items.Add(it)

    It work great.

    But Those days I had a nice idea to change the colours of listview.
    I changed colour of header column

    and also the colour of selected row on listview1.

    this is code I used to do that.

     Private Sub ListView1_DrawColumnHeader(sender As Object, e As DrawListViewColumnHeaderEventArgs) Handles ListView1.DrawColumnHeader
    
            Using headerFont = New Font("Microsoft segoe UI", 9, FontStyle.Regular)
                e.Graphics.FillRectangle(New SolidBrush(firstcolour), e.Bounds)
                e.Graphics.DrawString(e.Header.Text, headerFont, Brushes.White, e.Bounds)
            End Using
            Dim x As Integer = e.Bounds.Right - 1
            Dim y1 As Integer = e.Bounds.Top
            Dim y2 As Integer = e.Bounds.Bottom
            e.Graphics.DrawLine(Pens.White, x, y1, x, y2)
        End Sub
    
        Private Sub ListView1_DrawSubItem(sender As Object, e As DrawListViewSubItemEventArgs) Handles ListView1.DrawSubItem
    
            If (e.Item.Focused = True) Then
                Using headerFont = New Font("Microsoft segoe UI", 9, FontStyle.Regular)
                    e.Graphics.FillRectangle(New SolidBrush(secondcolour), e.Bounds)
                    e.Graphics.DrawString(e.SubItem.Text, headerFont, Brushes.White, e.Bounds)
                End Using
    
            Else
                Using headerFont = New Font("Microsoft segoe UI", 9, FontStyle.Regular)
                    e.Graphics.FillRectangle(New SolidBrush(Color.Transparent), e.Bounds)
                    e.Graphics.DrawString(e.SubItem.Text, headerFont, Brushes.White, e.Bounds)
                    ' e.DrawDefault = True
                End Using
    
            End If
    
        End Sub
    
        Private Sub ListView1_DrawItem(sender As Object, e As DrawListViewItemEventArgs) Handles ListView1.DrawItem
    
            If Not (e.State And ListViewItemStates.Selected) = 0 Then
                Using headerfont = New Font("microsoft segoe ui", 9, FontStyle.Regular)
                    e.Graphics.FillRectangle(New SolidBrush(secondcolour), e.Bounds)
                    e.Graphics.DrawString(e.Item.Text, headerfont, Brushes.White, e.Bounds)
                End Using
    
            Else
                Using headerfont = New Font("microsoft segoe ui", 9, FontStyle.Regular)
                    e.Graphics.FillRectangle(New SolidBrush(Color.Transparent), e.Bounds)
                    e.Graphics.DrawString(e.Item.Text, headerfont, Brushes.White, e.Bounds)
                    'e.DrawDefault = True
                End Using
    
            End If
        End Sub
    where firstcolour and secondcolour are delcared before on form and there is not a problem with colours.

    Now I have 2 problems.

     first problem If I change at load the  display index of listview, when I add rows to listview the text is not ok, it overload the column 1

    and 2 problem image is not added or is maybe is not visible because of drawing mode.

    If  I don't change the index order on load form text of items and subitems is ok but not the image that does not appear.

    How can resolve this problem? any help?

    Thanks

    Tuesday, July 25, 2017 12:38 PM

Answers

  • Thanks for your help CHerry,

    is ok your replay but not for my problem.

    I solved in other way, drawing triangle first for column index = 0 and 1 then for last column.

    Please close thread. SOLVED and thanks to all

    Wednesday, July 26, 2017 9:36 AM

All replies

  • Friends,

    I solved it in this way.

    first I changed the order of my column so I will not use on form load to change index because I will add image now not directely imagelist but throught drawing mode so the image will go on column 2 of my listview.

    this is code I used:

    Private Sub ListView1_DrawSubItem(sender As Object, e As DrawListViewSubItemEventArgs) Handles ListView1.DrawSubItem 'code added If e.ColumnIndex = 2 Then If e.Item.Text = "ok" Then Dim im As Image = ImageList1.Images(0) Dim location As Point = Me.ListView1.Items(e.ItemIndex).SubItems(e.ColumnIndex).Bounds.Location e.Graphics.DrawImage(im, location) end if

    end if end sub

    Remain now only a little problem Image and text are to near each other. how can I add a space between image and text "ok" ?

    thank you to all

    Tuesday, July 25, 2017 1:39 PM
  • Hi PaoloDeBeneditis,

    According to your description, you want to add a space between image and text "ok", you just need to modify your code like this.

    Dim it As New ListViewItem("   " + "OK", ImageList1.Images.Count - 2)
            it.SubItems.Add("hello")
            ListView1.Items.Add(it)

    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.

    Wednesday, July 26, 2017 3:08 AM
    Moderator
  • Thanks for your help CHerry,

    is ok your replay but not for my problem.

    I solved in other way, drawing triangle first for column index = 0 and 1 then for last column.

    Please close thread. SOLVED and thanks to all

    Wednesday, July 26, 2017 9:36 AM