Forums

Winforms Example

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

Winforms Example

Postby Charles » Mon Feb 11, 2008 12:28 pm

"""
To compile:
cobra -c -t:winexe -r:System.Windows.Forms winforms.cobra

To run:
winforms

Or leave out the "-c" to compile and run in one shot.

Recommended reading:
Programming Microsoft Windows Forms
by Charles Petzold

Cobra tips:
* Combine enums: AnchorStyle(Left, Right)
* Hook up events: listen someObj.someEvent, ref .myMethod
"""

use System.Windows.Forms

class MyForm
inherits Form

def init
.text = 'Click Me'
listen .click, ref .handleClick

def handleClick(sender as Object, args as EventArgs)
MessageBox.show('You clicked me!', 'Click')


class Program

def main is shared
Application.run(MyForm())


Note that the Petzold book has extensive information on how to build GUIs by hand and take advantage of anchors, docking, autosizing, etc.

The current Cobra weakness that I know of is that if in the "listen" statement you try to "ref" an overloaded method you will get a not-so-helpful error message. If you break the overloading by changing the method name that handles the event then all is well.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Winforms Example

Postby miked » Mon Feb 11, 2008 5:23 pm

Thanks Chuck. It works great.
miked
 
Posts: 5

Re: Winforms Example

Postby miked » Tue Feb 12, 2008 12:20 am

Hi Chuck,

When I try to add a button to the form (see below), I get:
"error: Cannot find a definition for "controls" in "this" whose type is "MyForm".
I've tried various combinatons of capitalization. Nothing works. Any ideas?

[cobra]
use System.Windows.Forms

class MyForm
inherits Form

def init
.text = 'CitrusTree'
listen .click, ref .handleClick

def handleClick( sender as Object, args as EventArgs )
btn = Button()
.controls.add( btn )
MessageBox.show('You clicked me!', 'Click')

class Program
def main is shared
Application.run( MyForm() )
\[cobra]
miked
 
Posts: 5

Re: Winforms Example

Postby Charles » Tue Feb 12, 2008 12:42 am

I have the same problem. I haven't tracked it down yet, but my current guess is that Cobra is not following the inheritance chain of library classes when looking for properties. Yeah that sounds kind of crazy, but it's got to be something. :D

I'll let you know what I find.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Winforms Example

Postby Charles » Tue Feb 12, 2008 1:01 am

Okay scratch that idea. My next best, more educated guess is that there is bug in how Cobra reads DLLs. But I have a neat workaround which I tested:
(this to dynamic).controls.add(btn)

What you're doing here is bypassing the compiler with dynamic typing. Now it's the Cobra run-time support for dynamic binding that does the work of looking up "controls" and invoking "add".

When I fix this, you'll be able to axe "(this to dynamic)". You could make a "def add" util method to encapsulate this.

Sorry for the inconvenience.

-Chuck
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Winforms Example

Postby Charles » Tue Feb 12, 2008 1:47 am

Yep this is all about reading DLLs. I was having a problem with nested types and as a temporary measure I skipped them. Then I forgot about them because until we tried to access .controls I didn't experience any problems! Exclamation mark for the > 500 test cases and 20KLOC compiler that works without them.

This is fixable, but not a small fix. You'll have to use the "to dynamic" workaround for now.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Winforms Example

Postby miked » Tue Feb 12, 2008 2:22 am

Yup, that fixed it. Thanks! (Talk about quick service!)

BTW, did I see somewhere that you'll be speaking on Cobra in Culver City on Feb. 28th? Will you be posting an announcement of this? I'm rather excited about Cobra, so I might make the trip down from the Bay Area to hear your talk and visit family if you'll be talking.
miked
 
Posts: 5

Re: Winforms Example

Postby Charles » Tue Feb 12, 2008 2:52 am

Yes, I will speaking on the 28th in Culver. I'll certainly be flattered if you schedule a family visit to coincide with it.

I don't know yet how much time they have carved out for me. Let me find out. Of course, we can spend time outside the meeting talking about Cobra and other things tech.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: Winforms Example

Postby miked » Tue Feb 12, 2008 3:48 am

Sounds good. I'm starting up a desktop app project and I've been exploring a bunch of .Net languages: LSharp, IronPython, IronScheme, IronRuby, F#, C#. I'm going to go with Cobra, unless I hit any bad bumps. If you can write a compiler in it it should be mature enough to get started in.

BTW, the contracts and unit tests really come through here, since they lower the risk factor to adopting a new, unproven language. So does the C# code gen. And then, you're eating your own dogfood with the compiler. Taken together, they serve as a kind of safety net. I don't want to get three months into this project and discover a showstopper in the language.
miked
 
Posts: 5

Re: Winforms Example

Postby Charles » Tue Feb 12, 2008 11:24 am

Cool. There are obviously still some rough spots but with over 500 regression test programs and the other factors you mentioned, it should be alright.
Charles
 
Posts: 2515
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 15 guests