{"id":4066,"date":"2022-01-06T10:08:33","date_gmt":"2022-01-06T10:08:33","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=4066"},"modified":"2022-01-06T10:08:34","modified_gmt":"2022-01-06T10:08:34","slug":"8c-wxwidgets-wxtogglebutton","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2022\/01\/06\/8c-wxwidgets-wxtogglebutton\/","title":{"rendered":"8c.wxWidgets &#8211; wxToggleButton"},"content":{"rendered":"\n<p><strong>OVERVIEW<\/strong><\/p>\n\n\n\n<p>wxToggleButtons are like checkboxes in that they represent two states- true or false. Except here, they generate a command event every time since they are buttons.<\/p>\n\n\n\n<p>The sample code below shows 3 toggle buttons and a label which shows the state of the each of the 3 toggle buttons.<\/p>\n\n\n\n<p><strong>SAMPLE CODE<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">togglebutton.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    #include &lt;wx\/wx.h&gt;\n#endif\n#include &lt;wx\/tglbtn.h&gt;\n\nclass ToggleButton: public wxFrame {\n\n    public:\n      ToggleButton(const wxString &amp;title);\n      void onClick(wxCommandEvent &amp;event);\n\n    private:\n      wxToggleButton *mBtn1, *mBtn2, *mBtn3;\n      wxStaticText *mLbl;\n\n};\n\n\n<\/pre><\/div>\n\n\n<pre class=\"wp-block-preformatted\">togglebutton.cpp<\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &quot;togglebutton.h&quot;\n\n\nToggleButton::ToggleButton(const wxString &amp;title):\n\n    wxFrame(NULL, -1, title, wxDefaultPosition, wxSize(500,200)) {\n \n    wxPanel *panel = new wxPanel(this, -1);    \n    wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);\n    panel-&gt;SetSizer(vbox);\n\n    mBtn1 = new wxToggleButton(panel, -1, wxString(&quot;1&quot;));\n    mBtn2 = new wxToggleButton(panel, -1, wxString(&quot;2&quot;));\n    mBtn3 = new wxToggleButton(panel, -1, wxString(&quot;3&quot;));\n    mLbl = new wxStaticText(panel, -1, wxString(&quot;&quot;));\n\n    vbox-&gt;Add(mBtn1, 1, wxEXPAND);\n    vbox-&gt;Add(mBtn2, 1, wxEXPAND);\n    vbox-&gt;Add(mBtn3, 1, wxEXPAND);\n    vbox-&gt;Add(mLbl, 1, wxEXPAND);\n\n    mBtn1-&gt;Bind(wxEVT_TOGGLEBUTTON, &amp;ToggleButton::onClick, this);\n    mBtn2-&gt;Bind(wxEVT_TOGGLEBUTTON, &amp;ToggleButton::onClick, this);\n    mBtn3-&gt;Bind(wxEVT_TOGGLEBUTTON, &amp;ToggleButton::onClick, this);\n    Center();\n\n}\n\nvoid ToggleButton::onClick(wxCommandEvent &amp;event) {\n\twxString label = wxT(&quot;&quot;);\n\tif (mBtn1-&gt;GetValue())\n\t\tlabel.Append(&quot;1=toggled &quot;);\n\tif (mBtn2-&gt;GetValue())\n\t\tlabel.Append(&quot;2=toggled &quot;);\n\tif (mBtn3-&gt;GetValue())\n\t\tlabel.Append(&quot;3=toggled &quot;);\n\t\t\n \tmLbl-&gt;SetLabel(label);\t\n}\n\n\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    #include &lt;wx\/wx.h&gt;\n#endif\n\nclass Main: public wxApp {\n\n\tpublic:\n\t        virtual bool OnInit();\n};\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;togglebutton.h&quot;\n \nIMPLEMENT_APP(Main)\n\nbool Main::OnInit() {\n\n    ToggleButton *app = new ToggleButton(wxT(&quot;ToggleButton&quot;));\n    app-&gt;Show(true);\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=\"200\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-06-15-36-46.png\" alt=\"\" class=\"wp-image-4069\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-06-15-36-46.png 500w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-06-15-36-46-300x120.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=\"200\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-06-15-37-02.png\" alt=\"\" class=\"wp-image-4070\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-06-15-37-02.png 500w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2022\/01\/Screenshot-from-2022-01-06-15-37-02-300x120.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OVERVIEW wxToggleButtons are like checkboxes in that they represent two states- true or false. Except here, they generate a command event every time since they <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2022\/01\/06\/8c-wxwidgets-wxtogglebutton\/\" title=\"8c.wxWidgets &#8211; wxToggleButton\">[&#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-4066","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\/4066","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=4066"}],"version-history":[{"count":3,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4066\/revisions"}],"predecessor-version":[{"id":4071,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/4066\/revisions\/4071"}],"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=4066"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=4066"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=4066"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}