Using wxClientData with wxCombobox control

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
abdou
Earned a small fee
Earned a small fee
Posts: 16
Joined: Mon Nov 20, 2006 4:57 pm

Using wxClientData with wxCombobox control

Post by abdou »

Hi,
I have a problem when I try to append an item in my wxCombobox control. Here is the code whenthe appl crashes:

Code: Select all

  return wxComboBox::Append(item, new StringClientData(str));
Here is the StringClientData class

Code: Select all

class StringClientData : public wxClientData
  {
  public:
    StringClientData(const wxChar *c)
    {
      str=c;
    }
    wxString str;
  };
And the debugger shows that the error happens here :
#0 0051EF41 wxItemContainer::Append(this=0x150, item=@0x22f50c, clientData=0x1ebcd90) (D:/wxWidgets-2.8.3/include/wx/ctrlsub.h:103)

#1 0040BCBB ctlComboBox::Append(this=0x0, item=@0x22f50c, str=@0x22f51c) (D:/wxTulipe/src/ctrl/ctlComboBox.cpp:30)

#2 0040D097 ctlComboBox::FillStringKey(this=0x0, conn=@0x1eafc00, qry=0x1ec3ffc, lPrependKey=false) (D:/wxTulipe/src/ctrl/ctlComboBox.cpp:190)
Can you please point the error for me ?

Thank you !
Abdou
tan
wxWorld Domination!
wxWorld Domination!
Posts: 1471
Joined: Tue Nov 14, 2006 7:58 am
Location: Saint-Petersburg, Russia

Re: Using wxClientData with wxCombobox control

Post by tan »

Hi,
abdou wrote:Hi,
I have a problem when I try to append an item in my wxCombobox control. Here is the code whenthe appl crashes:

Code: Select all

  return wxComboBox::Append(item, new StringClientData(str));
is it your real code string? You have to use real instance of the wxComboBox class:

Code: Select all

    return m_combo_ctrl->Append(...);
Here is the StringClientData class

Code: Select all

class StringClientData : public wxClientData
  {
  public:
    StringClientData(const wxChar *c)
    {
      str=c;
    }
    wxString str;
  };
In wx there is a class for string clent data:
wxStringClientData
you don't need to create custom class for that.
OS: Windows XP Pro
Compiler: MSVC++ 7.1
wxWidgets: 2.8.10
abdou
Earned a small fee
Earned a small fee
Posts: 16
Joined: Mon Nov 20, 2006 4:57 pm

Re: Using wxClientData with wxCombobox control

Post by abdou »

Hi Tan,
Thank you for your assistance. I'll try to correct it according to your indications

Have a nice day !
Abdou
abdou
Earned a small fee
Earned a small fee
Posts: 16
Joined: Mon Nov 20, 2006 4:57 pm

Re: Using wxClientData with wxCombobox control

Post by abdou »

Hello again Tan,
tan wrote: is it your real code string? You have to use real instance of the wxComboBox class:

Code: Select all

    return m_combo_ctrl->Append(...);
I tried to apply your indication but I still receive the same error.
It's a class I derived from wxComboxBox to add methods to dynamically load items at runtime. These are my tries :

Code: Select all

  return Append(item, new wxStringClientData (str));

Code: Select all

  return this->Append(item, new wxStringClientData (str));
In wx there is a class for string clent data:
wxStringClientData
you don't need to create custom class for that.
You may have noticed that I'm using wxStringClientData now.

Thank you !
Abdou

P.S. What do you mean by
is it your real code string?
?[/quote]
tan
wxWorld Domination!
wxWorld Domination!
Posts: 1471
Joined: Tue Nov 14, 2006 7:58 am
Location: Saint-Petersburg, Russia

Re: Using wxClientData with wxCombobox control

Post by tan »

abdou wrote: I tried to apply your indication but I still receive the same error.
It's a class I derived from wxComboxBox to add methods to dynamically load items at runtime. These are my tries :

Code: Select all

return Append(item, new wxStringClientData (str));
return this->Append(item, new wxStringClientData (str));
Ok, i get it. It is in your derived class method (i thought it is in your parent dialog|panel|frame). In this case both lines are correct (just equal :)). I can't see anything wrong here. I'd like to look at your class definition and the metods implementation.
OS: Windows XP Pro
Compiler: MSVC++ 7.1
wxWidgets: 2.8.10
abdou
Earned a small fee
Earned a small fee
Posts: 16
Joined: Mon Nov 20, 2006 4:57 pm

Re: Using wxClientData with wxCombobox control

