Home / News / VB.Net Tutorial 18: Coding The File Menu – 1

VB.Net Tutorial 18: Coding The File Menu – 1

In last few tutorials, we learned how to add menu to the VB.net form. We also learned how to add sub-menus and coding for exit button. However, no form is complete with just one menu. We have not provided any functions for copying contents of the form or pasting anything to the form. So, lets add two main menus,”Edit” and “View” to our form.

In edit add sub-menus as:

  • Undo (Ctrl + Z)
  • Copy (Ctrl + C)
  • Cut (Ctrl + X)
  • Paste (Ctrl + V)

In view, add sub-menus as:

  • View Textbox (Ctrl + Shift + T)
  • View Label (Ctrl + Shift + L)
  • View Images ((Ctrl + Shift +  I)

Now that you have added menus to the form, lets start coding. Let us start with File>Open sub-menu. For that purpose, locate OpenFileDialog in toolbox. Adding it to the form, won’t make any visible changes but a control will get added at the bottom. Click on the control and access properties to change its name. Now double click on File>Open menu to access code window and put following code in it.

Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
OpenDiaBox.ShowDialog()
End Sub

Here OpenDiaBox is the changed name of OpenFileDialog box. Save the code and run the program. Click on File>Open and see what happens. A default directory will open in front of you. You can even customize which directory to open. For that purpose, add following line of code into the one written above.

OpenDiaBox.InitialDirectory = "C:\"

Run the program and something like this will open in front of you. Make a note that I have just provided path for C drive but you can extend the path to inner directories as per choice.

As you can see, title of this box is “Open”. You can change it to your choice by accessing “title” value of dialog box. Your code after adding code for title change must look like this:

Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
OpenDiaBox.InitialDirectory = "C:\"
OpenDiaBox.Title = "Open File"
OpenDiaBox.ShowDialog()
End Sub

Run the code and see the change in title. As you must have come across many times, we can display a list of specific files to open according to the file type. There is drop down list at the bottom of every Open dialog box which can let you select according to file type  like “.txt” for text file or “.jpg” for image files. Even we can add such filter to our dialog box.

OpenDiaBox.InitialDirectory = "C:\"
OpenDiaBox.Title = "Open File"
OpenDiaBox.Filter = "Text Files|*.txt"
OpenDiaBox.ShowDialog()

Run the program to see effect of our new code. You must get drop down list saying “Text Files”. This filter can be coded further to add different file types.

OpenDiaBox.Filter = "Text Files|*.txt|Word Files|*.doc"

See the image below to check if you are getting similar output.

You must have noticed by now that open dialog box is not actually “opening” files right now. Its because we have coded it just to show files that can be opened. In next tutorial, we will learn to open files so stay tuned and keep coding!

 

About Gaurav@TGC

I am pursuing my Computer Engineering degree from University of Mumbai. Freelance content writing is one of my hobbies that I am using for my bread! "I love technology" justifies my presence here!
Scroll To Top