H o m e

 

C l o n e    M a i n    M e n u    t o    C r e a t e    C o n t e x t    M e n u    

By Michael McIntyre

mikemc@getdotnetcode.com

 

In this article a ContextMenu for a RichTextBox will be created from an application’s Edit menu. The ContextMenu will include the Edit menu’s Cut, Copy, and Paste menu items.

 

Scenario:  A Windows Forms application form includes a MainMenu which in turn includes an Edit menu.  On the same form is a RichTextBox named NewsEditorRichTextBox.

 

' Instantiate a new ContextMenu object and

' assign it to a variable named theContextMenu.

Dim theContextMenu As New ContextMenu()

' Declare a variable of type MenuItem

' named theMenuItem.

Dim theMenuItem As MenuItem

 

' Iterate through the EditMenuItem's MenuItems.

For Each theMenuItem In EditMenuItem.MenuItems

   ' If theMenuItem's text is Cut, Copy, or Paste

   ' clone theMenuItem and add the clone

   ' to theContextMenu's MenuItems collection.

   If theMenuItem.Text = "Cut" Or theMenuItem.Text = "Copy" _

   Or theMenuItem.Text = "Paste" Then

        theContextMenu.MenuItems.Add(theMenuItem.CloneMenu())

   End If

 Next

' Assign theContextMenu to NewsEditorRichTextBox.

NewsEditorRichTextBox.ContextMenu = theContextMenu

 

Copyright © 2001-2004 aZ Software Developers. All rights reserved.