Table of Contents

13.4 Context menu

A special type of menu is the context menu as a pop-up menu. The context is a visible component. This means that the pop-up menu only refers to this component. A menu only becomes a context menu when it is assigned to a special context menu. If you use the menu editor, you can define a list of different menus and assign them to individual components in the source code - ComboBox and PictureBox in this case - as context menus:

Public Sub ComboBox1_Menu()
  mnuCombobox1.Popup
End ' ComboBox1_Menu()
 
Public Sub PictureBox1_Menu()
  mnuPictureBox1.Popup
End ' PictureBox1_Menu()

You save these 2 statements if you already make the assignment in the object inspector with the' PopupMenu' property from the list of defined menus offered there.

The context menu opens after right-clicking over the component at the position of the mouse pointer. A context menu contains a list of menus that trigger a subroutine call after clicking with the left mouse button. You can create and configure the context menu at design time in the menu editor or encode it in the source code. You can equip the following components with a context menu:

Button, ButtonBox, CheckBox, CheckBox, ColorButton, ColorChooser, ColumnView, ComboBox, ComboBox, Container, Control, DateBox, DateChooser, Dial, DirChooser, DirView, DrawingArea, Editor, Embedder, Expander, FileChooser, FileView, FontChooser, Form, Frame, GridView, HBox, HPanel, HSplit, HSplit

Good to know:
For example, if you use the TextArea_Menu() and mnuContextTextArea.Popup event to declare a context menu for a TextArea, the original is overwritten as shown in the following two images:

KontextMemü-Original
Figure 13.4.1: Context menu TextArea - Original

Benutzer-Menü
Figure 13.4.2: TextArea context menu - user-defined

You can not only overwrite context menus of selected components with your own menus, but you can also deactivate the context menu in general. The following code is used here for the (fictitious) component TextArea3:

Public Sub TextArea3_Menu()
  Stop Event
End

13.4.1 Project

In the presented project, the original context menu of the two components 'TextArea' is replaced by a user-defined, simple context menu. At the same time, the use of the ClipBoard class is demonstrated by copying or cutting text from the TextArea1 and pasting it into the TextArea2. You can also view the type (text format) of the inserted text after a copy operation. A violent click on the reset button (R) restores the start state of the program.

ClipBoard1
Figure 13.4.1.1: ClipBoard is empty {link_3}

Text
Figure 13.4.1.2: Marked text is copied to the ClipBoard - context menu1

O.K.
Figure 13.4.1.3: Contents of the ClipBoard can be inserted into the TextArea2 - Context menu2

Inhalt
Figure 13.4.1.4: Contents of the ClipBoard was inserted into the TextArea2

13.4.2 Project source code

The source text is completely specified and only commented on in selected parts, because the comments in the source text already show essential information:

