Click to See Complete Forum and Search --> : Listbox won't display anything in owner draw mode


Josef_Stalin
March 8th, 2010, 03:34 PM
I have a listbox which works perfectly fine in regular mode. I'm experimenting with the other modes which allow you to switch font.

As soon as I go to ownerdraw mode, either variable or fixed, it is blank when I try to add items to to it. It throws no error but just doesn't do anything. There has to be something else I have to do, to tell it how I want to display it. But I don't know what. I'm using version 2003 of .NET

I'm using "Items.add" to add items.

I'm using this line to set font : lbox.Font = New Font("Arial", 8.25, FontStyle.Bold, GraphicsUnit.Point, Nothing)

Then I'm setting it unbold later.

Why is it working perfectly fine without owner draw? Granted it isn't changing the font but it's working fine. What do I have to do to make it work otherwise, so that I can change the font?

It's pretty much a standard listbox, without many changes made to the properties.

dglienna
March 8th, 2010, 05:54 PM
Why use VS2003? There is 2005, 2008, and now, 2010 (still in beta, but available to everyone)

Josef_Stalin
March 8th, 2010, 06:17 PM
Compatibility reasons and I fiscal reasons.

dglienna
March 8th, 2010, 11:16 PM
There are FREE versions of ALL of them. The beta of VS2010 is the PRO version, and it's FREE as well

HanneSThEGreaT
March 9th, 2010, 07:21 AM
You need to use the ListBox's DrawItem event to draw the stuff :)

For example :
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListBox1.Items.Add("One")
ListBox1.Items.Add("Two")
ListBox1.Items.Add("Three")
End Sub

Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
Dim textFont As Font
textFont = New Font(e.Font.FontFamily, e.Font.Size * 2)

e.Graphics.DrawString(ListBox1.Items(e.Index).ToString(), _
textFont, _
New SolidBrush(e.ForeColor), _
e.Bounds.X, e.Bounds.Y)

e.DrawFocusRectangle()

End Sub

I agree that there are new versions of .NET available, but I also still have many 2003 programs, which will take an eternitity to port to newer versions ;)