簡體   English   中英

如何在wxWidgets(C ++代碼)中使用wxListCtrl向第二列添加值?

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

我下面的代碼:

int column_width = 100;

long indx1 = alist-> InsertColumn(0,L“用戶名”,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”);

我希望在第一行中看到兩列,其中“用戶名”和“用戶ID”分別以“ John Smith”和“ jsmith”作為值。 相反,我只在“用戶名”列下看到“ John Smith”,而在“用戶ID”列下什么都沒有。 這是顯示結果的快照的鏈接: http : //drop.io/agtyt6s

謝謝!

這是顯示兩列的最小示例。 請注意,我在SetItem方法中使用InsertItem方法返回的索引項。

#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);

好的,我終於在wxWidgets論壇貢獻者的幫助下解決了它。 這是問題所在:我正在使用

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

作為wxListCtrl的樣式。 事實證明,wxLC_ICON和wxLC_REPORT是互斥的,因此您只能擁有一個。 當我刪除wxLC_ICON時,一切正常! 再次感謝您的幫助small_duck!

這個例子對我來說是插入列的最佳方法。

#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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM