Visual Programming Menu: Muhammad Kamran
Visual Programming Menu: Muhammad Kamran
Menu
LAB 12
Muhammad kamran
Email: [email protected]
MENU
• MENU STRIP
• CONTENT MENU STRIP
Creating Top-Level Menus in C#
STEP 1
• So let's begin by opening up Visual Studio
2010 and create a new project. Let's create a
Windows Form Application and I named mine
DICdialogTutorial because I was just making it
for this tutorial but you came name it anything
you want.
4
STEP 2
• Now that we have our project open and can see the Windows Form let's look to the left and open the
Toolbox. If you look in the Toolbox you should see a section labeled Menus & Toolbars let's click that so
that the items will drop down. We are looking for the MenuStrip as shown in the picture below.
5
STEP 3
• Drag the MenuStrip over to your Form and position it at the top as shown in this picture.
6
STEP 4
• Once we have placed the MenuStrip in the Form we can then begin typing the words New, Open, Save, and Exit
into the blanks provided. Just type New into the blank then hit enter and another blank should appear so you
can type in Open. Continue until you have typed each one into the blanks provided as shown in the picture
below.
7
STEP 5
• As show in the next picture below, it's time to go back to the Toolbox. Click the triangle beside the
word Dialogs so that you can see the items in the Dialog dropdown. We're looking for OpenFileDialog
and the SaveFileDialog.
8
STEP 6
• Drag them both to the gray area that is below your form. If you're unsure where I'm talking about just
refer to the image below.
9
STEP 7
Now it's time to set up our shortcuts. Yes, shortcuts. In case you're going huh? what are you talking about. Here's what I mean. Go to the File menu
your Visual Studio you see how it beside the Save it tells you to hit Ctrl + S so you can save using the keyboard shortcuts. Well that's what we're abo
to do.
As you can see in the picture below when you go to the File menu and then click on New your properties window in the lower right hand corner
changes. You are now looking at the properties for that New command. Scroll down until you see the words ShortcutKeys click the drop down
beside that and click the checkmark beside Ctrl and then in the drop down for the keys click on N then hit enter. Go back up to your File menu and
you should see the shortcut added to your New command. Do this for each command in your file menu and your shortcuts are done. Here is what
most people use for the shortcuts:
New Ctrl + N
Open Ctrl + O
Save Ctrl + S
Exit Alt + F4
10
STEP 8
Now you should have shortcuts for each command. So let’s add a textbox to the
form that way we can have something to save and somewhere to put the text
when we open the file. Now let's get down to the coding. Double click each
command in the File menu so that your event handler appears in the code view.
Let's start with the New command:
In the code above, we take the text in the textbox and clear it by emptying the string. That
way when we hit the new button it starts us over with an empty textbox.
11
More
12
STEP 9
In this function, we use the OpenFileDialog to specify the type of document we want to
open. We then set the title bar of the dialog to say Open. Then we told the dialog that we
want it to open up to the C:\ folder every time we open this dialog. The if statement tells
the dialog that if the user selects a file then it should read all the text in the file and then
display it into the textbox.
The Save dialog begins by creating a buffer and setting it equal to the text that is in the
textbox. We then told the dialog that we want to save the file as a text document. Then
we set the dialog title to say Save As. We set the Initial Directory to be the C:\ folder so
that it opens there every time and told the dialog that we did not want it restore the
directory. Then using the if statement we told the dialog that if the user presses the ok
button then it is to take the name the user entered it and use that as the file name and
13
then it writes what is in the textbox (the buffer) to the file.
STEP 10
Now let’s take a look at the Save dialog.
14
STEP 11
• Finally we will look at the Exit command.
This one is really simple. Since we do not have to use any dialogs to close the program
we just tell it to exit the application when the user selects Exit.
Now you can run the application and you’re good to go. I hope this application helped
you understand the dialog boxes a little bit better. Feel free to ask any questions or post
comments on how I could improve my tutorials.
15
Questions?