Handling CheckBox state changes in DLangUI

Daren Scot Wilson darenw at darenscotwilson.com
Sat Dec 31 02:40:49 UTC 2022


I'm writing a GUI program using dlangui. It has some checkboxes. 
I'm trying to figure out how to invoke a callback function when 
the user clicks the box. What are the valid ways of doing that?

I can copy from dlangide's source, where a delegate is defined 
in-line and assigned. That seems to work.  But is that the only 
way?


         bool g_x = true;

         bool checkbox_b_clicked(Widget source, bool checked)
         {
           g_x = checked;
           if (checked) {
               writeln(checked);
           }
           return true;
         }


         auto check_a = new CheckBox("wantalt", "Alternating"d);
         auto check_b = new CheckBox("wantblinkb", "Blink(delg)"d);
         auto check_c = new CheckBox("wantblinkc", 
"Blink(direct)"d);

         check_a.checkChange = delegate(Widget w, bool checked) {
                      g_x=checked;
                      return true;
                      };
         check_b.checkChange = delegate(Widget w, bool checked) {
                      return checkbox_b_clicked(w,checked);
                      };
         check_c.checkChange = checkbox_b_clicked;
         check_c.checkChange = &checkbox_b_clicked;

The assignment to check_a is fine with the compiler.

For check_b, I try calling a function defined earlier. (Maybe in 
real life it's too complex to try having inline.)  It was giving 
a compiler error until I realized I'm dumb, wasn't passing 'w' 
and 'checked' to it. Fixed, works fine now. Okay!

But what I think I should be able to do: assign 
checkbox_b_clicked directly to the .checkChange property of the 
checkbox, as shown for check_c.  It doesn't work. Oh, I see an 
example where '&' is used - okay let's try that... nope!

The compiler errors I get are, for no '&' and with '&':

Error: function `app.checkbox_b_clicked(Widget source, bool 
checked)` is not callable using argument types `()`

Error: none of the overloads of `opAssign` are callable using 
argument types `(bool function(Widget source, bool checked))`




More information about the Digitalmars-d-learn mailing list