Page 1 of 1

Menu Bar Part 1 - File Menu (For Complete Beginners)

#1 RandomlyKnighted  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,367
  • Joined: 14-January 10

Posted 05 March 2011 - 10:08 PM

A Few Words By The Author

Hey everyone, welcome to my first tutorial on making a Menu bar for your Windows application. In this tutorial, I will teach you how to make a File menu.

Note: It is assumed that you already know how to create a C# project in the IDE. For this program, we will be using Visual Studio 2010 and .NET Framework 4.

The Tutorial

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.

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.

Posted Image

Drag the MenuStrip over to your Form and position it at the top as shown in this picture.

Posted Image

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.

Posted Image

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.

Posted Image

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.

Posted Image

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 of 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 about 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

Posted Image

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:

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox1.Text = string.Empty;
        }



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.

Now for the Open command.

private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Text Document (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog1.Title = "Open";

            openFileDialog1.InitialDirectory = @"C:\";
            openFileDialog1.RestoreDirectory = false;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string file = openFileDialog1.FileName;
                string text = File.ReadAllText(file);
                textBox1.Text = text;
            }

        }




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.

Now letís take a look at the Save dialog.

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string buffer = textBox1.Text;
            
            // Available file extensions
            saveFileDialog1.Filter = "Text Document (*.txt)|*.txt|All files (*.*)|*.*";
            saveFileDialog1.Title = "Save As";

            // Startup directory
            saveFileDialog1.InitialDirectory = @"C:\";

            // Restores the selected directory, next time
            saveFileDialog1.RestoreDirectory = false;

            // Display the Save file dialog.
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                // Get file name from the user.
                string name = saveFileDialog1.FileName;
                File.WriteAllText(name, buffer);
            }
        }



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 then it writes what is in the textbox (the buffer) to the file.

Finally we will look at the Exit command.

private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }



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.

Is This A Good Question/Topic? 1
  • +

Replies To: Menu Bar Part 1 - File Menu (For Complete Beginners)

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,857
  • Joined: 02-June 10

Posted 07 March 2011 - 06:28 AM

It's worth noting that a menustrip has an option in the SmartTags pop up (that little arrow on the right of the menustrip component) to automatically fill the menustrip with all the standard options people expect, along with the keystrokes and icons.

Not only is this a big time saver but it keeps your application in line with the expected user look and feel experience.

Once you have the standard items placed for you, you can always set the .Visible property to False for any option you aren't going to implement.
Was This Post Helpful? 0
  • +
  • -

#3 RandomlyKnighted  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,367
  • Joined: 14-January 10

Posted 07 March 2011 - 09:16 AM

Thank you ! I never realized that. I just tried it out and that definately makes things so much simpler. Now all anyone had to do is insert the Standard Items then code the dialogs.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,857
  • Joined: 02-June 10

Posted 07 March 2011 - 09:27 AM

Yeah. You'll find a LOT of the .NET components have a SmartTag menu with access to most used features. A TabControl has Add Tab and Remove Tab. In a ListView this is one of the easiest way to setup columns and groups. Even the lowly textbox has a SmartTag menu for 'Multiline'

You might also want to re-examine this line from both dialogs:
openFileDialog1.InitialDirectory = @"C:\";

It makes a big assumption that the user has a C drive. This is not always the case. I've had PC's where the system drive was F and there was no C drive on the computer.

This will get you the system drive (the drive the PC booted from). Have a play with it.
string DriveLetter = System.Environment.GetEnvironmentVariable("SystemDrive");

Was This Post Helpful? 1
  • +
  • -

#5 RandomlyKnighted  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,367
  • Joined: 14-January 10

Posted 07 March 2011 - 12:51 PM

View PosttlhIn`toq, on 07 March 2011 - 10:27 AM, said:

Yeah. You'll find a LOT of the .NET components have a SmartTag menu with access to most used features. A TabControl has Add Tab and Remove Tab. In a ListView this is one of the easiest way to setup columns and groups. Even the lowly textbox has a SmartTag menu for 'MultiLine'

You might also want to re-examine this line from both dialogs:
openFileDialog1.InitialDirectory = @"C:\";

It makes a big assumption that the user has a C drive. This is not always the case. I've had PC's where the system drive was F and there was no C drive on the computer.

This will get you the system drive (the drive the PC booted from). Have a play with it.
string DriveLetter = System.Environment.GetEnvironmentVariable("SystemDrive");


I use the textbox MultiLine property a lot. I thought about mentioning that in the tutorial but I decided to leave it out to keep things simple. I've never thought about the drive letter being different when creating the tutorial but that's still great information. Thanks for sharing it!
Was This Post Helpful? 0
  • +
  • -

#6 wajih1990  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 22-February 11

Posted 19 March 2011 - 02:13 PM

View PostRandomlyKnighted, on 07 March 2011 - 09:16 AM, said:

Thank you ! I never realized that. I just tried it out and that definately makes things so much simpler. Now all anyone had to do is insert the Standard Items then code the dialogs.

hi i done file menu four things remainig first how we make logic for find option other how we use status strip third how we make help optin all thigs for notepad so how we do for these three thins make in note paid
Was This Post Helpful? 0
  • +
  • -

#7 RandomlyKnighted  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,367
  • Joined: 14-January 10

Posted 19 March 2011 - 03:50 PM

I will cover all of these in a later tutorial. If you need help sooner, you can always ask in the forum. We have great members here that will be glad to help you and keep on the lookout for future tutorials.
Was This Post Helpful? 0
  • +
  • -

#8 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Posted 21 April 2011 - 03:33 AM

View PosttlhIn`toq, on 07 March 2011 - 11:27 AM, said:

Yeah. You'll find a LOT of the .NET components have a SmartTag menu with access to most used features. A TabControl has Add Tab and Remove Tab. In a ListView this is one of the easiest way to setup columns and groups. Even the lowly textbox has a SmartTag menu for 'Multiline'

You might also want to re-examine this line from both dialogs:
openFileDialog1.InitialDirectory = @"C:\";

It makes a big assumption that the user has a C drive. This is not always the case. I've had PC's where the system drive was F and there was no C drive on the computer.

This will get you the system drive (the drive the PC booted from). Have a play with it.
string DriveLetter = System.Environment.GetEnvironmentVariable("SystemDrive");



Hi tlhIn`toq,

That is a good idea. Instead of directing to the c drive it will look for the actual drive on certain harddrives to retrieve informtion from. I didn't know that part. Live and learn right :)
Thanks for the added information.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1