none
Listbox with fixed Item height does not show items in listbox? RRS feed

  • Question

  • Hallo, I have a listbox which is bound to bindingsource, with default settings listbox display all rows and newly added rows without any problem, but if i change Drawmode= OwnerDrawFixed  and Item Height = 15 then it does not display anything my listbox is empty, Why? Is there any other property which i must change in order to fix? Thanks. 
    Friday, July 26, 2019 6:34 AM

Answers

  • Hi,

    redraw:

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim str() As String = New String() {"1", "2", "3", "4"}
            ListBox1.DataSource = str
            ListBox1.ItemHeight = 15
            ListBox1.DrawMode = DrawMode.OwnerDrawFixed
            AddHandler ListBox1.DrawItem, AddressOf ListBox1_DrawItem
        End Sub
        Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As DrawItemEventArgs)
            e.Graphics.FillRectangle(New SolidBrush(e.BackColor), e.Bounds)
    
            If e.Index >= 0 Then
                Dim sStringFormat As StringFormat = New StringFormat()
                sStringFormat.LineAlignment = StringAlignment.Center
                e.Graphics.DrawString((CType(sender, ListBox)).Items(e.Index).ToString(), e.Font, New SolidBrush(e.ForeColor), e.Bounds, sStringFormat)
            End If
    
            e.DrawFocusRectangle()
        End Sub
    
    End Class
    
    
    

    https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.listbox.drawmode?redirectedfrom=MSDN&view=netframework-4.8

    Best Regards,

    Alex


    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.

    • Marked as answer by Shan1986 Friday, July 26, 2019 9:12 AM
    Friday, July 26, 2019 7:06 AM

