add string to wxChoice Topic is solved

Do you have a question about makefiles, a compiler or IDE you are using and need to know how to set it up for wxWidgets or why it doesn't compile but other IDE's do ? Post your questions here.
Post Reply
hinet
Experienced Solver
Experienced Solver
Posts: 64
Joined: Mon Aug 08, 2016 9:44 am
Location: France
Contact:

add string to wxChoice

Post by hinet »

Hi all coming back after 2 years :D :D :D :D

It is possible to add a wxstring to a wxChoice from a derived class??

Code: Select all

	wxString m_choice3Choices[] = { wxT("COM1"), wxT("COM2"), wxT("COM3") };
	int m_choice3NChoices = sizeof( m_choice3Choices ) / sizeof( wxString );
	m_choice3 = new wxChoice( sbSizer5->GetStaticBox(), wxID_ANY, wxDefaultPosition, wxDefaultSize, m_choice3NChoices, m_choice3Choices, 0 );
	m_choice3->SetSelection( 0 );
	bSizer10->Add( m_choice3, 1, wxALL, 5 );
this code is generated using wxformbuilder, I d like add "COM4" from an anther class, I won't add it here in this class because I use wxformebuilder to generate code and every time this code is updated.

thanks for help.
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: add string to wxChoice

Post by doublemax »

I don't know wxformbuilder, but usually there are some markers in the source that show where you can add code that will not get overwritten.
Use the source, Luke!
Nunki
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 235
Joined: Fri Sep 14, 2012 8:26 am
Location: Kontich, Belgium
Contact:

Re: add string to wxChoice

Post by Nunki »

Hi Hinet,

That's why I'm such an advocate of the XRC system. Then you can separate the screens from your code. It's worthwhile to look into that part of wxWidgets. Though not everything is possible, you do get pretty far without having to add some extra coding. If you want some more information on using XRC and some code examples on how I structure my classes, feel free to ask.

Now if you want to stay with your current way of coding, I would add for every dialog a call to a Set() method, somewhere in an area that is free from alteration by the generator. Then define your Set() method and put all your special code there.

Regards,
Nunki
hinet
Experienced Solver
Experienced Solver
Posts: 64
Joined: Mon Aug 08, 2016 9:44 am
Location: France
Contact:

Re: add string to wxChoice

Post by hinet »

doublemax wrote:I don't know wxformbuilder, but usually there are some markers in the source that show where you can add code that will not get overwritten.
Hi doublemax,


wxformbuilder generate the same file everytime we click generate code. every time the file is overwritten.

The idea is to separate the view from my code "control". I have to add in real time values to this wxchoice like , "COM1" for example.

To do so i derived from the view class, I could access to all the pointers to objects in the view.

now I d like to get this object and append a new wxstring from the derived class without editing the view class.

Code: Select all

///////////////////////////////////////////////////////////////////////////
// C++ code generated with wxFormBuilder (version Jun 17 2015)
// http://www.wxformbuilder.org/
//
// PLEASE DO "NOT" EDIT THIS FILE!
///////////////////////////////////////////////////////////////////////////

#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif //__BORLANDC__

#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif //WX_PRECOMP

#include "GUIFrame.h"

///////////////////////////////////////////////////////////////////////////

