# Issues adding sub-menu items

**URL:** <https://www.ruby-forum.com/t/issues-adding-sub-menu-items/120584>\
**Category:** wxRuby\
**Created:** [November 18, 2007, 9:30pm UTC](https://www.ruby-forum.com/t/issues-adding-sub-menu-items/120584 "2007-11-18T21:30:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![crazyhat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/dc4da7/32.png) [@crazyhat](https://www.ruby-forum.com/u/crazyhat)\
**Post date:** [November 18, 2007, 9:30pm UTC](https://www.ruby-forum.com/t/issues-adding-sub-menu-items/120584/1 "2007-11-18T21:30:19Z")

</div>

Hello All,

I’m new to the list and to Ruby.

I was trying to use one of the examples as a skeleton for something that  
I’m working on.

I’m currently stuck when it comes to adding another item to the  
sub-menus.  
I have a menu item by the name of “Tools” and I would like to add a  
sub-menu item called “Find Unique Fields” but it doesn’t show up when I  
try to run it.

If I add a Wx::ID\_UNIQUE and a handle for the event the program fails.

What do I need to do or what I’m I missing. I appreciate your response.

Thanks,

Glenn

#!/usr/bin/env ruby

begin  
…  
…  
…

class HL7Query \< Wx::Frame  
def initialize(title)

```
super(nil, :title => title, :size => [850, 600])

menu_bar = Wx::MenuBar.new

# The "file" menu
menu_file = Wx::Menu.new
menu_file.append(Wx::ID_EXIT, "E&xit\tAlt-X", "Quit this program")
menu_bar.append(menu_file, "&File")

# The "Tools" menu
menu_tools = Wx::Menu.new
#menu_tools.append(Wx::ID_UNIQUEFIELDS, "Find Unique Fields", "Find

```

Unique Fields")  
menu\_tools.append(nil, “Find Unique Fields”, “Find Unique Fields”)  
menu\_bar.append(menu\_tools, “Tools”)

```
# The "help" menu
menu_help = Wx::Menu.new
# Using Wx::ID_ABOUT default id means the menu item will be placed
# in the correct platform-specific place - eg on OS X
menu_help.append(Wx::ID_ABOUT, "&About...\tF1", "Show about dialog")
menu_bar.append(menu_help, "&Help")

# Assign the menubar to this frame
self.menu_bar = menu_bar

# Create a status bar
create_status_bar(2)
self.status_text = "Welcome to HL7Query!"

# Set it up to handle menu events using the relevant methods
evt_menu Wx::ID_EXIT, :on_quit
evt_menu Wx::ID_ABOUT, :on_about

```

end

def on\_quit  
close()  
end

…  
…  
…

Wx::App.run do  
self.app\_name = ‘HL7Query’  
frame = HL7Query.new(":::HL7Query:::")  
frame.show  
end

---

<div class="post-metadata">

**Author:** ![crazyhat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/dc4da7/32.png) [@crazyhat](https://www.ruby-forum.com/u/crazyhat)\
**Post date:** [November 19, 2007, 11:30am UTC](https://www.ruby-forum.com/t/issues-adding-sub-menu-items/120584/2 "2007-11-19T11:30:23Z")

</div>

Glenn Pringle wrote:

> I’m currently stuck when it comes to adding another item to the  
> sub-menus.  
> I have a menu item by the name of “Tools” and I would like to add a  
> sub-menu item called “Find Unique Fields” but it doesn’t show up when I  
> try to run it.
> 
> If I add a Wx::ID\_UNIQUE and a handle for the event the program fails.
> 
> What do I need to do or what I’m I missing. I appreciate your response.

Your example (the row that wasn’t commented out) had nil as the menu  
item id, which will fail. Either you have to supply your own valid ID  
value (starting from 100 going upwards is a good idea) or you use  
Wx::ID\_ANY as the ID and you’ll get an automatically assigned id (use  
get\_id for the returned menu item object to know the ID).

You have many ways to go here. Since you seem to choose to set up IDs  
yourself, first try a numerical value (for example 101) so you see how  
it works:  
menu\_tools.append(101, “Find Unique Fields”, “Find Unique  
Fields”)  
…and set up the menu item event handler…  
evt\_menu 101, :on\_unique\_fields  
…and then create the event handler method…  
def on\_unique\_fields  
p “on unique fields menu item selected”  
end

Now, replace the number 101 with ID\_UNIQUE:  
ID\_UNIQUE = 101  
…  
menu\_tools.append(ID\_UNIQUE, “Find Unique Fields”, “Find Unique Fields”)  
…  
evt\_menu ID\_UNIQUE, :on\_unique\_fields

Please note that the Wx:: prefix should NOT be used prior to your own  
constant names - you don’t want your own constants to interfere with the  
wxRuby name scope.

Best regards,

Jari W.

---

<div class="post-metadata">

**Author:** ![crazyhat](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/dc4da7/32.png) [@crazyhat](https://www.ruby-forum.com/u/crazyhat)\
**Post date:** [November 19, 2007, 11:44pm UTC](https://www.ruby-forum.com/t/issues-adding-sub-menu-items/120584/3 "2007-11-19T23:44:46Z")

</div>

Jari W. wrote:

> Glenn Pringle wrote:

> Please note that the Wx:: prefix should NOT be used prior to your own  
> constant names - you don’t want your own constants to interfere with the  
> wxRuby name scope.
> 
> Best regards,
> 
> Jari W.

Thanks
