|
|
IronRuby > Documentation > .NET Integration > Events
EventsFrom $1Table of contentsNo headersThere are several ways to interact with Events in IronRuby. The most basic way is to add a code block to the event. >> require "System.Windows.Forms"
=> true
>> button = System::Windows::Forms::Button.new
=> System.Windows.Forms.Button, Text:
>> button.click { |sender, e| puts "Click!" }
=> #<Proc:0x0000204@(irb):3>
>> button.perform_click
Click!
=> nil The following is equivalent to the previous example. >> require "System.Windows.Forms" => true >> button = System::Windows::Forms::Button.new => System.Windows.Forms.Button, Text: >> button.click do |sender, e| ?> puts "Click!" >> end => #<Proc:0x0000216@(irb):3> >> button.perform_click Click! => nil You can also pass Events a Proc object if you need to store it to the side. This allows you to later unsubscribe from the event. >> require "System.Windows.Forms"
=> true
>> button = System::Windows::Forms::Button.new
=> System.Windows.Forms.Button, Text:
>> on_click = proc { |sender, e| puts "Click!" }
=> #<Proc:0x000020a@(irb):3>
>> button.click(&on_click)
=> #<Proc:0x000020a@(irb):3>
>> button.perform_click
Click!
=> nil
>> button.click.remove(on_click)
=> nil
>> button.perform_click
=> nil
Tags:
|