本文主要是介绍30、wxWidgets小部件之wxListBox,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
还可以参考
1.wxListCtrl https://docs.wxwidgets.org/3.0/classwx_list_ctrl.html
2.wxDataViewCtrl https://docs.wxwidgets.org/3.0/classwx_data_view_ctrl.html
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif // WX_PRECOMP
#include <wx/listbox.h>
#include "mypanel.h"
class Simple:public wxFrame
{
public:
Simple(const wxString& title);
wxListBox* listbox;
protected:
void OnDblClick(wxCommandEvent& event);
MyPanel* btnPanel;
};
Simple::Simple(const wxString& title)
:wxFrame(NULL,-1,title)
{
wxPanel* panel = new wxPanel(this,-1);
listbox = new wxListBox(panel,-1,wxPoint(-1,-1),wxSize(-1,-1));
// ,0
// ,NULL
// ,wxLB_MULTIPLE|wxLB_SORT);
btnPanel=new MyPanel(panel);
wxBoxSizer* szMain = new wxBoxSizer(wxHORIZONTAL);
szMain->Add(listbox,1,wxEXPAND|wxALL,20);
szMain->Add(btnPanel,0,wxEXPAND|wxRIGHT,20);
Bind(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED,Simple::OnDblClick,this);
panel->SetSizer(szMain);
Centre();
}
void Simple::OnDblClick(wxCommandEvent& event)
{
wxString text,renamed;
int sel = listbox->GetSelection();
if (sel != -1) {
text = listbox->GetString(sel);
renamed = wxGetTextFromUser(wxT("新项目名称"),
wxT("重全名"), text);
}
if (!renamed.IsEmpty())
{
listbox->Delete(sel);
listbox->Insert(renamed, sel);
}
}
#include <wx/wx.h>
#include <wx/listbox.h>
#include "simple.h"
class MyPanel:public wxPanel
{
public:
MyPanel(wxPanel* parent);
protected:
void btnNew_Click(wxCommandEvent& event);
void btnRename_Click(wxCommandEvent& event);
void btnClear_Click(wxCommandEvent& event);
void btnDel_Click(wxCommandEvent& event);
wxListBox* m_lbData;
wxButton* btnNew;
wxButton* btnRename;
wxButton* btnClear;
wxButton* btnDel;
};
MyPanel::MyPanel(wxPanel* parent)
:wxPanel(parent,wxID_ANY)
{
wxBoxSizer* vbox = new wxBoxSizer(wxVERTICAL);
Simple* simple = (Simple*)parent->GetParent();
m_lbData = simple->listbox;
btnNew=new wxButton(this,-1,"New");
btnDel=new wxButton(this,-1,"Del");
btnRename=new wxButton(this,-1,"Rename");
btnClear=new wxButton(this,-1,"Clear");
vbox->Add(-1,20);
vbox->Add(btnNew,0,wxTOP,5);
vbox->Add(btnRename,0,wxTOP,5);
vbox->Add(btnDel,0,wxTOP,5);
vbox->Add(btnClear,0,wxTOP,5);
Bind(wxEVT_COMMAND_BUTTON_CLICKED,MyPanel::btnNew_Click,this,btnNew->GetId());
Bind(wxEVT_COMMAND_BUTTON_CLICKED,MyPanel::btnRename_Click,this,btnRename->GetId());
Bind(wxEVT_COMMAND_BUTTON_CLICKED,MyPanel::btnDel_Click,this,btnDel->GetId());
Bind(wxEVT_COMMAND_BUTTON_CLICKED,MyPanel::btnClear_Click,this,btnClear->GetId());
m_lbData->Append(wxT("测试1"));
m_lbData->Append(wxT("测试2"));
m_lbData->Append(wxT("测试3"));
m_lbData->Append(wxT("测试4"));
m_lbData->Append(wxT("测试5"));
m_lbData->Append(wxT("测试6"));
SetSizer(vbox);
}
void MyPanel::btnNew_Click(wxCommandEvent& event)
{
wxString str = wxGetTextFromUser(wxT("项目名称"),wxT("增加新项"));
if(str.length()>0){
m_lbData->Append(str);
}
}
void MyPanel::btnDel_Click(wxCommandEvent& event)
{
int sel = m_lbData->GetSelection();
if(sel != -1) m_lbData->Delete(sel);
}
void MyPanel::btnClear_Click(wxCommandEvent& event)
{
m_lbData->Clear();
}
void MyPanel::btnRename_Click(wxCommandEvent& event)
{
wxString text,renamed;
int sel = m_lbData->GetSelection();
if (sel != -1) {
text = m_lbData->GetString(sel);
renamed = wxGetTextFromUser(wxT("新项目名称"),
wxT("重全名"), text);
}
if (!renamed.IsEmpty())
{
m_lbData->Delete(sel);
m_lbData->Insert(renamed, sel);
}
}
这篇关于30、wxWidgets小部件之wxListBox的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!