GTK#

Menu widgets

Example

C# example

// menu.cs - GTK# Tutorial example
//
// Authors: Ugo Albarello <ugo.albarello@007mundo.com>
//
// Ported from Menu Widget example for GTK+ 2.0 Tutorial

namespace GtkSharpTutorial {

	using GLib;
	using Gtk;
	using GtkSharp;
	using System;
		
	public class MenuSample {
		private static Menu menu;
		static void Main()
		{
			Application.Init ();
	
			/* create a new window */
			Window window = new Window("GTK# Menu Test");
			window.SetDefaultSize (200, 100);
			window.DeleteEvent += new DeleteEventHandler (delete_event);
			
	
			/* Init the menu-widget, and remember -- never
			* use the Show() method in the menu widget!! 
			* This is the menu that holds the menu items, the one that
			* will pop up when you click on the "Root Menu" in the app */
			menu = new Menu();
	
			/* Next we make a little loop that makes three menu-entries for "test-menu".
			* Notice the call to Append().  Here we are adding a list of
			* menu items to our menu.  */
	
			for (int i=0; i<3; i++)
				{
					/* Copy the names to the buf. */
					String buf = "Test-undermenu - " + i.ToString();
					
					/* Create a new menu-item with a name... */
					MenuItem menu_items = new MenuItem(buf);
					
					/* ...and add it to the menu. */
					menu.Append (menu_items);
					
					/* Do something interesting when the menuitem is selected */
					menu_items.Activated += new EventHandler (menuitem_response);
				}
	
			/* This is the root menu, and will be the label
			* displayed on the menu bar.  There won't be a signal handler attached,
			* as it only pops up the rest of the menu when pressed. */
			MenuItem root_menu = new MenuItem("Root Menu");
		
			/* Now we specify that we want our newly created "menu" to be the menu
			* for the "root menu" */
			root_menu.Submenu = menu;
	
			/* A vbox to put a menu and a button in: */
			VBox vbox = new VBox(false, 0);
			window.Add (vbox);
	
			/* Create a menu-bar to hold the menus and add it to our main window */
			MenuBar menu_bar = new MenuBar();
			vbox.PackStart(menu_bar, false, false, 2);
			
			/* Create a button to which to attach menu as a popup */
			Button button = new Button ("press me");
			button.Clicked += new EventHandler (button_press);
			vbox.PackEnd(button, true, true, 2);
			
			/* And finally we append the menu-item to the menu-bar -- this is the
			* "root" menu-item I have been raving about =) */
			menu_bar.Append (root_menu);
			
			/* always display the window as the last step so it all splashes on
			* the screen at once. */
			window.ShowAll ();
			
			Application.Run ();
		}
	
		static void delete_event (object obj, DeleteEventArgs args)
		{
			Application.Quit ();
		}
	
		/* Respond to a button-press by posting a menu passed */
	
		static void button_press (object obj, EventArgs args)
		{
			menu.Popup(null, null, null, IntPtr.Zero, 0, 0);
		}
		
		/* Print a string when a menu item is selected */
		
		static void menuitem_response(object obj, EventArgs args)
		{
			Console.WriteLine("menuitem_response " + obj.Name);
		}
	}


}


MonoBasic example