OwnerDrawn Listbox data bound

B

Brian Henry

Here is my problem..

I have a owner drawn list box, I also have it data bound. I want to use the
display member as the text I draw in the box, and still return the value
member like a normal data bound list box. How do i get the display member
text to draw on the listbox? I have tried a few ways but i can't figure out
how to access that information to draw it. thanks!

I do know how to draw a normal list box, I have a few of them already, its
just data bound ones getting the display member text is my problem..
 
K

Ken Tucker [MVP]

Hi,

Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem

Dim g As Graphics = e.Graphics

Dim strOut As String

Dim r As RectangleF = RectangleF.op_Implicit(e.Bounds)

If TypeOf ListBox1.DataSource Is DataView Then

strOut = DirectCast(ListBox1.DataSource,
DataView).Item(e.Index).Item(ListBox1.DisplayMember).ToString

Else

strOut = DirectCast(ListBox1.DataSource,
DataTable).Rows(e.Index).Item(ListBox1.DisplayMember).ToString

End If

g.DrawString(strOut, ListBox1.Font, Brushes.Black, r)

End Sub



Ken
 
B

Brian Henry

thanks!


Ken Tucker said:
Hi,

Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem

Dim g As Graphics = e.Graphics

Dim strOut As String

Dim r As RectangleF = RectangleF.op_Implicit(e.Bounds)

If TypeOf ListBox1.DataSource Is DataView Then

strOut = DirectCast(ListBox1.DataSource,
DataView).Item(e.Index).Item(ListBox1.DisplayMember).ToString

Else

strOut = DirectCast(ListBox1.DataSource,
DataTable).Rows(e.Index).Item(ListBox1.DisplayMember).ToString

End If

g.DrawString(strOut, ListBox1.Font, Brushes.Black, r)

End Sub



Ken
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top