Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# 2008 Step by Step.pdf
Скачиваний:
26
Добавлен:
25.03.2016
Размер:
13.96 Mб
Скачать

Chapter 23 Working with Menus and Dialog Boxes

471

10.Using Windows Explorer, move to your Documents folder. Verify that the TestMember.txt file has been created.

11.Double-click the file, and verify that it contains the details of the member that you added. Close Notepad when you have finished.

You can use a similar technique for opening a file: create an OpenFileDialog object, activate it by using the ShowDialog method, and retrieve the FileName property when the method returns if the user has clicked the Open button. You can then open the file, read its contents, and populate the fields on the screen. For more details on using the OpenFileDialog class,

consult the MSDN Library for Visual Studio 2008.

If you want to continue to the next chapter

Keep Visual Studio 2008 running, and turn to Chapter 24.

If you want to exit Visual Studio 2008 now

On the File menu, click Exit. If you see a Save dialog box, click Yes (if you are using Visual Studio 2008) or Save (if you are using Microsoft Visual C# 2008 Express Edition) and save the project.

Chapter 23 Quick Reference

To

Do this

Create a menu for a form

Add a DockPanel control, and place it at the top of the form. Add a Menu

 

control to the DockPanel control.

 

 

Add menu items to a menu

Add MenuItem elements to the Menu control. Specify the text for a menu

 

item by setting the Header property, and give each menu item a name by

 

specifying the Name property. You can optionally specify properties so

 

that you can display features such as icons and child menus. You can add

 

an access key to a menu item by prefixing the appropriate letter with an

 

underscore character.

Create a separator bar in a menu

Add a Seperator element to the menu.

Enable or disable a menu item

Set the IsEnabled property to True or False in the Properties window at

 

design time, or write code to set the IsEnabled property of the menu item

 

to true or false at run time.

 

 

Perform an action when the

Select the menu item, and specify an event method for the Click event.

user clicks a menu item

Add your code to the event method.

 

 

Create a shortcut menu

Add a ContextMenu to the window resources. Add items to the shortcut

 

menu just as you add items to an ordinary menu.

 

 

Associate a shortcut menu

Set the ContextMenu property of the form or control to refer to the

with a form or control

shortcut menu.

 

 

472

Part IV Working with Windows Applications

 

 

Create a shortcut menu

Create a ContextMenu object. Populate the Items collection of this

dynamically

object with MenuItem objects defining each of the menu items. Set the

 

 

ContextMenu property of the form or control to refer to the shortcut

 

 

menu.

 

 

Prompt the user for the name

Use the SaveFileDialog class. Display the dialog box by using the

of a file to save

ShowDialog method. When the dialog box closes, the FileName property

 

 

of the SaveFileDialog instance contains the name of the file selected by

the user.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]