// MenuDemoWithToolbarApplication.cs Copyright (c) Kari Laitinen // http://www.naturalprogramming.com // 2008-03-02 File created. // 2022-12-20 Tested in Developer Command Prompt for VS 2022. // This program is a modified version of MenuDemoApplication.cs. // This version provides a toolbar in addition to the menus. // Some activities that can be carried out through the menus // can alternatively be carried out by pressing the buttons // on the toolbar. // This program works in the same way as the corresponding // Java and Python programs, i.e., MenuDemoWithToolbarApplication.java // and MenuDemoWithToolbarGTK.py. // More notes at the end of this file. using System ; using System.Windows.Forms ; using System.Drawing ; class MenuDemoWithToolbarForm : Form { const int WINDOW_WIDTH = 800 ; const int WINDOW_HEIGHT = 480 ; MenuItem cyan_background_menu_item ; MenuItem small_text_menu_item, medium_text_menu_item, large_text_menu_item ; String last_selected_menu_item = "NO SELECTIONS MADE." ; ToolBar toolbar_of_this_application = new ToolBar() ; ToolBarButton toolbar_quit_button = new ToolBarButton( "Quit") ; ToolBarButton toolbar_cyan_background_button = new ToolBarButton( "CyanBackground" ) ; ToolBarButton toolbar_small_text_button = new ToolBarButton( "Small" ) ; ToolBarButton toolbar_medium_text_button = new ToolBarButton( "Medium" ) ; ToolBarButton toolbar_large_text_button = new ToolBarButton( "Large" ) ; public MenuDemoWithToolbarForm() { BackColor = Color.White ; ClientSize = new Size( WINDOW_WIDTH, WINDOW_HEIGHT ) ; Text = "C# GUI APPLICATION WITH MENUS AND TOOLBAR" ; // Window title MainMenu main_menu_of_this_window = new MainMenu() ; MenuItem file_menu = main_menu_of_this_window.MenuItems.Add( "&File" ) ; file_menu.MenuItems.Add( "&Open", new EventHandler( file_open_selection_made ) ) ; file_menu.MenuItems.Add( "&Save", new EventHandler( file_save_selection_made ) ) ; file_menu.MenuItems.Add( "E&xit", new EventHandler( file_exit_selection_made ) ) ; MenuItem edit_menu = main_menu_of_this_window.MenuItems.Add( "Edit" ) ; edit_menu.MenuItems.Add( "Copy", new EventHandler( edit_copy_selection_made ) ) ; edit_menu.MenuItems.Add( "Paste", new EventHandler( edit_paste_selection_made ) ) ; MenuItem settings_menu = main_menu_of_this_window.MenuItems.Add( "Settings" ) ; cyan_background_menu_item = new MenuItem( "Cyan background", new EventHandler( cyan_background_selection_made ) ) ; settings_menu.MenuItems.Add( cyan_background_menu_item ) ; small_text_menu_item = new MenuItem( "Small", new EventHandler( text_size_selection_made ) ) ; medium_text_menu_item = new MenuItem( "Medium", new EventHandler( text_size_selection_made ) ) ; large_text_menu_item = new MenuItem( "large", new EventHandler( text_size_selection_made ) ) ; medium_text_menu_item.Checked = true ; // This is selected by default. small_text_menu_item.RadioCheck = true ; medium_text_menu_item.RadioCheck = true ; large_text_menu_item.RadioCheck = true ; // The following statement builds a submenu for the Settings menu. MenuItem text_size_menu = settings_menu.MenuItems.Add( "Text size" ) ; text_size_menu.MenuItems.Add( small_text_menu_item ) ; text_size_menu.MenuItems.Add( medium_text_menu_item ) ; text_size_menu.MenuItems.Add( large_text_menu_item ) ; MenuItem help_menu = main_menu_of_this_window.MenuItems.Add( "Help" ) ; help_menu.MenuItems.Add( "About", new EventHandler( help_about_selection_made ) ) ; // Finally, we'll attach the main menu to the form Menu = main_menu_of_this_window ; toolbar_of_this_application.Buttons.Add( toolbar_quit_button ) ; toolbar_of_this_application.Buttons.Add( toolbar_cyan_background_button ) ; toolbar_of_this_application.Buttons.Add( toolbar_small_text_button ) ; toolbar_of_this_application.Buttons.Add( toolbar_medium_text_button ) ; toolbar_of_this_application.Buttons.Add( toolbar_large_text_button ) ; // A single method reacts to all clickings of the ToolBar buttons. toolbar_of_this_application.ButtonClick += new ToolBarButtonClickEventHandler( some_toolbar_button_clicked ) ; Controls.Add( toolbar_of_this_application ) ; // Add toolbar to this form. } // The following methods react to the menu selections. // In most cases the only reaction is that the text that // appears on the screen is changed. private void file_open_selection_made( Object sender, EventArgs event_data ) { last_selected_menu_item = "Open" ; Invalidate() ; } private void file_save_selection_made( Object sender, EventArgs event_data ) { last_selected_menu_item = "Save" ; Invalidate() ; } private void file_exit_selection_made( Object sender, EventArgs event_data ) { Close() ; // This terminates the program. } private void edit_copy_selection_made( Object sender, EventArgs event_data ) { last_selected_menu_item = "Copy" ; Invalidate() ; } private void edit_paste_selection_made( Object sender, EventArgs event_data ) { last_selected_menu_item = "Paste" ; Invalidate() ; } private void cyan_background_selection_made( Object sender, EventArgs event_data ) { if ( cyan_background_menu_item.Checked == true ) { cyan_background_menu_item.Checked = false ; BackColor = Color.White ; } else { cyan_background_menu_item.Checked = true ; BackColor = Color.Cyan ; } last_selected_menu_item = "Cyan background" ; Invalidate() ; } // The following method reacts to all text size selections. private void text_size_selection_made( Object sender, EventArgs event_data ) { if ( sender == small_text_menu_item ) { small_text_menu_item.Checked = true ; medium_text_menu_item.Checked = false ; large_text_menu_item.Checked = false ; } else if ( sender == medium_text_menu_item ) { small_text_menu_item.Checked = false ; medium_text_menu_item.Checked = true ; large_text_menu_item.Checked = false ; } else if ( sender == large_text_menu_item ) { small_text_menu_item.Checked = false ; medium_text_menu_item.Checked = false ; large_text_menu_item.Checked = true ; } last_selected_menu_item = "Text size item." ; Invalidate() ; } // Help -> About selection brings a message dialog to the screen. private void help_about_selection_made( Object sender, EventArgs event_data ) { MessageBox.Show( "This is a simple GUI application" + "\nwith menus.", "About" ) ; } // The following method will be called when any of the toolbar // buttons is pressed. This method finds out which button is // in questions, and calls the corresponding menu-related // methods. This is possible since the "meaning" of a toolbar // button is the same as the "meaning" of a menu item. private void some_toolbar_button_clicked( Object sender, ToolBarButtonClickEventArgs event_data ) { if ( event_data.Button == toolbar_quit_button ) { file_exit_selection_made( sender, event_data ) ; } else if ( event_data.Button == toolbar_cyan_background_button ) { cyan_background_selection_made( sender, event_data ) ; } else if ( event_data.Button == toolbar_small_text_button ) { text_size_selection_made( small_text_menu_item, event_data ) ; } else if ( event_data.Button == toolbar_medium_text_button ) { text_size_selection_made( medium_text_menu_item, event_data ) ; } else if ( event_data.Button == toolbar_large_text_button ) { text_size_selection_made( large_text_menu_item, event_data ) ; } } protected override void OnPaint( PaintEventArgs paint_data ) { Graphics graphics = paint_data.Graphics ; Font font_for_text_line = null ; if ( small_text_menu_item.Checked == true ) { font_for_text_line = new Font( FontFamily.GenericSerif, 10 ) ; } else if ( medium_text_menu_item.Checked == true ) { font_for_text_line = new Font( FontFamily.GenericSerif, 12 ) ; } else // must be that large_text_menu_item.Checked == true { font_for_text_line = new Font( FontFamily.GenericSerif, 16 ) ; } graphics.DrawString( "Last selected menu item: \"" + last_selected_menu_item + "\"", font_for_text_line, new SolidBrush( Color.Black ), 100, 200 ) ; } } public class MenuDemoWithToolbarApplication { static void Main() { Application.Run( new MenuDemoWithToolbarForm() ) ; } } /* NOTE: If you like to have so called 'tool tips' connected to the toolbar buttons, you can do it with the following statements: toolbar_quit_button.ToolTipText = "Exit this program and close window." ; toolbar_cyan_background_button.ToolTipText = "Change background color" ; toolbar_small_text_button.ToolTipText = "Small text size" ; toolbar_medium_text_button.ToolTipText = "Medium text size" ; toolbar_large_text_button.ToolTipText = "Large text size" ; */