Forums

Defining and referring to GUI controls

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

Defining and referring to GUI controls

Postby ObtuseAngle » Tue Apr 29, 2014 7:26 pm

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())
99 programming bugs in the code, 99 bugs in the code, fix 1 bug and compile it again, 101 programming bugs in the code
ObtuseAngle
 
Posts: 42
Location: Gippsland, Australia

Re: Defining and referring to GUI controls

Postby Charles » Tue Apr 29, 2014 7:35 pm

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".
Charles
 
Posts: 2510
Location: Los Angeles, CA

Re: Defining and referring to GUI controls

Postby ObtuseAngle » Tue Apr 29, 2014 7:55 pm

Thank you Charles!
Hopefully it will grow more comfortable with familiarity through usage.
My thanks also for the tip about protected.
99 programming bugs in the code, 99 bugs in the code, fix 1 bug and compile it again, 101 programming bugs in the code
ObtuseAngle
 
Posts: 42
Location: Gippsland, Australia


Return to Discussion

Who is online

Users browsing this forum: No registered users and 8 guests

cron