wxbutton

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
gorf25
Earned a small fee
Earned a small fee
Posts: 15
Joined: Mon Aug 21, 2017 3:10 pm

wxbutton

Post by gorf25 »

Checking to see is there is a way to change a button state, if the button has been setup for cancel
and you clicked a different button to a function can you change the cancel button from cancel to call a new function for example like print button and then when done chance it back to a cancel button ?

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

Re: wxbutton

Post by doublemax »

You can change the text of a button with SetLabel() and what you do inside the event handler is up to you. E.g. you can set a flag when an action is started and if the button is pressed while this flag is set, you do something different.

But this would only make sense if you actually have an action that is performed in the backround, using a timer or a worker thread.
Use the source, Luke!
gorf25
Earned a small fee
Earned a small fee
Posts: 15
Joined: Mon Aug 21, 2017 3:10 pm

Re: wxbutton

Post by gorf25 »

it may not be an easy do...

I have 2 buttons one with text that has Test for the button the other to exit the program...

When Test is clicked when in the function I change the test to calculate button and set a flag so when calculate is pressed
it performs a different function, now I change the text to save and set a flag, what I would like to do is change the exit button
to Cancel and not exit the program just cancel not save the results and the change both buttons back to Test and Exit...
I could use a messagebox but was trying to do it without the messagebox

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

Re: wxbutton

Post by doublemax »

Apart from the fact that that sounds like a bad user interface to me: What exactly is the problem from a technical point of view?
Use the source, Luke!
coderrc
Earned some good credits
Earned some good credits
Posts: 141
Joined: Tue Nov 01, 2016 2:46 pm

Re: wxbutton

Post by coderrc »

why not just make 4 different buttons "test|calculate|exit|cancel" then show and hide them as you see fit?
gorf25
Earned a small fee
Earned a small fee
Posts: 15
Joined: Mon Aug 21, 2017 3:10 pm

Re: wxbutton

Post by gorf25 »

I thought about that approach but been having trouble hiding the buttons doesn't seem to work for me,
I may be going about it wrong not sure tried a few different ways but no luck

Do you have an example to view...

Thanks gary
coderrc
Earned some good credits
Earned some good credits
Posts: 141
Joined: Tue Nov 01, 2016 2:46 pm

Re: wxbutton

Post by coderrc »

try this
h

Code: Select all

/////////////////////////////////////////////////////////////////////////////
// Name:        complexdialog2.h
// Purpose:     
// Author:      Anthemion Software Ltd
// Modified by: 
// Created:     03/11/2017 09:04:27
// RCS-ID:      
// Copyright:   (c) Anthemion Software Ltd
// Licence:     
/////////////////////////////////////////////////////////////////////////////
#ifndef _COMPLEXDIALOG2_H_
#define _COMPLEXDIALOG2_H_
#pragma interface "complexdialog2.h"

/*!
 * Includes
 */
////@begin includes
////@end includes
/*!
 * Forward declarations
 */
////@begin forward declarations
////@end forward declarations
/*!
 * Control identifiers
 */
////@begin control identifiers
#define ID_COMPLEXDIALOG 15000
#define ID_BTN_EXIT 15001
#define ID_BTN_CANCEL2 15004
#define ID_TEST 15003
#define ID_btn_Calculate 15002
#define SYMBOL_COMPLEXDIALOG2_STYLE wxCAPTION|wxRESIZE_BORDER|wxSYSTEM_MENU|wxCLOSE_BOX
#define SYMBOL_COMPLEXDIALOG2_TITLE _("ComplexDialog2")
#define SYMBOL_COMPLEXDIALOG2_IDNAME ID_COMPLEXDIALOG
#define SYMBOL_COMPLEXDIALOG2_SIZE wxSize(400, 300)
#define SYMBOL_COMPLEXDIALOG2_POSITION wxDefaultPosition
////@end control identifiers

/*!
 * ComplexDialog2 class declaration
 */
class ComplexDialog2: public wxDialog
{    
    DECLARE_DYNAMIC_CLASS( ComplexDialog2 )
    DECLARE_EVENT_TABLE()
public:
    /// Constructors
    ComplexDialog2();
    ComplexDialog2( wxWindow* parent, wxWindowID id = SYMBOL_COMPLEXDIALOG2_IDNAME, const wxString& caption = SYMBOL_COMPLEXDIALOG2_TITLE, const wxPoint& pos = SYMBOL_COMPLEXDIALOG2_POSITION, const wxSize& size = SYMBOL_COMPLEXDIALOG2_SIZE, long style = SYMBOL_COMPLEXDIALOG2_STYLE );
    /// Creation
    bool Create( wxWindow* parent, wxWindowID id = SYMBOL_COMPLEXDIALOG2_IDNAME, const wxString& caption = SYMBOL_COMPLEXDIALOG2_TITLE, const wxPoint& pos = SYMBOL_COMPLEXDIALOG2_POSITION, const wxSize& size = SYMBOL_COMPLEXDIALOG2_SIZE, long style = SYMBOL_COMPLEXDIALOG2_STYLE );
    /// Destructor
    ~ComplexDialog2();
    /// Initialises member variables
    void Init();
    /// Creates the controls and sizers
    void CreateControls();
////@begin ComplexDialog2 event handler declarations

