Chapter 5 Menus, Common Dialog Boxes, and MethodsMenusSlide 3SubmenusSubMenusSeparator BarsModifying Menu ItemsThe Enabled PropertyThe Checked PropertyChecked/Enabled PropertiesSetting Keyboard ShortcutsStandards for Windows MenusCommon Dialog BoxesDisplaying a Common Dialog BoxUsing the Information from the Dialog BoxCreating Context MenusContext MenusCreating Context Menus cont.Writing General MethodsCreating a New MethodCreating a New Method cont.Passing Arguments to MethodsMethod ParametersMethod Header FormatMethods with Multiple ArgumentsBreaking Calculations into Smaller UnitsPowerPoint PresentationChapter 5 Menus, Common Dialog Boxes, and MethodsProgramming in C# .NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.2MenusConsist of a menu bar containing menus with list of menu itemsUse menu items in place of or in addition to buttons to activate a methodEach Menu item is a control with properties and eventsCreate menus with the Visual Studio Menu Designer3Menus4SubmenusA new list that pops up under an item on a menu is called a submenuA filled triangle to the right of a menu item indicates a submenu for that menu itemCreate a submenu by moving to the right of a menu item and typing the next item’s text5SubMenus6Separator BarsA separator bar in a menu groups menu items according to their purposeTwo ways to create a separator barType a single hyphen (-) for the text orRight-click on Menu Designer at separator bar position and choose Insert SeparatorKeep the default Name property of the separator bar control7Modifying Menu ItemsUse the Menu Designer to modify menu itemsRight-click on the menu bar for options including Delete, Insert New (above), Insert Separator, Edit NamesDrag and drop menu items to rearrange8The Enabled PropertyThe Enabled property is true by defaultAn enabled menu item has black text and is available for selectionA disabled menu item is grayed out and not availableSet the Enabled property at design time or in code at run time9The Checked PropertyA menu item may contain a check mark next to it to indicate the option is currently selectedBy default, the Checked property is set to falseChange the Checked property at design time or in code at run time10Checked/Enabled Properties11Setting Keyboard ShortcutsCreate keyboard shortcuts for your menu itemsSelect the menu item in the designerSelect the Shortcut property in the Properties windowSelect choice from drop down listOnly work for lowest level item – ie. Items with submenusShowShortcut property is true by default12Standards for Windows MenusInclude keyboard access keys and stick with the standard key assignmentsPlace the File menu on the left end of the menu bar with an Exit command at the endIf there is a Help menu, it should be at the right end of the menu barAny menu item that will display a dialog box should have “…” appended to its Text property13Common Dialog BoxesCommon dialog controls are used to display dialog boxes that are provided with WindowsVisual Studio .NET supports the following common dialog controls- OpenFileDialog - SaveFileDialog- FontDialog - ColorDialog- PrintDialog - PrintPreviewDialog14Displaying a Common Dialog BoxUse the ShowDialog methodExample:dlgColor.ShowDialog();A modal dialog box stays on top of the application and must be responded toUse the ShowDialog methodA modeless dialog box does not require a responseUse the Show method15Using the Information from the Dialog BoxColor Dialog BoxThe color selected by user stored in the Color propertyFont Dialog BoxThe font selected by user stored in the Font propertyInitialize the Font or Color property to an existing valueExample: FontDialog1.Font = subTotalLabel.Font;16Creating Context MenusContext menus are the shortcut menus that pop up when you right-click on an objectItems on a context menu are generally specific to that objectAdd a ContextMenu control to the formClick on words Context Menu then Type Here to add menu items17Context Menus18Creating Context Menus cont.An application can have more than one context menuAssign the context menu to a form or control with the ContextMenu propertyIf there is only one context menu, attach it to the form19Writing General MethodsA general method contains reusable code that is called from other methodsThe method may return a value or notIf a value is returned, specify the return type before the method nameIf no value is returned, use the keyword void before the method name20Creating a New MethodWrite a method header and enclose the lines of code within a set of bracesExample:private void SelectColor(){//Display the color dialog boxColorDialog1.ShowDialog();}21Creating a New Method cont.You must specifically call the method from another method Example:private void changeButtonMessage_Click(object sender, System.EventArgs e){//Change the color of the messageSelectColor();messageLabel.ForeColor = ColorDialog1.Color;}22Passing Arguments to MethodsUse arguments when you need to use the value of a variable in one method and also in a second method that is called from the firstAny arguments defined in a method must be supplied in a call to that methodThe argument value must be the same data type in both locationsThe name of an argument does not have to be the same in both locations23Method Parameters24Method Header FormatMethod name Argument data typeReturn data type ArgumentAccess modifierprivate decimal Commission(decimal decSalesAmount)25Methods with Multiple ArgumentsThe sequence and data type of the arguments in the call must exactly match the arguments in the method headerThe list of zero or more arguments is enclosed in parenthesesWhen you call the method, the Visual Studio .NET smart editor displays the arguments of your method26Breaking Calculations into Smaller UnitsProjects with many calculations can be easier to understand and write if broken down into small unitsEach unit should perform one program function or block of
View Full Document