need help on using wxComboBox

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
Hossein
Earned some good credits
Earned some good credits
Posts: 120
Joined: Sun Sep 21, 2008 7:23 am
Location: Somwhere nearby...
Contact:

need help on using wxComboBox

Post by Hossein »

hello all , i wrote a simple snippet of code to actually play with couple of wxWidgets components . i got stuck . can any one please help me get this bit of code to work ? and tell me my problems ?

Code: Select all

	int SelectedItemIndex = WxComboBox1->GetSelection();
	if ( WxComboBox1->GetString(SelectedItemIndex) == wxT("open file"))
	{
        WxEdit1->SetValue(WxComboBox1->GetCurrentSelection());
        if ( WxOpenFileDialog1->ShowModal() == wxID_OK )
        {
            WxEdit1->SetValue(WxOpenFileDialog1->GetPath());
        }
        else
        {
            WxEdit1->SetValue("NoThing ");
        }

    }
Thank you in advance
Add-on Components (90)
Applications (183)
Development Tools (27)
Icons and Resources (1)
Sample Code and Project Templates (10)
Utilities (4)
wxWidgets (10)

http://www.wxcommunity.com/
Hossein
Earned some good credits
Earned some good credits
Posts: 120
Joined: Sun Sep 21, 2008 7:23 am
Location: Somwhere nearby...
Contact:

Re: need help on using wxComboBox

Post by Hossein »

got it . the documentation needs to be updated !
in documentation it says :
GetCurrentSelection () const
Returns the item being selected right now.
that made me think it returns the actual object , which in my case was a simple string ( or WxString ) . although i noticed that virtual int , but was unsure which one was a typo !
how am i supposed to get the current selected object in a combo box then ?
i would like to know how i can do it in wxWidgets .

and here is the code anyway .

Code: Select all

	int SelectedItemIndex = WxComboBox1->GetSelection();
	if ( WxComboBox1->GetString(SelectedItemIndex) == wxT("open file"))
	{
        WxEdit1->SetValue(WxComboBox1->GetString(SelectedItemIndex));
        if ( WxOpenFileDialog1->ShowModal() == wxID_OK )
        {
            WxEdit1->SetValue(WxOpenFileDialog1->GetPath());
        }
        else
        {
            WxEdit1->SetValue(wxT("NoThing "));
        }

    }
by the way i recall from two or three years ago , that wxwidgets were going to convert strings to WxString at backstage , is it not yet implemented ? or it is me that is missing something here . would appreciate any help on this too
Add-on Components (90)
Applications (183)
Development Tools (27)
Icons and Resources (1)
Sample Code and Project Templates (10)
Utilities (4)
wxWidgets (10)

http://www.wxcommunity.com/
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: need help on using wxComboBox

Post by doublemax »

And what exactly is the problem?
Use the source, Luke!
Hossein
Earned some good credits
Earned some good credits
Posts: 120
Joined: Sun Sep 21, 2008 7:23 am
Location: Somwhere nearby...
Contact:

Re: need help on using wxComboBox

Post by Hossein »

doublemax wrote:And what exactly is the problem?
how do i get the currently selected item (object ) .
the documentation is wrong here :
GetCurrentSelection () const
Returns the item being selected right now.
it returns the index of the object not the object itself !
Add-on Components (90)
Applications (183)
Development Tools (27)
Icons and Resources (1)
Sample Code and Project Templates (10)
Utilities (4)
wxWidgets (10)

http://www.wxcommunity.com/
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: need help on using wxComboBox

Post by doublemax »

Code: Select all

   int SelectedItemIndex = WxComboBox1->GetSelection();
   if ( WxComboBox1->GetString(SelectedItemIndex) == wxT("open file"))
The code you used looks ok, although just using GetStringSelection() should do the same.

If it doesn't work, use a debugger (or old-style printf debugging) to check if the return values of GetSelection() and GetString() contain what you expect.
Use the source, Luke!
Hossein
Earned some good credits
Earned some good credits
Posts: 120
Joined: Sun Sep 21, 2008 7:23 am
Location: Somwhere nearby...
Contact:

