Search code examples
c++wxwidgets

Setting vector<double> as choice values of wxComboBox


I am creating my first C++ program with GUI. I am using Code::Blocks v16.01 and wxWidgets v3.03. I have found that in the wxComboBox class constructor, the type that stands for choices is wxArrayString. I have tried to convert vector to vector and later to vector and to wxArrayString, but it failed miserably...

My question is how to set default choice values of wxComboBox object? Preferably I would like them to be filled with values of vector, created during program execution.

Here is the code:

#include <iostream>
#include <vector>
#include <fstream>
#include <sstream> 
using namespace std;

vector <double> vector_double;
vector <string> vector_string;
vector <wxString> vector_wxstring;

void convert_double_to_string(vector<double> &dbl, vector <string> &str)
{
    for (int i = 0; i < dbl.size(); i++)
    {
        ostringstream stream;
        stream << dbl[i];
        str.push_back(stream.str());
    }
}
void convert_string_to_wxString(vector<string> & str, vector <wxString> &wxstr);
{
    for (int i = 0; i < str.size(); i++)
    {
        wxstr.push_back(_(str[i]));
    }
}
void main()
{
    /////////

    // here setting vector_double's values

    //////////

   convert_double_to_string(vector_double, vector_string);
   convert_string_to_wxString(vector<string> vector_string, vector_wxstring);
}

This is what I have got. The string conversion to wxString is not working though. And even if it would, I wouldn't know how to insert it into wxArrayString.


Solution

  • Something along these lines:

    wxComboBox * cbo = new wxComboBox( ... );
    wxArrayString as;
    for ( auto& s : vector_string )
      as.Add( s );
    cbo->Set( as );