Search | Contact | Link To Us  

      VB .NET       

.NET Defined
OOP with VB
VB Language
Win Forms
Windows Controls
ADO .NET
User Controls
File Handling
Multithreading
Deployment
XML Web Services Essential XML
Resources
Discussions
ASP.NET
About



Advertisement


     Home> Windows Controls> Context Menus


Context Menus

Context menus are menus that appear when an item is right-clicked. In any windows application when you
right-click your mouse you get a menu which might display some shortcuts from the Edit Menu, for example, cut, copy, paste, paste special and so on. All these menu items which are available when you right-click are called Context Menus. In Visual Basic we create context menus with the ContextMenu component. The ContextMenu component is edited exactly the same way the MainMenu component is edited. The ContextMenu appears at the top of the form and you can add menu items by typing them. To associate a ContextMenu with a particular form or control we need to set the ContextMenu property of that form or control to the appropriate menu.

Working With Example

Let's understand ContextMenus with an example. On a new Form drag a ContextMenu component from the toolbox. Click on the ContextMenu component to open the editor at the top of the form. In the type here box, enter cut, copy, paste. Cut is assigned MenuItem1, Copy with MenuItem2 and Paste with MenuItem3. Darg two RichTextBoxes onto the form. In the properties window for the form and the richtextboxes, select the ContextMenu property and set it to ContextMenu1. Make sure you set the ContextMenu property for both the richtextboxes. This sample application allows you to enter some text in RichTextBox1, select some text, cut/copy the slected text and paste it in RichTextBox2. It is similar to the right-click menu with which you work in other windows applications. The whole design should look like the image below.

ContextMenu Component

Code to get the desired result looks like this:

Public Class Form3 Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

#End Region

Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e _
As System.EventArgs) Handles MenuItem1.Click
RichTextBox1.Cut()
End Sub

Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e _
As System.EventArgs) Handles MenuItem2.Click
RichTextBox1.Copy()
End Sub

Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e _
As System.EventArgs) Handles MenuItem3.Click
RichTextBox2.Paste()
End Sub

End Class

You can run the application, enter some text in richtextbox1, cut/copy it and paste it in richtextbox2. The images below display sample output from the above said code.



  Privacy Policy | Terms of Use | Site Map | Contact

  © 2004-2010 Startvbdotnet.com. All rights reserved.