clojure-clr how to wire up a delegate

292 views
Skip to first unread message

adam11235

unread,
Feb 24, 2010, 1:43:48 AM2/24/10
to Clojure
Hi,

I've made progress in creating a simple app to show a windows form,
however I am having trouble wiring up a delegate (to handle button
clicks).

The Java version uses Proxy to implement ActionListener, instead I am
just trying to create an EventHandler passing as the 2nd constructor
argument the code I would like executed. (see the .add_Click line)

The delegate code gets invoked immediately instead of when the button
click occurs, and then complains it expected a function pointer rather
than the DialogResult it received (due to execution of the code)

I tried quoting that code but no success.

How do you wire up delegates?

(import '(System.Windows.Forms MessageBox Form Button))

(defn windowsPlay []
(let
[ win (Form.)
temp-button (Button.)
]
(.. win (get_Controls) (Add temp-button))
(doto temp-button
(.set_Top 50)
(.set_Text "Clicky")
(.add_Click (EventHandler. temp-button (MessageBox/Show "I got
clicked"))))
(doto win
(.set_Text "hello")
(.ShowDialog))))

(windowsPlay)

Thanks, Adam.

Joop Kiefte

unread,
Feb 24, 2010, 9:09:08 AM2/24/10
to clo...@googlegroups.com
Tried to encapsulate what you want to put in the eventhandler in an
anonymous function?

And I don't know how that works in Clojure-CLR, but you might need proxy...

2010/2/24 adam11235 <adam.k...@gmail.com>:

> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

--
Communication is essential. So we need decent tools when communication
is lacking, when language capability is hard to acquire...

- http://esperanto.net - http://esperanto-jongeren.nl

Linux-user #496644 (http://counter.li.org) - first touch of linux in 2004

Joop Kiefte

unread,
Feb 24, 2010, 9:09:50 AM2/24/10
to clo...@googlegroups.com
Forget about the proxy part, re-read your message... =x

2010/2/24 Joop Kiefte <iko...@gmail.com>:

soyrochus

unread,
Feb 24, 2010, 4:39:49 PM2/24/10
to Clojure
Hi Adam,

You need to use the gen-delegate macro to create delegates,

See http://wiki.github.com/richhickey/clojure-clr/clr-interop

The signature of the macro is (gen-delegate Type [args] body) whereby
in case of event-handlers you would typically use the EventHandler
class.
The code becomes then:

(System.Reflection.Assembly/LoadWithPartialName
"System.Windows.Forms")


(import '(System.Windows.Forms MessageBox Form Button))

(defn windowsPlay []
(let [win (Form.)
temp-button (Button.)]
(.. win (get_Controls) (Add temp-button))
(doto temp-button
(.set_Top 50)
(.set_Text "Clicky")

(.add_Click (gen-delegate EventHandler [sender args] (MessageBox/


Show "I got clicked"))))
(doto win
(.set_Text "hello")
(.ShowDialog))))

(windowsPlay)

Regards,

Iwan

adam11235

unread,
Feb 24, 2010, 6:25:59 PM2/24/10
to Clojure
Excellent that worked.

For other clojure-clr newbies, the Clojure.Source project has some
samples that are helpful.

Thanks, Adam.

On Feb 25, 7:39 am, soyrochus <iwanvanderkle...@gmail.com> wrote:
> Hi Adam,
>
> You need to use the gen-delegate macro to create delegates,
>

> Seehttp://wiki.github.com/richhickey/clojure-clr/clr-interop

dmiller

unread,
Feb 24, 2010, 9:21:37 AM2/24/10
to Clojure
Parallel to proxy, clojure-clr adds a gen-delegate. Example code is
in

http://github.com/richhickey/clojure-clr/blob/master/Clojure/Clojure.Source/clojure/samples/celsius.clj

Specifically, for adding an EventHandler:

(.add_Click button
(gen-delegate EventHandler [sender args]
(let [c (Double/Parse (.Text tb)) ]
(.set_Text f-label (str (+ 32 (* 1.8 c)) " Fahrenheit")))))

-David

Reply all
Reply to author
Forward
0 new messages