Re: need help on using wxComboBox

Post by Hossein »

doublemax wrote:

Code: Select all

   int SelectedItemIndex = WxComboBox1->GetSelection();
   if ( WxComboBox1->GetString(SelectedItemIndex) == wxT("open file"))
The code you used looks ok, although just using GetStringSelection() should do the same.

If it doesn't work, use a debugger (or old-style printf debugging) to check if the return values of GetSelection() and GetString() contain what you expect.
you didnt get me .
in c# we can save objects in combo box , be it a string or a button or pretty much anything else , then we could retrieve that object and cast it to its respective type and re use it .
but here , it seems it only supports strings! if it is not the case , i would like to hear more explanation on how to achieve the exact result in wxwdigets .
Add-on Components (90)
Applications (183)
Development Tools (27)
Icons and Resources (1)
Sample Code and Project Templates (10)
Utilities (4)
wxWidgets (10)

http://www.wxcommunity.com/
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: need help on using wxComboBox

Post by doublemax »

You can't achieve the exact same behavior.

Check the clientData parameter in the Append call. If you want to attach any data to the entries, that's the way to go.
http://docs.wxwidgets.org/stable/wx_wxc ... temsappend

Use wxControlWithItems::GetClientData() or wxControlWithItems::GetClientObject() to retrieve the pointer later.
Use the source, Luke!
Hossein
Earned some good credits
Earned some good credits
Posts: 120
Joined: Sun Sep 21, 2008 7:23 am
Location: Somwhere nearby...
Contact:

Re: need help on using wxComboBox

Post by Hossein »

doublemax wrote:You can't achieve the exact same behavior.

Check the clientData parameter in the Append call. If you want to attach any data to the entries, that's the way to go.
http://docs.wxwidgets.org/stable/wx_wxc ... temsappend

Use wxControlWithItems::GetClientData() or wxControlWithItems::GetClientObject() to retrieve the pointer later.
thank you .
by any chane do you have any demo sample demonstrating that control in action ?
Add-on Components (90)
Applications (183)
Development Tools (27)
Icons and Resources (1)
Sample Code and Project Templates (10)
Utilities (4)
wxWidgets (10)

http://www.wxcommunity.com/
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: need help on using wxComboBox

Post by doublemax »

Both the "controls" and "widgets" samples use that feature, but these sample are not really focusing on this particular feature, so it might not be so clear on how to use them.

But in general there is not much you need to know. The big difference between using untyped and typed data is, that by deriving your own class from wxClientData you achieve a little bit of type-safety and - more importantly - the control will take ownership of the pointer and delete it when the entry gets deleted. When you're using untyped data, you're responsible for deleting it yourself.
Use the source, Luke!
Hossein
Earned some good credits
Earned some good credits
Posts: 120
Joined: Sun Sep 21, 2008 7:23 am
Location: Somwhere nearby...
Contact:

Re: need help on using wxComboBox

Post by Hossein »

doublemax wrote:Both the "controls" and "widgets" samples use that feature, but these sample are not really focusing on this particular feature, so it might not be so clear on how to use them.

But in general there is not much you need to know. The big difference between using untyped and typed data is, that by deriving your own class from wxClientData you achieve a little bit of type-safety and - more importantly - the control will take ownership of the pointer and delete it when the entry gets deleted. When you're using untyped data, you're responsible for deleting it yourself.
thank you again .
i think i'll give it a try and in case i encounter an error i report back then .
Add-on Components (90)
Applications (183)
Development Tools (27)
Icons and Resources (1)
Sample Code and Project Templates (10)
Utilities (4)
wxWidgets (10)

http://www.wxcommunity.com/
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: need help on using wxComboBox

Post by PB »

A simple example of client object usage with wxComboBox

Code: Select all

#include <wx/wxprec.h>

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

class Person: public wxClientData
{
public:
    wxString m_name;
    int      m_age;

    Person(const wxString& name, int age)
    {
        m_name = name;
        m_age = age;
    }            
};