Post by abdou »

tan wrote: I'd like to look at your class definition and the metods implementation.
I don't beleive it will compile since I did not include all necessary files. but you'll see all the class.

Code: Select all

#define __COMBOBOX_H

// wxWindows headers
#include <wx/wx.h>
#include "pg/base.h"


class pgConnBase;
class ctlComboBox : public wxComboBox
{
public:
    ctlComboBox(wxWindow *wnd, int id, wxPoint pos, wxSize siz, long attr);

    int Append(const wxString& item, const wxString &str);
    int Append(const wxString& item, long l);

    long GetLongKey(int sel=-1);
    wxString GetStringKey(int sel=-1);

    bool SetKey(long val);
    bool SetKey(const wxString &val);

    int FillLongKey(pgConn &conn, const wxChar *qry);
    int FillStringKey (pgConn &conn, const wxChar *qry, bool lPrependKey);

    void FillComboLib(pgConn &conn, const wxString groupe, bool lPrependKey=false);
    void FillComboSeg(pgConn &conn, const wxString groupe, bool lPrependKey=false);
}
#endif

Code: Select all


// App headers
#include "pg/pgConnBase.h"
#include "pg/pgSetBase.h"

//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
ctlComboBox::ctlComboBox(wxWindow *wnd, int id, wxPoint pos, wxSize siz, long attr)
    : wxComboBox(wnd, id, wxEmptyString, pos, siz, 0, NULL, attr)
{}

int ctlComboBox::Append(const wxString& item, const wxString &str)
{
  return wxComboBox::Append(item, new wxStringClientData (str));
}

int ctlComboBox::Append(const wxString& item, long l)
{
  return wxComboBox::Append(item, (void*)l);
}

int ctlComboBox::FillLongKey(pgConn &conn, const wxChar *qry)
{
  int cnt=0;
  pgSetIterator set(conn.ExecuteSet(qry));
  while (set.RowsLeft())
    {
      long l=set.GetLong(0);
      wxString txt=set.GetVal(1);
      Append(txt, l);
      cnt++;
    }
  return cnt;
}

int ctlComboBox::FillStringKey(pgConn &conn, const wxChar *qry, bool lPrependKey)
{
  int cnt=0;
  pgSetIterator Set(conn.ExecuteSet(qry));
  while (Set.RowsLeft()) // Fill it now !
    {
      wxString str=Set.GetVal(0);
      wxString txt=Set.GetVal(0); //.Set()->TLibell();// Process with user language concern
      str = str.Strip(wxString::trailing);
      txt = txt.Strip(wxString::trailing);
      if  (lPrependKey)
        {
          txt = txt.Prepend(_(" - ")); // Key is preprended to the item text
          txt = txt.Prepend(str);
        }
      Append(txt, str);
      cnt++;
    }
}

long ctlComboBox::GetLongKey(int sel)
{
  if (sel < 0)
    sel = GetSelection();
  return (long)GetClientData(sel);
}

wxString ctlComboBox::GetStringKey(int sel)
{
  if (sel < 0)
    sel = GetSelection();
  wxStringClientData  *scd=(wxStringClientData *)GetClientObject(sel);
  if (scd)
    return scd->GetData() ;
  return wxEmptyString;
}


bool ctlComboBox::SetKey(long val)
{
  int i;
  for (i=0 ; i < GetCount() ; i++)
    {
      if (GetLongKey(i) == val)
        {
          SetSelection(i);
          return true;
        }
    }
  SetSelection(wxNOT_FOUND);
  return false;
}

bool ctlComboBox::SetKey(const wxString &val)
{
  int i;
  for (i=0 ; i < GetCount() ; i++)
    {
      if (GetStringKey(i) == val)
        {
          SetSelection(i);
          return true;
        }
    }
  SetSelection(wxNOT_FOUND);
  return false;
}
tan
wxWorld Domination!
wxWorld Domination!
Posts: 1471
Joined: Tue Nov 14, 2006 7:58 am
Location: Saint-Petersburg, Russia

Post by tan »

Hi,
i don't sure, but it is possible the problem is using the different client data types (typed - wxStringClientData* and untyped - void*) in the same control.
WX doc:
Finally note that in the same control all items must have client data of the same type (typed or untyped), if any. This type is determined by the first call to Append (the version with client data pointer) or SetClientData.
http://www.wxwidgets.org/manuals/stable ... lwithitems
OS: Windows XP Pro
Compiler: MSVC++ 7.1
wxWidgets: 2.8.10
Post Reply