    /// wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_BTN_EXIT
    void OnBtnExitClick( wxCommandEvent& event );

    /// wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_BTN_CANCEL2
    void OnBtnCancel2Click( wxCommandEvent& event );

    /// wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_TEST
    void OnTestClick( wxCommandEvent& event );

    /// wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_btn_Calculate
    void OnBtnCalculateClick( wxCommandEvent& event );

////@end ComplexDialog2 event handler declarations
////@begin ComplexDialog2 member function declarations
    /// Retrieves bitmap resources
    wxBitmap GetBitmapResource( const wxString& name );
    /// Retrieves icon resources
    wxIcon GetIconResource( const wxString& name );
////@end ComplexDialog2 member function declarations
    /// Should we show tooltips?
    static bool ShowToolTips();
////@begin ComplexDialog2 member variables
    wxButton* m_btnExit;
    wxButton* m_btnCancel;
    wxButton* m_btnTest;
    wxButton* m_btnCalculate;
////@end ComplexDialog2 member variables
};
#endif
    // _COMPLEXDIALOG2_H_

cpp

Code: Select all

/////////////////////////////////////////////////////////////////////////////
// Name:        complexdialog2.cpp
// Purpose:     
// Author:      Anthemion Software Ltd
// Modified by: 
// Created:     03/11/2017 09:04:27
// RCS-ID:      
// Copyright:   (c) Anthemion Software Ltd
// Licence:     
/////////////////////////////////////////////////////////////////////////////
#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
#pragma implementation "complexdialog2.h"
#endif

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

////@begin includes
////@end includes
#include "complexdialog2.h"
////@begin XPM images
////@end XPM images

/*!
 * ComplexDialog2 type definition
 */
IMPLEMENT_DYNAMIC_CLASS( ComplexDialog2, wxDialog )

/*!
 * ComplexDialog2 event table definition
 */
BEGIN_EVENT_TABLE( ComplexDialog2, wxDialog )
////@begin ComplexDialog2 event table entries
    EVT_BUTTON( ID_BTN_EXIT, ComplexDialog2::OnBtnExitClick )
    EVT_BUTTON( ID_BTN_CANCEL2, ComplexDialog2::OnBtnCancel2Click )
    EVT_BUTTON( ID_TEST, ComplexDialog2::OnTestClick )
    EVT_BUTTON( ID_btn_Calculate, ComplexDialog2::OnBtnCalculateClick )

////@end ComplexDialog2 event table entries
END_EVENT_TABLE()

/*!
 * ComplexDialog2 constructors
 */
ComplexDialog2::ComplexDialog2()
{
	Init();
}
ComplexDialog2::ComplexDialog2( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
	Init();
	Create(parent, id, caption, pos, size, style);
}

/*!
 * ComplexDialog2 creator
 */
bool ComplexDialog2::Create( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
////@begin ComplexDialog2 creation
    SetExtraStyle(wxWS_EX_BLOCK_EVENTS);
    wxDialog::Create( parent, id, caption, pos, size, style );
    CreateControls();
    Centre();
////@end ComplexDialog2 creation
	return true;
}

/*!
 * ComplexDialog2 destructor
 */
ComplexDialog2::~ComplexDialog2()
{
////@begin ComplexDialog2 destruction
////@end ComplexDialog2 destruction
}

/*!
 * Member initialisation
 */
void ComplexDialog2::Init()
{
////@begin ComplexDialog2 member initialisation
    m_btnExit = NULL;

    m_btnCancel = NULL;

    m_btnTest = NULL;

    m_btnCalculate = NULL;
////@end ComplexDialog2 member initialisation
}

/*!
 * Control creation for ComplexDialog2
 */
