Creating wxBitmapButtons dynamically and writing a function for each

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.
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Creating wxBitmapButtons dynamically and writing a function for each

Post by doublemax »

Don't mix up IDs and array indices. It's a miracle that your program didn't crash ;)

Code: Select all

int button_id = 10000 + Durchlauf_fortlaufend;
wxBitmapButton *button = new wxBitmapButton(Panel1, button_id, wxBitmap(_T("C:\\Users\\Thomas\\Documents\\Programmprojekte\\Wall-Mat\\warenkorb.png")), wxDefaultPosition, wxDefaultSize, wxNO_BORDER, wxDefaultValidator, _T("this string doesn't matter"));

BitmapButton[Durchlauf_fortlaufend] = button;
FlexGridSizer2->Add(button, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
Connect(button_id, wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&Geometrie::OnBitmapButtonClick);
Sorry, i forgot the %d in here:

Code: Select all

void Geometrie::OnBitmapButtonClick(wxCommandEvent& event)
{
   wxLogMessage( "you clicked button with id=%d", (event.GetId()-10000));
}
Use the source, Luke!
Wanderer82
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 670
Joined: Tue Jul 26, 2016 2:00 pm

Re: Creating wxBitmapButtons dynamically and writing a function for each

Post by Wanderer82 »

Oh, that was my next post, but now everything works fine. Thanks a lot!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Creating wxBitmapButtons dynamically and writing a function for each

Post by ONEEYEMAN »

Hi,
It seems there is much easier solution. ;-)
Derive a class from wxBitmapButton. Override its OnClick event handler.
Use this newly created class in you program.

This way you won't be needing ID for your control and will just use wxID_ANY.

Thank you.
Post Reply