Page 1 of 1

Defining and referring to GUI controls

PostPosted: Tue Apr 29, 2014 7:26 pm
by ObtuseAngle
I have the feeling I'm not doing this right, although it might be only an unfamiliar itch where I'm used to Python not defining it out on its own like that.
What's the correct way to define a control e.g. chkTest below, in order to refer to it later e.g. .chkTest.checked - do I define it as a class variable as below, or is there a better way?


Code: Select all
use System.Windows.Forms

class MyForm inherits Form

    var chkTest as CheckBox

    cue init
        base.init
        .text = 'Simple sample'
        .width=180
        .height=70
        pnl = FlowLayoutPanel(parent=this, dock=DockStyle.Fill, flowDirection=FlowDirection.LeftToRight, autoSize=true)
        .chkTest = CheckBox(parent = pnl, autoSize=true, text='check me')
        btnTest = Button(autoSize=true, text='test')
        pnl.controls.add(btnTest)
        listen btnTest.click, ref .handleTestClick

    def handleTestClick(sender, args as EventArgs)
        MessageBox.show(this, 'Checkbox is checked: [.chkTest.checked]', 'Check the Checkbox')

class Program

    def main has STAThread
        Application.run(MyForm())

Re: Defining and referring to GUI controls

PostPosted: Tue Apr 29, 2014 7:35 pm
by Charles
That's correct. In Cobra, the instance variables are declared. They are typically typed as well for the various benefits that gets you, though it's optional.

I would typically make such vars "protected" so they cannot be accessed by outside classes. You can do that through the name such as "_chkTest" or through the modifier "protected".

Re: Defining and referring to GUI controls

PostPosted: Tue Apr 29, 2014 7:55 pm
by ObtuseAngle
Thank you Charles!
Hopefully it will grow more comfortable with familiarity through usage.
My thanks also for the tip about protected.