All replies

  • Hi,

    redraw:

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim str() As String = New String() {"1", "2", "3", "4"}
            ListBox1.DataSource = str
            ListBox1.ItemHeight = 15
            ListBox1.DrawMode = DrawMode.OwnerDrawFixed
            AddHandler ListBox1.DrawItem, AddressOf ListBox1_DrawItem
        End Sub
        Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As DrawItemEventArgs)
            e.Graphics.FillRectangle(New SolidBrush(e.BackColor), e.Bounds)
    
            If e.Index >= 0 Then
                Dim sStringFormat As StringFormat = New StringFormat()
                sStringFormat.LineAlignment = StringAlignment.Center
                e.Graphics.DrawString((CType(sender, ListBox)).Items(e.Index).ToString(), e.Font, New SolidBrush(e.ForeColor), e.Bounds, sStringFormat)
            End If
    
            e.DrawFocusRectangle()
        End Sub
    
    End Class
    
    
    

    https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.listbox.drawmode?redirectedfrom=MSDN&view=netframework-4.8

    Best Regards,

    Alex


    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.

    • Marked as answer by Shan1986 Friday, July 26, 2019 9:12 AM
    Friday, July 26, 2019 7:06 AM
  • Hi,

    redraw:

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim str() As String = New String() {"1", "2", "3", "4"}
            ListBox1.DataSource = str
            ListBox1.ItemHeight = 15
            ListBox1.DrawMode = DrawMode.OwnerDrawFixed
            AddHandler ListBox1.DrawItem, AddressOf ListBox1_DrawItem
        End Sub
        Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As DrawItemEventArgs)
            e.Graphics.FillRectangle(New SolidBrush(e.BackColor), e.Bounds)
    
            If e.Index >= 0 Then
                Dim sStringFormat As StringFormat = New StringFormat()
                sStringFormat.LineAlignment = StringAlignment.Center
                e.Graphics.DrawString((CType(sender, ListBox)).Items(e.Index).ToString(), e.Font, New SolidBrush(e.ForeColor), e.Bounds, sStringFormat)
            End If
    
            e.DrawFocusRectangle()
        End Sub
    
    End Class
    
    

    https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.listbox.drawmode?redirectedfrom=MSDN&view=netframework-4.8

    Best Regards,

    Alex


    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.

    Thanks Alex. I dont understand why it is not simple like datagridview. 
    Friday, July 26, 2019 9:12 AM
  • Hi,

    redraw:

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim str() As String = New String() {"1", "2", "3", "4"}
            ListBox1.DataSource = str
            ListBox1.ItemHeight = 15
            ListBox1.DrawMode = DrawMode.OwnerDrawFixed
            AddHandler ListBox1.DrawItem, AddressOf ListBox1_DrawItem
        End Sub
        Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As DrawItemEventArgs)
            e.Graphics.FillRectangle(New SolidBrush(e.BackColor), e.Bounds)
    
            If e.Index >= 0 Then
                Dim sStringFormat As StringFormat = New StringFormat()
                sStringFormat.LineAlignment = StringAlignment.Center
                e.Graphics.DrawString((CType(sender, ListBox)).Items(e.Index).ToString(), e.Font, New SolidBrush(e.ForeColor), e.Bounds, sStringFormat)
            End If
    
            e.DrawFocusRectangle()
        End Sub
    
    End Class
    
    

    https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.listbox.drawmode?redirectedfrom=MSDN&view=netframework-4.8

    Best Regards,

    Alex


    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.

    Hallo Alex,

    I added you code but my list box items are shown as "System.Data.DataRowView " How do i fix it? At the beginning listbox is not populated. I add row to datatable then it shows up in listbox.

            With VT_lbx_eqlist
                .DisplayMember = dtName
                .ValueMember = dtEQID
                .DataSource = EQ_BS
                .DrawMode = DrawMode.OwnerDrawFixed
                .ItemHeight = 15
            End With
            AddHandler VT_lbx_eqlist.DrawItem, AddressOf VT_lbx_eqlist_DrawItem

    Thanks

    Friday, July 26, 2019 2:51 PM
  • Hallo Alex,

    I added you code but my list box items are shown as "System.Data.DataRowView " How do i fix it? At the beginning listbox is not populated. I add row to datatable then it shows up in listbox.

            With VT_lbx_eqlist
                .DisplayMember = dtName
                .ValueMember = dtEQID
                .DataSource = EQ_BS
                .DrawMode = DrawMode.OwnerDrawFixed
                .ItemHeight = 15
            End With
            AddHandler VT_lbx_eqlist.DrawItem, AddressOf VT_lbx_eqlist_DrawItem

    Thanks

    Friday, July 26, 2019 2:52 PM
  • Hallo Alex,

    I added you code but my list box items are shown as "System.Data.DataRowView " How do i fix it? At the beginning listbox is not populated. I add row to datatable then it shows up in listbox.

            With VT_lbx_eqlist
                .DisplayMember = dtName
                .ValueMember = dtEQID
                .DataSource = EQ_BS
                .DrawMode = DrawMode.OwnerDrawFixed
                .ItemHeight = 15
            End With
            AddHandler VT_lbx_eqlist.DrawItem, AddressOf VT_lbx_eqlist_DrawItem

    Thanks

    When this happens DisplayMember is incorrect, might want to check your spelling to whatever dtName.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, July 26, 2019 3:52 PM
    Moderator
  • Hallo Alex,

    I added you code but my list box items are shown as "System.Data.DataRowView " How do i fix it? At the beginning listbox is not populated. I add row to datatable then it shows up in listbox.

            With VT_lbx_eqlist
                .DisplayMember = dtName
                .ValueMember = dtEQID
                .DataSource = EQ_BS
                .DrawMode = DrawMode.OwnerDrawFixed
                .ItemHeight = 15
            End With
            AddHandler VT_lbx_eqlist.DrawItem, AddressOf VT_lbx_eqlist_DrawItem

    Thanks

    When this happens DisplayMember is incorrect, might want to check your spelling to whatever dtName.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Hallo Karen, it is correct . i dont know why it is not working.
    Friday, July 26, 2019 4:23 PM