Cut, Copy, Paste and Undo in VB .NET

Cut, Copy, Paste and Undo in VB .NET

If you haven't already, add two textboxes to your form and set their MultiLine property to True. What we'll do now is to get the following menu items to work: Undo, Cut, Copy and Paste. We'll start with Copying.

 

Implement a Copy Menu in VB NET

If you type Textbox1 in your code window, then a full stop, you get a list of properties and methods available to the textbox. Scroll up to the top and locate the Copy method:

VB NET Intellisense menu showing the Copy item

Notice the tool tip. It's telling you what this method does - copies the current selection to the clipboard. The clipboard is a temporary storage area available to most Windows applications. When you invoke the Copy method of the textbox, any selected text is place in this temporary storage area for you. You don't have to write any other code.

So access the code for your Copy menu item, and add this line to it:

Textbox1.Copy()

That's all there is for the copy menu! But nothing visible will happen when you run your code. Let's paste it into the second textbox.

 

The Paste Menu

Again, there's only one line of code to write. It's this:

TextBox2.Paste()

Notice that we're saying paste to textbox2. Because the copy menu places the text from textbox one onto the clipboard, you only need this one line of code. You're saying "Whatever is on the Clipboard, paste it into Textbox2".

So add that line to your Paste menu item. Your code window should look like this:

Time to test it out. Run your programme. Select all the text in textbox one (it might already be selected), then click Edit > Copy from your menu.

Click inside the second textbox. Then click Edit > Paste from your menu. The text should appear in textbox two.

 

The Cut Menu

Access the code for you Cut menu item. Add the following code to it:

TextBox1.Cut()