GUIFrame::GUIFrame( wxWindow* parent, wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style ) : wxFrame( parent, id, title, pos, size, style )
{
	this->SetSizeHints( wxDefaultSize, wxDefaultSize );
	
	mbar = new wxMenuBar( 0 );
	fileMenu = new wxMenu();
	wxMenuItem* menuFileQuit;
	menuFileQuit = new wxMenuItem( fileMenu, idMenuQuit, wxString( wxT("&Quit") ) + wxT('\t') + wxT("Alt+F4"), wxT("Quit the application"), wxITEM_NORMAL );
	fileMenu->Append( menuFileQuit );
	
	mbar->Append( fileMenu, wxT("&File") ); 
	
	helpMenu = new wxMenu();
	wxMenuItem* menuHelpAbout;
	menuHelpAbout = new wxMenuItem( helpMenu, idMenuAbout, wxString( wxT("&About") ) + wxT('\t') + wxT("F1"), wxT("Show info about this application"), wxITEM_NORMAL );
	helpMenu->Append( menuHelpAbout );
	
	mbar->Append( helpMenu, wxT("&Help") ); 
	
	m_menu3 = new wxMenu();
	mbar->Append( m_menu3, wxT("Data Acquisition") ); 
	
	this->SetMenuBar( mbar );
	
	statusBar = this->CreateStatusBar( 2, wxST_SIZEGRIP, wxID_ANY );
	wxBoxSizer* bSizer4;
	bSizer4 = new wxBoxSizer( wxVERTICAL );
	
	m_hyperlink1 = new wxHyperlinkCtrl( this, wxID_ANY, wxT("-----------"), wxT("http://www.wxformbuilder.org"), wxDefaultPosition, wxDefaultSize, wxHL_DEFAULT_STYLE );
	bSizer4->Add( m_hyperlink1, 0, wxALL, 5 );
	
	wxBoxSizer* bSizer41;
	bSizer41 = new wxBoxSizer( wxVERTICAL );
	
	wxStaticBoxSizer* sbSizer5;
	sbSizer5 = new wxStaticBoxSizer( new wxStaticBox( this, wxID_ANY, wxT("Serial Settings") ), wxHORIZONTAL );
	
	wxGridSizer* gSizer5;
	gSizer5 = new wxGridSizer( 0, 2, 0, 0 );
	
	wxBoxSizer* bSizer10;
	bSizer10 = new wxBoxSizer( wxHORIZONTAL );
	
	m_staticText5 = new wxStaticText( sbSizer5->GetStaticBox(), wxID_ANY, wxT("Availibale Ports"), wxDefaultPosition, wxDefaultSize, 0 );
	m_staticText5->Wrap( -1 );
	bSizer10->Add( m_staticText5, 0, wxALL, 5 );
	
	wxString m_choice3Choices[] = { wxT("COM1"), wxT("COM2"), wxT("COM3") };
	int m_choice3NChoices = sizeof( m_choice3Choices ) / sizeof( wxString );
	m_choice3 = new wxChoice( sbSizer5->GetStaticBox(), wxID_ANY, wxDefaultPosition, wxDefaultSize, m_choice3NChoices, m_choice3Choices, 0 );
	m_choice3->SetSelection( 0 );
	bSizer10->Add( m_choice3, 1, wxALL, 5 );
	
	
	gSizer5->Add( bSizer10, 1, wxEXPAND, 5 );
	
	wxBoxSizer* bSizer11;
	bSizer11 = new wxBoxSizer( wxHORIZONTAL );
	
	m_staticText6 = new wxStaticText( sbSizer5->GetStaticBox(), wxID_ANY, wxT("BaudRate"), wxDefaultPosition, wxDefaultSize, 0 );
	m_staticText6->Wrap( -1 );
	bSizer11->Add( m_staticText6, 0, wxALL, 5 );
	
	wxString m_choice4Choices[] = { wxT("9600"), wxT("19200"), wxT("115200") };
	int m_choice4NChoices = sizeof( m_choice4Choices ) / sizeof( wxString );
	m_choice4 = new wxChoice( sbSizer5->GetStaticBox(), wxID_ANY, wxDefaultPosition, wxDefaultSize, m_choice4NChoices, m_choice4Choices, 0 );
	m_choice4->SetSelection( 0 );
	bSizer11->Add( m_choice4, 1, wxALL, 5 );
	
	
	gSizer5->Add( bSizer11, 1, wxEXPAND, 5 );
	
	
	sbSizer5->Add( gSizer5, 1, wxEXPAND, 5 );
	
	
	bSizer41->Add( sbSizer5, 0, wxEXPAND, 5 );
	
	
	bSizer4->Add( bSizer41, 1, wxEXPAND, 5 );
	
	wxGridSizer* gSizer1;
	gSizer1 = new wxGridSizer( 0, 2, 0, 0 );
	
	wxStaticBoxSizer* sbSizer2;
	sbSizer2 = new wxStaticBoxSizer( new wxStaticBox( this, wxID_ANY, wxT("Serial Transmit") ), wxVERTICAL );
	
	wxBoxSizer* bSizer9;
	bSizer9 = new wxBoxSizer( wxVERTICAL );
	
	wxGridSizer* gSizer6;
	gSizer6 = new wxGridSizer( 0, 2, 0, 0 );
	
	
	bSizer9->Add( gSizer6, 1, wxEXPAND, 5 );
	
	
	sbSizer2->Add( bSizer9, 1, wxEXPAND, 5 );
	
	
	gSizer1->Add( sbSizer2, 1, wxEXPAND, 5 );
	
	wxStaticBoxSizer* sbSizer4;
	sbSizer4 = new wxStaticBoxSizer( new wxStaticBox( this, wxID_ANY, wxT("Serial Receive") ), wxVERTICAL );
	
	wxBoxSizer* bSizer6;
	bSizer6 = new wxBoxSizer( wxHORIZONTAL );
	
	
	sbSizer4->Add( bSizer6, 1, wxEXPAND, 5 );
	
	
	gSizer1->Add( sbSizer4, 1, wxEXPAND, 5 );
	
	
	bSizer4->Add( gSizer1, 3, wxEXPAND, 5 );
	
	wxBoxSizer* bSizer8;
	bSizer8 = new wxBoxSizer( wxVERTICAL );
	
	wxStaticBoxSizer* sbSizer9;
	sbSizer9 = new wxStaticBoxSizer( new wxStaticBox( this, wxID_ANY, wxT("Log") ), wxVERTICAL );
	
	
	bSizer8->Add( sbSizer9, 1, wxEXPAND, 5 );
	
	
	bSizer4->Add( bSizer8, 1, wxEXPAND, 5 );
	
	
	this->SetSizer( bSizer4 );
	this->Layout();
	
	// Connect Events
	this->Connect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler( GUIFrame::OnClose ) );
	this->Connect( menuFileQuit->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( GUIFrame::OnQuit ) );
	this->Connect( menuHelpAbout->GetId(), wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( GUIFrame::OnAbout ) );
}

