wxButton and identifier 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
YuMERA
Knows some wx things
Knows some wx things
Posts: 44
Joined: Fri Jul 05, 2019 8:47 pm

wxButton and identifier

Post by YuMERA »

How to get the value of wxButton label based on the Identifier or otherwise. I have a few buttons on wxDialog and I would like to bind the event by clicking on any button to display the label of that button.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7458
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: wxButton and identifier

Post by ONEEYEMAN »

Hi,
In order to do that you need to write:

Code: Select all

MyDialog::MyDialog()
{
auto *button1 = new wxButton(...);
auto *button2 = new wxButton(...);
button1->Bind( wxEVT_BUTTON, &MyDialog::ProcessClick, this );
button2->Bind( wxEVT_BUTTON, &MyDialog::ProcessClick, this );
}

void MyDialog::ProcessClick(wxCommandEvent &event)
{
    wxButton buttonClicked = dynamic_cast<wxButton *>( event.GetEventObject() );
    if( buttonClicked == button1 )
        wxMessageBox( "First button clicked" + buttonClicked->GetLabel() );
    else
        wxMessageBox( "Second button clicked" + buttonClicked->GetLabel() );
}
Thank you.
YuMERA
Knows some wx things
Knows some wx things
Posts: 44
Joined: Fri Jul 05, 2019 8:47 pm

Re: wxButton and identifier

Post by YuMERA »

Thx but for this

Code: Select all

wxButton buttonClicked = dynamic_cast<wxButton *>( event.GetEventObject() );
I have error
error: conversion from 'wxButton*' to non-scalar type 'wxButton' requested|
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4193
Joined: Sun Jan 03, 2010 5:45 pm

Re: wxButton and identifier

Post by PB »

The declaration of buttonClicked was missing the asterisk, it should be

Code: Select all

wxButton* buttonClicked = dynamic_cast<wxButton *>( event.GetEventObject() );
YuMERA
Knows some wx things
Knows some wx things
Posts: 44
Joined: Fri Jul 05, 2019 8:47 pm

Re: wxButton and identifier

Post by YuMERA »

Thank you all for your help. What I needed was this:

Code: Select all

wxButton* buttonClicked = dynamic_cast<wxButton *>( event.GetEventObject() );
tcPinDisplay->SetValue(buttonClicked->GetLabel());
and works.
Thx @ONEEYEMAN and @PB.
Post Reply