Run your programme, and select the text in textbox one. From your menu, click Edit > Cut. The text should disappear (it's on the clipboard, though). Click inside textbox two, and click Edit > Paste. The text should be pasted over.

 

The Undo Menu

For the Undo menu, add this line of code:

TextBox1.Undo()

Run your programme. Then select the text in textbox one. Click Edit > Cut and the text disappears. Now click Edit > Undo. The text reappears.

The Edit menu we implemented is only a simple one. But it does demonstrate what you can do with VB.NET and menus.

We'll complete out look at menus by coding for the View menu you added to your form. In the process, we'll take a look at pictures boxes, as well as seeing how easy it is to hide and disable controls on a form.

 

How to Show and Hide Controls

The items on our view menu are:

View Textboxes
View Labels
View Images

Although these are not terribly practical examples of what to place on a View menu, they will help us to demonstrate a few useful techniques. The first of these is how to show and hide controls.

 

The View Textboxes menu item

Controls on a form can be hidden or shown as the need arises. The process is quite easy. Access the code for your View Textboxes menu item. Type the following for the menu item:

Textbox1.Visible = False
Textbox2.Visible = False

Run your code and test it out. Click View > View Textboxes. The two textboxes you added should disappear.

To hide a control, simply set it's Visible property to False. If you want to get it back, show a control by setting the Visible property to True.

A good idea is to have the ability to toggle a control on and off: One click of a menu item could hide the control, and a second click could show it again. You can do that with your menus.

Each item on your menu has a Checked property. If set to True, you'll see a tick appear next to the menu item. As in the image below:

The selected menu item has a tick next to it

You can use this Checked property as a toggle: If the menu item is ticked, display the textbox; if it's not ticked, hide the textbox.

Delete or comment out the line of code for your View Textboxes menu item. Add the following line in its place (this assumes that you've Named your View Textboxes menu item as mnuViewTextboxes. If you've named it something else, changed the part before the full stop):

mnuViewTextboxes.Checked = Not mnuViewTextboxes.Checked

This line toggles the Tick on and off. The part before the equals sign sets the Checked property of our menu item. The part after the equals sign sets it to whatever it's NOT at the moment. So if Checked is True, it's NOT False. In which case, set it to False.

Run your code and test it out. Click View > View Textboxes. Have a look at the menu again, and you'll see a tick appear. Click View > View Textboxes again and the tick will disappear.

We can show the textboxes if there's a tick next to View Textboxes. Just test the value of the Checked property in an If Statement. Add this If Statement just below your first line:

If mnuViewTextboxes.Checked = True Then

TextBox1.Visible = True
TextBox2.Visible = True

Else

TextBox1.Visible = False
TextBox2.Visible = False

End If

So the If Statement examines the Checked property of the menu item. If it's True, make the textboxes Visible ; Else, we set the Visible property of the textboxes to False.

Before you run your code, return to the Form view by holding Shift + F7 on your keyboard. When you have your form displayed, and not the code, click on textbox1 to select it. In the property box, locate the Visible property and set it to False. Do the same for texbox2. When your form runs, the two textboxes will then be hidden.

Now run your programme and test out your new menu. Click View > View Textboxes and see if they toggle on and off.

Exercise

Add two labels to your form. Write code to toggle the labels on and off. The two labels should disappear with the textboxes. And they should reappear when the menu item is toggled to the on position

 

The View Images menu Item

It's easy to add an image to your form with VB.Net. To insert an image, locate the PictureBox control in the toolbox (under Common Controls). Either double click the control, or hold down your mouse on the form and draw one out. You should see something like this:

The Picture Box control

Change the Height and Width properties of the Picture Box to 100, 100. (You can change both from the Size property.) You'll have a small square. To make it stand out more, locate the BorderStyle property. Change the value to Fixed3D. Your Picture Box will then look like this:

A fixed3d picture box

To add a picture at design time, locate the Image property in the properties box:

The Image property of the Picture Box

Download or open any image from your computer. Then click the button with the three dots on it. A dialogue box appears. Locate an image. Select it, and then click Open in the dialogue box. The image will appear in your Picture Box:

An image in a picture box

If you select an image that is too big for the picture box, only part if it will be visible. The Picture Box control does not resize your image.

You can, however, set another property of the picture box - the SizeMode property. Set this to AutoSize and your picture box will resize to the size of your image.

 

Insert an Image with your View Menu

You can use your open file dialogue box again to specify an image for the user to select. We'll do this from the View Images menu item.

Highlight your code for the mnuOpen item. Copy the first five lines, these lines:

Dim strFileName As String

openFD.InitialDirectory = "C:\"

openFD.Title = "Open an Text File"
openFD.Filter = "Text Files|*.txt"
Dim DidWork As Integer = openFD.ShowDialog()

Paste them to your mnuViewImages menu item code. Change the Title property to this:

openFD.Title = "Open an Image"

And change the Filter property to this:

openFD.Filter = "jpegs|*.jpg|gifs|*.gif|Bitmaps|*.bmp"

Run your code and click your View Images menu item. You should see the Open dialogue box appear. If you look at the "Files of type" box, you should see this:

image types in Windows 7

You should now be able to see only the three image formats we've specified.

To insert an image into your Picture Box, some new code is needed. Again though, we'll wrap it up in an If Statement.

 

Add the following code below the lines you've just added:

If DidWork <> DialogResult.Cancel Then

strFileName = openFD.FileName
PictureBox1.Image = Image.FromFile(strFileName)
openFD.Reset()

End If

There's only two lines you haven't met yet. The first is this line:

PictureBox1.Image = Image.FromFile(strFileName)

Previously, you were loading the image into the Image property of PictureBox1 directly from the Properties Box (by clicking the grey button with the three dots in it). Here, we're loading an image into the Image property using code. The way you do it is with the FromFile method of the Image Class.

Although that might be a bit baffling at this stage of your programming career, all it means is that there is some in-built code that allows you to load images from a file. In between round brackets, you type the name and path of the file you're trying to load. Since our file name has been placed inside of the strFileName variable, we can just use this. You can then assign this to the Image property of a Picture Box.

The last line, openFD.Reset(), will reset the initial directory of the open file dialogue box. To see what this does, comment out the line (put a single quote at the start of the line). Run your programme and Click View > View Images. Insert an image and then click File > Open. You'll notice that the files displayed in your dialogue are from the last directory you opened, rather than the one you set with "InitialDirectory = "C:\". By resetting the open dialogue box control, you're fixing this problem.

OK, that concludes our look at menus. We'll create a new programme now, and explore checkboxes, radio buttons and Group Boxes