void ComplexDialog2::CreateControls()
{    
////@begin ComplexDialog2 content construction
    ComplexDialog2* itemDialog1 = this;
    wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
    itemDialog1->SetSizer(itemBoxSizer2);
    wxBoxSizer* itemBoxSizer3 = new wxBoxSizer(wxVERTICAL);
    itemBoxSizer2->Add(itemBoxSizer3, 1, wxGROW|wxALL, 5);
    wxStaticText* itemStaticText4 = new wxStaticText( itemDialog1, wxID_STATIC, _("This dialog demonstrates button hiding."), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer3->Add(itemStaticText4, 0, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);
    itemBoxSizer3->Add(5, 5, 1, wxALIGN_CENTER_HORIZONTAL|wxALL, 5);

    wxBoxSizer* itemBoxSizer6 = new wxBoxSizer(wxHORIZONTAL);
    itemBoxSizer3->Add(itemBoxSizer6, 0, wxGROW, 5);
    wxBoxSizer* itemBoxSizer7 = new wxBoxSizer(wxHORIZONTAL);
    itemBoxSizer6->Add(itemBoxSizer7, 1, wxGROW, 5);
    m_btnExit = new wxButton( itemDialog1, ID_BTN_EXIT, _("Exit"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer7->Add(m_btnExit, 1, wxGROW|wxALL, 5);
    m_btnCancel = new wxButton( itemDialog1, ID_BTN_CANCEL2, _("Cancel"), wxDefaultPosition, wxDefaultSize, 0 );
    m_btnCancel->Show(false);
    itemBoxSizer7->Add(m_btnCancel, 1, wxGROW|wxALL, 5);
    wxBoxSizer* itemBoxSizer10 = new wxBoxSizer(wxHORIZONTAL);
    itemBoxSizer6->Add(itemBoxSizer10, 1, wxGROW, 5);
    m_btnTest = new wxButton( itemDialog1, ID_TEST, _("Test"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer10->Add(m_btnTest, 1, wxGROW|wxALL, 5);
    m_btnCalculate = new wxButton( itemDialog1, ID_btn_Calculate, _("Calculate"), wxDefaultPosition, wxDefaultSize, 0 );
    m_btnCalculate->Show(false);
    itemBoxSizer10->Add(m_btnCalculate, 1, wxGROW|wxALL, 5);

#if defined(__WXMAC__)
    wxBoxSizer* itemBoxSizer13 = new wxBoxSizer(wxHORIZONTAL);
    itemBoxSizer3->Add(itemBoxSizer13, 0, wxGROW, 5);
    wxButton* itemButton14 = new wxButton( itemDialog1, wxID_HELP, _("&Help"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer13->Add(itemButton14, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
    itemBoxSizer13->Add(5, 5, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5);
    wxButton* itemButton16 = new wxButton( itemDialog1, wxID_CANCEL, _("&Cancel"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer13->Add(itemButton16, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
    wxButton* itemButton17 = new wxButton( itemDialog1, wxID_OK, _("&OK"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer13->Add(itemButton17, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

////@end ComplexDialog2 content construction

	m_btnCancel->Hide();
	m_btnCalculate - Hide();
	Layout();
}
#define __WXMSW__

/*!
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_BTN_EXIT
 */
void ComplexDialog2::OnBtnExitClick( wxCommandEvent& event )
{
	m_btnExit->Hide();
	m_btnCancel->Show();
	Layout();
	event.Skip();
}

/*!
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_BTN_CANCEL
 */
void ComplexDialog2::OnBtnCancel2Click( wxCommandEvent& event )
{
	m_btnCancel->Hide();
	m_btnExit->Show();
	Layout();
	event.Skip();
}

/*!
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_TEST
 */
void ComplexDialog2::OnTestClick( wxCommandEvent& event )
{
	m_btnTest->Hide();
	m_btnCalculate->Show();
	Layout();
	event.Skip(); 
}

/*!
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_btn_Calculate
 */
void ComplexDialog2::OnBtnCalculateClick( wxCommandEvent& event )
{
	m_btnCalculate->Hide();
	m_btnTest->Show();
	Layout();
	event.Skip();
}

/*!
 * Should we show tooltips?
 */
bool ComplexDialog2::ShowToolTips()
{
	return true;
}
/*!
 * Get bitmap resources
 */
wxBitmap ComplexDialog2::GetBitmapResource( const wxString& name )
{
	// Bitmap retrieval
////@begin ComplexDialog2 bitmap retrieval
    wxUnusedVar(name);
    return wxNullBitmap;
////@end ComplexDialog2 bitmap retrieval
}
/*!
 * Get icon resources
 */
wxIcon ComplexDialog2::GetIconResource( const wxString& name )
{
	// Icon retrieval
////@begin ComplexDialog2 icon retrieval
    wxUnusedVar(name);
    return wxNullIcon;
////@end ComplexDialog2 icon retrieval
}

the nice thing about having 4 buttons is you have 4 individual things to test and debug rather than 1 big mess of bullshit.
Post Reply