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: 11472
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Creating wxBitmapButtons dynamically and writing a function for each

Postby doublemax » Wed Sep 13, 2017 1:41 pm

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
Experienced Solver
Experienced Solver
Posts: 86
Joined: Tue Jul 26, 2016 2:00 pm

Re: Creating wxBitmapButtons dynamically and writing a function for each

Postby Wanderer82 » Wed Sep 13, 2017 1:49 pm

Oh, that was my next post, but now everything works fine. Thanks a lot!

ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 1996
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Creating wxBitmapButtons dynamically and writing a function for each

Postby ONEEYEMAN » Wed Sep 13, 2017 1:52 pm

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.


Return to “C++ Development”

Who is online

Users browsing this forum: Bing [Bot] and 1 guest