GUIFrame::~GUIFrame()
{
	// Disconnect Events
	this->Disconnect( wxEVT_CLOSE_WINDOW, wxCloseEventHandler( GUIFrame::OnClose ) );
	this->Disconnect( idMenuQuit, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( GUIFrame::OnQuit ) );
	this->Disconnect( idMenuAbout, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( GUIFrame::OnAbout ) );
	
}

Code: Select all

#ifndef HANDLING_H_INCLUDED
#define HANDLING_H_INCLUDED

class handling:public GUIFrame{
private:
public:
        handling( wxFrame *frame );

};

#endif // HANDLING_H_INCLUDED

Code: Select all


#include "GUIFrame.h"
#include "handling.h"
#include "Serial.h"




handling::handling( wxFrame *frame ):
            GUIFrame( frame)
{

    //to do call the wxchoice and append wxstring ....
}

Last edited by hinet on Sun May 27, 2018 4:28 am, edited 1 time in total.
Nunki
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 235
Joined: Fri Sep 14, 2012 8:26 am
Location: Kontich, Belgium
Contact:

Re: add string to wxChoice

Post by Nunki »

Hi Hinet,

I think you need to look for the possibility to store handles in the class so that you can access them from your code.

Code: Select all

 m_choice4 = new wxChoice( sbSizer5->GetStaticBox(), wxID_ANY, wxDefaultPosition, wxDefaultSize, m_choice4NChoices, m_choice4Choices, 0 );
instead of a local variable (m_choice4) in the method, this should be a member in your class. If that's not possible, then look for a way to give an ID to the wxChoice control instead of the wxID_ANY. In that way you should be able to search for the handle by means of this ID. And once you get the handle you can add strings.

Then there's always DialogBlocks as an alternative.

regards,
Nunki.
hinet
Experienced Solver
Experienced Solver
Posts: 64
Joined: Mon Aug 08, 2016 9:44 am
Location: France
Contact:

Re: add string to wxChoice

Post by hinet »

Nunki wrote:Hi Hinet,

I think you need to look for the possibility to store handles in the class so that you can access them from your code.

Code: Select all

 m_choice4 = new wxChoice( sbSizer5->GetStaticBox(), wxID_ANY, wxDefaultPosition, wxDefaultSize, m_choice4NChoices, m_choice4Choices, 0 );
instead of a local variable (m_choice4) in the method, this should be a member in your class. If that's not possible, then look for a way to give an ID to the wxChoice control instead of the wxID_ANY. In that way you should be able to search for the handle by means of this ID. And once you get the handle you can add strings.

Then there's always DialogBlocks as an alternative.

regards,
Nunki.
Hi Nunki thanks for your interest,

The object is already declared in the parent class with protected attribute, In an anther word we could access to this pointer from a drived class.


I found in wx documentation that we could call Append method.


and it works :D :D :D

Code: Select all

handling::handling( wxFrame *frame ):
            GUIFrame( frame)
{

    //to do call the wxchoice and append wxstring ....
    int number = m_choice3->GetCurrentSelection();
    m_choice3->Append("COM5");
    m_choice3->Append("COM39");


    m_choice4->Append("2345");
}
Hinet,
Regards.
Post Reply