Unable to add items to wxComboBox using std::vector

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
var_null
Earned a small fee
Earned a small fee
Posts: 14
Joined: Thu Feb 27, 2020 6:44 pm

Unable to add items to wxComboBox using std::vector

Post by var_null » Sun Mar 15, 2020 5:24 pm

Tried to do it this way:

MedCen__Main.cpp:

Code: Select all

void MedCen__Frame::loadComboBoxItems()
{
   std::vector<std::string> IdTypes;
   IdTypes.reserve(4);
   IdTypes.push_back("Social Security Card");
   IdTypes.push_back("Driver's License");
   IdTypes.push_back("Passport");
   IdTypes.push_back("Department of Defense ID Card");
   combobox_PatIdType->Set(IdTypes);  
}
However it is not working, could someone please show me a "corrected" version of this code?

I don't know if that code is wrong or if I need to add something to MedCen__Main.h

I wanted to use std::vector to add items to the wxComboBox, I was told that for achieving this I needed to make loadComboBoxItems() a member of MedCen__Frame (otherwise, I'd get the following error regarding the "combobox_PatIdType->Set(IdTypes);" part: error: 'combobox_PatIdType' was not declared in this scope), so that is what I'm trying to do, make my function a member of MedCen__Frame.

I'm getting this error:

Code: Select all

no declaration matches 'void MedCen__Frame::loadComboBoxItems()'

Regards
Last edited by var_null on Sun Mar 15, 2020 7:37 pm, edited 1 time in total.

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2386
Joined: Sun Jan 03, 2010 5:45 pm

Re: Unable to add items to wxComboBox using std::vector

Post by PB » Sun Mar 15, 2020 6:37 pm

This is not surprising, as AFAIK std::string nor std::vector are not mentioned anywhere in the wxComboBox API/documentation. wxWidgets generally do not use std classes (with few exceptions such as conversion between wxString and std::[w]string).

Just use wxArrayString when passing items to wxComboBox.

Code: Select all

void MedCen__Frame::loadComboBoxItems()
{
   wxArrayString IdTypes; // <--- the only change required
   
   IdTypes.reserve(4);
   IdTypes.push_back("Social Security Card");
   IdTypes.push_back("Driver's License");
   IdTypes.push_back("Passport");
   IdTypes.push_back("Department of Defense ID Card");
   combobox_PatIdType->Set(IdTypes);  
}
EDIT
Actually, since wxWidgets 3.1 wxComboBox (via wxItemContainer) does use std::vector but std::vector<wxString> instead of std::vector<std::string>. What I wrote above still stands though.

var_null
Earned a small fee
Earned a small fee
Posts: 14
Joined: Thu Feb 27, 2020 6:44 pm

Re: Unable to add items to wxComboBox using std::vector

Post by var_null » Sun Mar 15, 2020 6:45 pm

Thank you very much for your reply

Could you please explain to me how to define the void MedCen__Frame::loadComboBoxItems() function in the header file? (MedCen_Main.h)

I've been googling a bit and I've seen examples of how to define other kind of functions, however I'm not sure about how to proceed with this one

Best regards

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2386
Joined: Sun Jan 03, 2010 5:45 pm

Re: Unable to add items to wxComboBox using std::vector

Post by PB » Sun Mar 15, 2020 6:54 pm

Firstly, you DECLARE stuff in the header files and DEFINE it in the source files, quite a diference. ;)

Anyway, you declare this method just as any other, e.g.,

Code: Select all

private:

        //(*Handlers(MedCen__Frame)
        void OnQuit(wxCommandEvent& event);
        void OnAbout(wxCommandEvent& event);
        void Onbtn_RecordDataClick(wxCommandEvent& event);
        void Onbtn_ListClick(wxCommandEvent& event);
        void On_button_RecordData_Click(wxCommandEvent& event);
        void On_button_List_Click(wxCommandEvent& event);
        //*)

        void loadComboBoxItems(); <-- HERE

        //(*Identifiers(MedCen__Frame)
        static const long ID_STATICTEXT20;
        static const long ID_TEXTCTRL7;
....
Just make sure you add your own stuff outside the //(* ... //*) blocks, otherwise it would get overwritten by wxSmith.

I also added the declaration in the "private" section, which means it can be called only by this class. If you want to call it by other classes, you need to add it in the "public" section, but that's just basic C++.

var_null
Earned a small fee
Earned a small fee
Posts: 14
Joined: Thu Feb 27, 2020 6:44 pm

Re: Unable to add items to wxComboBox using std::vector

Post by var_null » Tue Mar 17, 2020 6:30 am

Thanks, everything works fine now :)

Post Reply