# Registering custom events

**URL:** <https://www.ruby-forum.com/t/registering-custom-events/183557>\
**Category:** wxRuby\
**Created:** [February 16, 2010, 3:21pm UTC](https://www.ruby-forum.com/t/registering-custom-events/183557 "2010-02-16T15:21:19Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![idanmiller](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ecb155/32.png) [@idanmiller](https://www.ruby-forum.com/u/idanmiller)\
**Post date:** [February 16, 2010, 3:21pm UTC](https://www.ruby-forum.com/t/registering-custom-events/183557/1 "2010-02-16T15:21:19Z")

</div>

Hi everyone,

I’m using the following code to try and register custom events:

When the commented line is uncommented, I the program crashes.  
I’m running on windows xp.

Anyone know what am I doing wrong?

Thanks,  
Idan.

* * *

require ‘wx’

class CustomCommandEvent \< Wx::CommandEvent

end

class RecorderFrame \< Wx::Frame

def initialize(parent, id, title, hwnds, scriptFilename,  
fieldsFilename)  
super(parent, id, title)  
@hwnds = hwnds  
@scriptFilename = scriptFilename  
@fieldsFilename = fieldsFilename

```
layout = Wx::BoxSizer.new(Wx::VERTICAL)
set_sizer(layout)

@startButton = Wx::Button.new(self, Wx::ID_ANY, "Start")
@stopButton = Wx::Button.new(self, Wx::ID_ANY, "Stop")
@label = Wx::StaticText.new(self, Wx::ID_ANY, "Preparing...")

@startButton.disable
@stopButton.disable

evt_button @startButton, :startTracing
evt_button @stopButton, :stopTracing

layout.add(@startButton, 0, Wx::ALL|Wx::EXPAND, 1)
layout.add(@stopButton, 0, Wx::ALL|Wx::EXPAND, 1)
layout.add(@label, 0, Wx::ALL|Wx::EXPAND, 1)

evt_close { |event| onClose(event) }

evt_custom_commands { |event| onCustomCommand(event) }

show

```

end

def onCustomCommand(event)  
puts (“Received command event, id: #{event.inspect.to\_s}”)  
end

def onClose(event)  
if (event.can\_veto)  
#TODO: Constants  
confirm = Wx::MessageDialog.new(nil, “Are you sure you want to  
stop  
recording?”, “Stop Recording”, Wx::OK | Wx::CANCEL)  
case confirm.show\_modal  
when Wx::ID\_OK  
destroy  
when Wx::ID\_CANCEL  
event.veto  
end  
else  
destroy  
end  
end

def startTracing  
end

def stopTracing  
end  
end

class WebRecorderApp \< Wx::App  
attr\_reader :frame

def initialize(hwnds, scriptFilename, fieldsFilename)  
@hwnds = hwnds  
@scriptFilename = scriptFilename  
@fieldsFilename = fieldsFilename

```
 super()

```

end

def on\_init  
@frame = RecorderFrame.new(nil, Wx::ALL, “Test”, @hwnds,  
@scriptFilename, @fieldsFilename)  
end  
end

#Wx::EvtHandler.register\_class(CustomCommandEvent, nil,  
“evt\_custom\_commands”, 1)

app = WebRecorderApp.new(nil, nil, nil)  
app.main\_loop
