Wiki

root/cobra/trunk/HowTo/380-WinForms.cobra

Revision 2344, 1.5 KB (checked in by Chuck.Esterbrook, 2 years ago)

Break out @ref and @args from the testify-only .args. directive. This is more realistic Cobra usage.

  • Property svn:eol-style set to native
Line 
1# .compile-only.
2# .args. -c
3"""
4To compile:
5cobra -c -t:winexe 380-WinForms.cobra
6
7To run:
8winforms
9
10Or leave out the "-c" to compile and run in one shot.
11
12Also, leave out -t:winexe and run from the command line to see the output of
13`print` and `trace` statements.
14
15Note that you don't have to explicitly reference the System.Windows.Forms.dll in
16the command line because the `use` clause below will automatically locate it.
17
18Recommended reading:
19
20    Programming Microsoft Windows Forms
21    by Charles Petzold
22
23    Pro .NET 2.0 Windows Forms
24    by Matthew MacDonald
25
26Cobra tips:
27    * Combine enums: AnchorStyle(Left, Right)
28    * Hook up events: listen someObj.someEvent, ref .myMethod
29    * In event handlers, leave out the type of the sender and you can use
30      dynamic typing to easily access the senders properties and methods.
31
32There is more substantial WinForms code in the source code for Cobra under
33Source/ObjectExplorer-WinForms.cobra.
34"""
35
36@args t:winexe
37
38use System.Windows.Forms
39
40
41class MyForm inherits Form
42
43    cue init
44        base.init
45        .text = 'Sample Form'
46        flp = FlowLayoutPanel(parent=this, dock=DockStyle.Fill, flowDirection=FlowDirection.TopDown)
47        b = Button(parent=flp, autoSize=true, text='One', tag=1)
48        listen b.click, ref .handleClick
49        b = Button(parent=flp, autoSize=true, text='Two', tag=2)
50        listen b.click, ref .handleClick
51
52    def handleClick(sender, args as EventArgs)
53        MessageBox.show(this, 'You clicked [sender.text]/[sender.tag]', 'Click')
54
55
56class Program
57
58    def main has STAThread
59        Application.run(MyForm())
Note: See TracBrowser for help on using the browser.