class MyDialog: public wxDialog
{
public:
    MyDialog()
        : wxDialog(NULL, wxID_ANY, _("Test"), wxDefaultPosition)
    {
       
        wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);        

        m_comboBox = new wxComboBox(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, NULL, wxCB_READONLY);        
        m_comboBox->Append(wxT("Son"), new Person(wxT("John"), 20));
        m_comboBox->Append(wxT("Dad"), new Person(wxT("Jack"), 40));
        m_comboBox->Append(wxT("Grandad"), new Person(wxT("Joe"), 60));
        m_comboBox->SetSelection(0);             
        sizer->Add(m_comboBox);

        wxButton* button = new wxButton(this, wxID_ANY, wxT("Display selected person info"));        
        sizer->Add(button);
        button->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MyDialog::OnClicked), NULL, this);

        SetSizer(sizer);
        Layout();
        Centre();
    }	
private:        
    wxComboBox* m_comboBox;    

     void OnClicked(wxCommandEvent& evt)
    {
        int sel = m_comboBox->GetSelection();
        
        if (sel != wxNOT_FOUND) {
            Person* p = dynamic_cast<Person*>(m_comboBox->GetClientObject(sel));        
            if (p) 
                wxMessageBox(wxString::Format(wxT("Name: %s, age: %d"), p->m_name.c_str(), p->m_age));                        
        }        
    }
};


class MyApp : public wxApp
{
public:	
	virtual bool OnInit()
	{
		if (!wxApp::OnInit())
			return false;       	
        
        MyDialog dlg;
        dlg.ShowModal();        

        return false;
	}
};

IMPLEMENT_APP(MyApp)
Hossein
Earned some good credits
Earned some good credits
Posts: 120
Joined: Sun Sep 21, 2008 7:23 am
Location: Somwhere nearby...
Contact:

Re: need help on using wxComboBox

Post by Hossein »

PB wrote:A simple example of client object usage with wxComboBox

Code: Select all

#include <wx/wxprec.h>

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

class Person: public wxClientData
{
public:
    wxString m_name;
    int      m_age;

    Person(const wxString& name, int age)
    {
        m_name = name;
        m_age = age;
    }            
};

class MyDialog: public wxDialog
{
public:
    MyDialog()
        : wxDialog(NULL, wxID_ANY, _("Test"), wxDefaultPosition)
    {
       
        wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);        

        m_comboBox = new wxComboBox(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, NULL, wxCB_READONLY);        
        m_comboBox->Append(wxT("Son"), new Person(wxT("John"), 20));
        m_comboBox->Append(wxT("Dad"), new Person(wxT("Jack"), 40));
        m_comboBox->Append(wxT("Grandad"), new Person(wxT("Joe"), 60));
        m_comboBox->SetSelection(0);             
        sizer->Add(m_comboBox);

        wxButton* button = new wxButton(this, wxID_ANY, wxT("Display selected person info"));        
        sizer->Add(button);
        button->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(MyDialog::OnClicked), NULL, this);

        SetSizer(sizer);
        Layout();
        Centre();
    }	
private:        
    wxComboBox* m_comboBox;    

     void OnClicked(wxCommandEvent& evt)
    {
        int sel = m_comboBox->GetSelection();
        
        if (sel != wxNOT_FOUND) {
            Person* p = dynamic_cast<Person*>(m_comboBox->GetClientObject(sel));        
            if (p) 
                wxMessageBox(wxString::Format(wxT("Name: %s, age: %d"), p->m_name.c_str(), p->m_age));                        
        }        
    }
};


class MyApp : public wxApp
{
public:	
	virtual bool OnInit()
	{
		if (!wxApp::OnInit())
			return false;       	
        
        MyDialog dlg;
        dlg.ShowModal();        

        return false;
	}
};

IMPLEMENT_APP(MyApp)
awesome =D>
thanks a billion for the treat :)
Add-on Components (90)
Applications (183)
Development Tools (27)
Icons and Resources (1)
Sample Code and Project Templates (10)
Utilities (4)
wxWidgets (10)

http://www.wxcommunity.com/
Post Reply