How to derive wxVListBox? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Tapsa
Earned some good credits
Earned some good credits
Posts: 144
Joined: Tue Dec 06, 2011 5:52 pm
Location: Helsinki

How to derive wxVListBox?

Post by Tapsa » Sun Jul 05, 2015 8:43 pm

From the documentation I understand that I need to implement OnDrawItem and wxVListBox.
I have no clue how to implement OnDrawItem. How can I have it return strings, like OnGetItemText with wxListCtrl?
Do I need to specify my own colors for selection states as well?

I have a working derived class from wxListCtrl, but I think that wxVListBox might be better, since I want it to look like a basic wxListBox.

User avatar
doublemax
Moderator
Moderator
Posts: 15079
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to derive wxVListBox?

Post by doublemax » Sun Jul 05, 2015 9:23 pm

How can I have it return strings, like OnGetItemText with wxListCtrl?
With wxVListBox you don't return a string or anything, you're responsible for drawing the whole item.

Code: Select all

void MyVListBox::OnDrawItem(wxDC &dc, const wxRect &rect, size_t n) const
{
   wxString itemtext;
   itemtext.Printf("this is item #%d", n );
   dc.DrawLabel( itemtext, wxNullBitmap, rect, wxALIGN_LEFT|wxALIGN_CENTER_VERTICAL );
}

wxCoord MyVListBox::OnMeasureItem(size_t n) const
{
    return 32;
}
Use the source, Luke!

Tapsa
Earned some good credits
Earned some good credits
Posts: 144
Joined: Tue Dec 06, 2011 5:52 pm
Location: Helsinki

Re: How to derive wxVListBox?

Post by Tapsa » Mon Jul 06, 2015 8:35 am

Thanks a lot!
I tried to use DrawText, but it drew everything on the same spot.

User avatar
doublemax
Moderator
Moderator
Posts: 15079
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to derive wxVListBox?

Post by doublemax » Mon Jul 06, 2015 9:04 am

I tried to use DrawText, but it drew everything on the same spot.
DrawText would work too, but you have to use the coordinates passed in "rect". The y-coordinate raises for each item. DrawLabel has the advantage that it does the alignment calculations for you.
Use the source, Luke!

Post Reply