#include#include class MyFrame : public wxFrame { public: MyFrame() : wxFrame(NULL, wxID_ANY, "List Event Example") { wxListBox* lb = new wxListBox(this, wxID_ANY, wxPoint(10, 10), wxSize(150, 150), 0, NULL, wxLB_SINGLE); lb->Append("Item 1"); lb->Append("Item 2"); lb->Append("Item 3"); Bind(wxEVT_LISTBOX, &MyFrame::OnListEvent, this); } private: void OnListEvent(wxCommandEvent& evt) { wxString text = evt.GetString(); // Retrieve selected item text wxMessageBox(text, "Selected Item"); } }; wxIMPLEMENT_APP(MyApp); bool MyApp::OnInit() { MyFrame* frame = new MyFrame(); frame->Show(); return true; }
#includeIn this example, a wxLC_REPORT style is used to create a list control with columns. When an item is selected in the list control, the OnListEvent method is called and the selected item text is retrieved using the GetText method of the wxListEvent class. The retrieved text is then displayed in a message box using the wxMessageBox function. This example also uses the wxWidgets library.#include class MyFrame : public wxFrame { public: MyFrame() : wxFrame(NULL, wxID_ANY, "List Event Example") { wxListCtrl* lc = new wxListCtrl(this, wxID_ANY, wxPoint(10, 10), wxSize(300, 150), wxLC_REPORT); lc->InsertColumn(0, "Column 1"); lc->InsertColumn(1, "Column 2"); lc->InsertItem(0, "Row 1 - Column 1"); lc->SetItem(0, 1, "Row 1 - Column 2"); lc->InsertItem(1, "Row 2 - Column 1"); lc->SetItem(1, 1, "Row 2 - Column 2"); Bind(wxEVT_LIST_ITEM_SELECTED, &MyFrame::OnListEvent, this); } private: void OnListEvent(wxListEvent& evt) { wxString text = evt.GetText(); // Retrieve selected item text wxMessageBox(text, "Selected Item"); } }; wxIMPLEMENT_APP(MyApp); bool MyApp::OnInit() { MyFrame* frame = new MyFrame(); frame->Show(); return true; }