[1] ' Gambas class file
[2]
[3] Private sText As String
[4]
[5] Public Sub Form_Open()
[6]
[7]   FMain.Center
[8]   FMain.Resizable = False
[9]   sText = "Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming "
[10]   sText &= "id quod mazim placeratfacer possim assum. Lorem ipsum dolor sit amet, consectetuer "
[11]   sText &= "adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna "
[12]   sText &= "aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation "
[13]   sText &= "ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat."
[14]   TextArea1.Text = sText
[15]   TextArea1.Wrap = True
[16]   TextArea2.Wrap = True
[17]
[18] End ' Form_Open
[19]
[20] Public Sub TextArea1_Menu()
[21]
[22]   Dim mnuContextTextArea1 As Menu
[23]   Dim mnuMenuItem As Menu
[24]
[25] ' A new menu object is created for the Textarea1
[26]   mnuContextTextArea1 = New Menu(FMain, False)
[27] ' 1. Menu in Menu1 of the TextArea1 component
[28]   mnuMenuItem = New Menu(mnuContextTextArea1) As "mnuSelectAll"
[29]   mnuMenuItem.Text = "Select all and copy all text to clipboard"
[30]   mnuMenuItem.Picture = Stock["select"]
[31] ' 2. Menü im Menü1
[32]   mnuMenuItem = New Menu(mnuContextTextArea1) As "mnuSelectedCopy"
[33]   mnuMenuItem.Text = "Copy selected text to clipboard"
[34]   mnuMenuItem.Picture = Stock["copy"]
[35] ' 3. Menü im Menü1
[36]   mnuMenuItem = New Menu(mnuContextTextArea1) As "mnuSelectedCut"
[37]   mnuMenuItem.Text = "Cut selected text"
[38]   mnuMenuItem.Picture = Stock["cut"]
[39]
[40]   mnuContextTextArea1.Popup 'Menu1 is assigned to TextArea_1 as a pop-up menu.
[41]
[42] ' One disadvantage of this procedure of giving each menu item the identifier mnuMenuItem should be
[43] ' should not be concealed: It is not possible in the program to select individual menu properties
[44] ' at runtime. This disadvantage does not apply when using the menu editor.
[45]
[46] End ' TextArea1_Menu
[47]
[48] ' The 3 actions that are triggered are now programmed here,
[49] ' if the corresponding menu was selected in the context menu of TextArea1.
[50]
[51] ' Menu 1 - Component TextArea1
[52] Public Sub mnuSelectAll_Click()
[53]   TextArea1.SelectAll()
[54]   TextArea2.Clear
[55]   If TextArea1.Selection.Text <> Zero Then
[56]      Clipboard.Copy(TextArea1.Text)
[57]    ' Alternative: TextArea1.Copy()
[58]   Endif ' TextArea1.Selection.Text <> Zero
[59] End ' mnuSelectAll_Click
[60]
[61] ' Menu 2 - Component TextArea1
[62] Public Sub mnuSelectedCopy_Click()
[63]   If TextArea1.Selection.Text <> Zero Then
[64]      TextArea2.Clear
[65]      Clipboard.Copy(TextArea1.Selection.Text)
[66]   ' Alternative: TextArea1.Copy()
[67]   Endif ' TextArea1.Selection.Text <> Zero
[68] End ' mnuSelectedCopy_Click
[69]
[70] ' Menu 3 - Component TextArea1
[71] Public Sub mnuSelectedCut_Click()
[72]   If TextArea1.Selection.Text <> Zero Then
[73]      TextArea2.Clear
[74]      Clipboard.Copy(TextArea1.Selection.Text)
[75]      TextArea1.Cut()
[76]   Endif ' TextArea1.Selection.Text <> Zero
[77] End ' mnuSelectedCopy_Click
[78]
[79] Public Sub TextArea2_Menu()
[80]   Dim mnuContextTextArea2 As Menu
[81]   Dim mnuP2T As Menu
[82]
[83] ' A new menu object is created for the text area2.
[84]   mnuContextTextArea2 = New Menu(FMain, False)
[85] ' Create a menu in Menu2 of the TextArea2 component.
[86]   mnuP2T = New Menu(mnuContextTextArea2) As "mnuPasteToTextArea2"
[87]   mnuP2T.Text = "Paste from ClipBoard"
[88]   mnuP2T.Picture = Stock["paste"]
[89]
[90]   If Clipboard.Type = Clipboard.Text Then
[91]      mnuContextTextArea2.Popup ' Menu2 is assigned to TextArea_2 as a screen pop menu (active).
[92]   Else
[93]     mnuP2T.Enabled = False
[94]     mnuContextTextArea2.Popup ' Menu2 is assigned to TextArea_2 as a pop-up menu (not active).
[95]   Endif ' Clipboard.Type = Clipboard.Text
[96]
[97] End ' TextArea2_Menu
[98]
[99]   Public Sub mnuPasteToTextArea2_Click()
[100] ' The one action that is triggered is programmed here,
[101] ' when the one menu is selected in the context menu of TextArea2.
[102]   TextArea2.Insert(Clipboard.Paste("text/plain"))
[103] End ' mnuPasteToTextArea2_Click
[104]
[105] Public Sub btnFormats_Click()
[106]   Dim sElement As String
[107]
[108]   TextArea2.Clear
[109]   For Each sElement In Clipboard.Formats
[110]     TextArea2.Insert(sElement & gb.NewLine)
[111]   Next ' sElement
[112]
[113] End ' btnFormats_Click
[114]
[115] Public Sub btnReset_Click()
[116]   Clipboard.Clear
[117]   TextArea2.Clear
[118]   TextArea1.Text = sText
[119]   TextArea1.SetFocus
[120] End ' Reset
[121]
[122] Public Sub btnClose_Click()
[123]   If Not Clipboard.Type = Clipboard.None Then Clipboard.Clear
[124]   FMain.Close
[125] End ' btnClose

Comment:

13.4.3 Download