{"id":4098,"date":"2022-01-12T17:23:09","date_gmt":"2022-01-12T17:23:09","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=4098"},"modified":"2022-01-12T17:23:11","modified_gmt":"2022-01-12T17:23:11","slug":"9a-wxwidgets-wxlistbox","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2022\/01\/12\/9a-wxwidgets-wxlistbox\/","title":{"rendered":"9a. wxWidgets &#8211; wxListBox"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p>A wxListBox is a list which displays strings as items. It can select either a single item or multiple items. In the code below, we start with an empty wxListBox and then use buttons to add\/edit\/delete items .<\/p>\n\n\n\n<p><strong>SAMPLE CODE<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">wxlistbox.h<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;wx\/wxprec.h&gt;\n#ifndef WX_PRECOMP\n\t#include &lt;wx\/wx.h&gt;\n#endif\n\nclass ListBox: public wxFrame {\n\tpublic:\n\t\tListBox(const wxString &amp;title);\t\n\t\tvoid onAdd(wxCommandEvent &amp;event);\n\t\tvoid onEdit(wxCommandEvent &amp;event);\n\t\tvoid onDelete(wxCommandEvent &amp;event);\n\t\tvoid onClear(wxCommandEvent &amp;event);\n\n\tprivate:\n\t\twxListBox *mList;\t\n};\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">wxlistbox.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;wxlistbox.h&quot;\n\nListBox::ListBox(const wxString &amp;title):\n\twxFrame(NULL, -1, title, wxDefaultPosition, wxSize(500,250)) {\n\n\twxPanel *basePanel = new wxPanel(this,-1);\n        wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);\n\tbasePanel-&gt;SetSizer(hbox);\n\n\twxPanel *leftPanel = new wxPanel(basePanel, -1);\t\n\twxBoxSizer *vbox1 = new wxBoxSizer(wxVERTICAL);\n\tleftPanel-&gt;SetSizer(vbox1);\n\n\twxPanel *rightPanel = new wxPanel(basePanel, -1);\t\n\twxBoxSizer *vbox2 = new wxBoxSizer(wxVERTICAL);\n\trightPanel-&gt;SetSizer(vbox2);\n\n\tmList = new wxListBox(leftPanel,-1, wxDefaultPosition, wxDefaultSize);\n\tvbox1-&gt;Add(mList, 1, wxEXPAND);\n\n\twxButton *btnAdd = new wxButton(rightPanel, -1, wxT(&quot;Add&quot;));\n\twxButton *btnEdit = new wxButton(rightPanel, -1, wxT(&quot;Edit&quot;));\n\twxButton *btnDelete = new wxButton(rightPanel, -1, wxT(&quot;Delete&quot;));\n\twxButton *btnClear = new wxButton(rightPanel, -1, wxT(&quot;Clear&quot;));\n\n\tbtnAdd-&gt;Bind(wxEVT_BUTTON, &amp;ListBox::onAdd, this);\n\tbtnEdit-&gt;Bind(wxEVT_BUTTON, &amp;ListBox::onEdit, this);\n\tbtnDelete-&gt;Bind(wxEVT_BUTTON, &amp;ListBox::onDelete, this);\n\tbtnClear-&gt;Bind(wxEVT_BUTTON, &amp;ListBox::onClear, this);\n\t\n\tvbox2-&gt;Add(btnAdd, 1, wxEXPAND | wxALL, 5);\n\tvbox2-&gt;Add(btnEdit, 1, wxEXPAND | wxALL, 5);\n\tvbox2-&gt;Add(btnDelete, 1, wxEXPAND | wxALL, 5);\n\tvbox2-&gt;Add(btnClear, 1, wxEXPAND | wxALL, 5);\n\n\thbox-&gt;Add(leftPanel, 2, wxEXPAND | wxALL, 5);\n\thbox-&gt;Add(rightPanel, 1, wxEXPAND | wxALL, 5);\n\n\tCenter();\n}\n\nvoid ListBox::onAdd(wxCommandEvent &amp;event) {\n\twxString string = wxGetTextFromUser(wxT(&quot;Add new Item&quot;));\n\tif (string != &quot;&quot;) {\n\t\tmList-&gt;Append(string);\n\t}\n}\nvoid ListBox::onEdit(wxCommandEvent &amp;event) {\n\tint selected = mList-&gt;GetSelection();\n\tif (selected == -1) {\n\t  wxMessageDialog *dlg = new wxMessageDialog(NULL, wxT(&quot;No item has been selected in the listbox&quot;), wxT(&quot;&quot;));\n\t  dlg-&gt;ShowModal();\n\t  return;\n\t}\n\twxString selString = mList-&gt;GetString(selected);\n\twxString changed = wxGetTextFromUser(wxT(&quot;Edit&quot;), wxT(&quot;&quot;), selString);\n\tif (changed != &quot;&quot;) {\n\t\tmList-&gt;Delete(selected);\n\t\tmList-&gt;Insert(changed, selected);\n\t}\n}\n\nvoid ListBox::onDelete(wxCommandEvent &amp;event) {\n\tint selected = mList-&gt;GetSelection();\n\tif (selected == -1) {\n\t  wxMessageDialog *dlg = new wxMessageDialog(NULL, wxT(&quot;No item has been selected in the listbox&quot;), wxT(&quot;&quot;));\n\t  dlg-&gt;ShowModal();\n\t  return;\n\t}\n\tmList-&gt;Delete(selected);\n\n}\n\nvoid ListBox::onClear(wxCommandEvent &amp;event) {\n\t wxMessageDialog* dial = new wxMessageDialog(NULL, wxT(&quot;Are you sure?&quot;), wxT(&quot;&quot;),\n\t\t\t\t wxYES_NO | wxNO_DEFAULT | wxICON_QUESTION);\n\t int retVal = dial-&gt;ShowModal();\n\t if (retVal == wxID_YES) {\n\t \tmList-&gt;Clear();\n\t }\n}\n\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">main.h<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;wx\/wxprec.h&gt;\n#ifndef WX_PRECOMP\n\t#include &lt;wx\/wx.h&gt;\n#endif\n\nclass Main: public wxApp {\n\n\tpublic:\n\t\tvirtual bool OnInit();\n};\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">main.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;main.h&quot;\n#include &quot;wxlistbox.h&quot;\n\nIMPLEMENT_APP(Main)\n\nbool Main::OnInit() {\n\tListBox *app = new ListBox(wxT(&quot;ListBox&quot;));\n\tapp-&gt;Show(true);\n\n}\n\n<\/pre><\/div>\n\n\n<p>The output is shown below<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"250\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-12-22-51-15.png\" alt=\"\" class=\"wp-image-4100\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-12-22-51-15.png 500w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-12-22-51-15-300x150.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"250\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-12-22-51-47.png\" alt=\"\" class=\"wp-image-4101\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-12-22-51-47.png 500w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-12-22-51-47-300x150.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"336\" height=\"184\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-12-22-51-59.png\" alt=\"\" class=\"wp-image-4102\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-12-22-51-59.png 336w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-12-22-51-59-300x164.png 300w\" sizes=\"auto, (max-width: 336px) 100vw, 336px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW A wxListBox is a list which displays strings as items. It can select either a single item or multiple items. In the code below, <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2022\/01\/12\/9a-wxwidgets-wxlistbox\/\" title=\"9a. wxWidgets &#8211; wxListBox\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":3595,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[302,365],"tags":[],"class_list":["post-4098","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cc","category-wxwidgets"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4098","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=4098"}],"version-history":[{"count":2,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4098\/revisions"}],"predecessor-version":[{"id":4103,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4098\/revisions\/4103"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/3595"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=4098"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=4098"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=4098"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}