wxComboBox - Is there a better way? 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
Dodle
Knows some wx things
Knows some wx things
Posts: 29
Joined: Fri Dec 26, 2008 11:21 am

wxComboBox - Is there a better way?

Post by Dodle » Tue May 12, 2009 7:33 am

I was just wondering if there was a better way to do this, or if this is satisfactory:

Code: Select all

  wxString sect_opt[] = {_T("Base System"), _T("Communication"), _T("Cross Platform"), _T("Development"),
  		_T("Documentation"), _T("Editors"), _T("Electronics"), _T("Email"), _T("Embedded Devices"),
  		_T("Gnome Desktop Environment"), _T("Games and Amusement"), _T("Graphics"),
  		_T("Internationalization and Localization"), _T("Interpreted Computer Languages"),
  		_T("KDE Desktop Environment"), _T("Libraries"), _T("Libraries - Development"),
  		_T("Libraries - Old"), _T("Mathematics"), _T("Meta Packages"), _T("Miscellaneous - Graphical"),
  		_T("Miscellaneous - Text Based"), _T("Multimedia"), _T("Networking"), _T("Newsgroup"),
  		_T("Perl Programming Language"), _T("Python Programming Language"),
  		_T("Science"), _T("Shells"), _T("System Administration"), _T("TeX Authoring"), _T("Unknown"),
  		_T("Utilities"), _T("Word Processing"), _T("World Wide Web")};


  wxComboBox *sect_widg = new wxComboBox(page_1, -1, sect_opt[31], wxDefaultPosition, wxDefaultSize, 35, sect_opt);

tan
Moderator
Moderator
Posts: 1471
Joined: Tue Nov 14, 2006 7:58 am
Location: Saint-Petersburg, Russia

Post by tan » Tue May 12, 2009 9:40 am

It is ok, but it is better use WXSIZEOF() for array dimension:

Code: Select all

wxComboBox *sect_widg = new wxComboBox(page_1, -1, sect_opt[31], wxDefaultPosition, wxDefaultSize, WXSIZEOF(sect_opt), sect_opt);
OS: Windows XP Pro
Compiler: MSVC++ 7.1
wxWidgets: 2.8.10

Jorg
Moderator
Moderator
Posts: 3971
Joined: Fri Aug 27, 2004 9:38 pm
Location: Delft, Netherlands
Contact:

Post by Jorg » Tue May 12, 2009 10:17 am

You always have to store the data somewhere. I would store it in a text file, read it with wxTextFile upon program execution, and fill your combobox. If you with to hard-code the text, store it in a seperate header file as a char array and write a small method to return a wxArrayString.

Header;

Code: Select all

const char *text[] = { "Text 1",
                       "Text 2",
                       "\0"  \\ marks end
                     };

void getText(wxArrayString &lines)
{
   lines.clear();
   
   const char **p = text;
   while(**p != '\0') {
      lines.add(*p);
      p++;
   }
}
You might need to doublecheck the ** v.s. * pointer convention to be sure it works..

Then you can use it like;

Code: Select all

wxArrayString lines;
getText(lines);
m_yourCombo->Clear();
m_yourCombo->Append(lines);
which makes it a lot easier on the eyes if you seperate your resource file(s) from the original code.

With regards,
- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb

Dodle
Knows some wx things
Knows some wx things
Posts: 29
Joined: Fri Dec 26, 2008 11:21 am

Post by Dodle » Tue May 12, 2009 7:29 pm

tan wrote:It is ok, but it is better use WXSIZEOF() for array dimension:

Code: Select all

wxComboBox *sect_widg = new wxComboBox(page_1, -1, sect_opt[31], wxDefaultPosition, wxDefaultSize, WXSIZEOF(sect_opt), sect_opt);
Thanks I like this a lot.

Jorg, I'll have to look into what you say. It may take me a little while to get it figured out. But thanks.

In Python I use lists a lot, which can be edited (remove and insert items). Is there anything like this in C++. I've looked at some documentation, but I tend to get a little confused.

ilovasz
Earned a small fee
Earned a small fee
Posts: 21
Joined: Tue Jul 10, 2007 6:56 pm

Post by ilovasz » Tue May 12, 2009 8:53 pm

Hi Dodle,

from the documentation:

Code: Select all

wxComboBox(wxWindow* parent, wxWindowID id, const wxString& value, const wxPoint& pos, const wxSize& size, const wxArrayString& choices, long style = 0, const wxValidator& validator = wxDefaultValidator, const wxString& name = "comboBox")
So You can use wxArrayString container for comboBox choices and You can manipulate it in several ways (Add, Insert, Remove, etc.)

Check out the documentation for more info.

LI

Post Reply