开发者

How to add value to second column using wxListCtrl in wxWidgets (C++ code)?

I've following code:

int column_width = 100;

long indx1 = alist->InsertColumn(0, L"User Name", wxLIST_FORMAT_LEFT, column_width);

long indx2 = alist->InsertColumn(1, L"User Id", wxLIST_FORMAT_LEFT, column_width);

long itemIndex1 = alist->InsertItem(indx1, L"John Smith", -1);

alist->SetItem(indx1, 1, L"jsmith");

I expect to see two columns with User Name and User Id as heading with "John Smith" and "jsmith" as values on the first row. Instead I only see "John Smith" under column User Name but nothing under User ID column. Here is a link to the snapshot showi开发者_运维问答ng the result: http://drop.io/agtyt6s

Thanks!


Here is a minimal sample that displays two columns. Note that I am using the index item returned by the InsertItem method in the SetItem method.

#include <wx/wx.h>

class Frame : public wxFrame
{
public:
  Frame():
    wxFrame(NULL, wxNewId(), _("App"))
  {
    wxBoxSizer * box = new wxBoxSizer(wxVERTICAL);

    wxListCtrl * listCtrl = new wxListCtrl(this, wxNewId(), wxDefaultPosition, wxDefaultSize, wxLC_REPORT);
    listCtrl->InsertColumn(0, _("User Name"));
    listCtrl->InsertColumn(1, _("User ID"));

    long index = listCtrl->InsertItem(0, _("John Smith"));
    listCtrl->SetItem(index, 1, _("jsmith"));

    box->Add(listCtrl, 1, wxEXPAND, 0);
    SetSizer(box);
    box->SetSizeHints(this);
    Show(true);
  }
};

class App : public wxApp
{
  bool OnInit()
  {
    SetTopWindow(new Frame());
  }
};

IMPLEMENT_APP(App);


Ok, I finally got it resolved by the help of wxWidgets forum contributors. Here is what the issue was: I was using

alist1 = new wxListCtrl( m_panel1, wxID_ANY, wxDefaultPosition, wxSize( 300,-1 ), wxLC_ICON|wxLC_REPORT|wxLC_SINGLE_SEL );

as the style of the wxListCtrl. It turns out that wxLC_ICON and wxLC_REPORT are mutually exclusive so you can only have one. When I removed wxLC_ICON it worked just fine! Thanks again for your help small_duck!


This example is a best way for me, to insert on column.

#include <wx/wx.h>
#include <wx/listctrl.h>
#include <wx/colour.h>

enum { ID_LISTBOOK = 10};

class MyApp:public wxApp {
bool OnInit()
{
if ( !wxApp::OnInit() )
return false;

wxInitAllImageHandlers();
wxFrame* frame   = new wxFrame(NULL, wxID_ANY,"wxListCtrl Insert Colored Items");
wxListCtrl* listCtrl = new wxListCtrl(frame,ID_LISTBOOK, wxDefaultPosition, wxDefaultSize, wxLC_REPORT);
listCtrl->InsertColumn(0, "Name", wxLIST_FORMAT_LEFT);
listCtrl->InsertColumn(1, "Number", wxLIST_FORMAT_LEFT);

wxListItem* item     = new wxListItem();

item->SetBackgroundColour(*wxRED);
item->SetText(wxT("Programmer"));
item->SetId(0);


listCtrl->InsertItem(0, *item);
listCtrl->InsertItem(1, *item);
listCtrl->InsertItem(2, *item);
listCtrl->InsertItem(3, *item);

listCtrl->SetItem(0,0,wxT("1"), -1);
listCtrl->SetItem(0,1,wxT("1"), -1);
listCtrl->SetItem(1,0,wxT("2"), -1);
listCtrl->SetItem(1,1,wxT("2"), -1);
listCtrl->SetItem(2,0,wxT("3"), -1);
listCtrl->SetItem(2,1,wxT("3"), -1);
listCtrl->SetItem(3,0,wxT("4"), -1);
listCtrl->SetItem(3,1,wxT("4"), -1);

frame->Show(true);
return true;
};
};

IMPLEMENT_APP(MyApp)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