20.4.1 Project 1 - Clipboard Text Demonstration Programme
The components TextArea and TextEdit as editors as well as the TextBox have a context menu (Undo, Redo, Cut, Copy, Paste, Delete, 'Select All') with which the exchange of text via the clipboard can be done safely - without having to use the Clipboard class.
The project presented demonstrates the use of the Clipboard class (gb.qt4) for texts. The inserted context menu for TextArea1 has three entries:
- Select entire text.
- Copy selected text to clipboard.
- Cut selected text “ clipboard.
Please note:
- If you declare a separate context menu for the above components, the original context menu will be overwritten!
- The TextBox with the small but incredibly significant text has the original context menu.
You can select text in Textarea1 or in TextBox and then use their context menus to copy or cut the selected text or paste text - even from other applications - into Textarea2:
Figure 20.4.1.1: 'Clipboard Text' demonstration programme
For example, if you copy and paste text from web pages or from OpenLibreWriter or from the email client Thunderbird, the display of text formats reveals a surprising variety of MIME types.
The source code for the demonstration programme is given in full:
Private sText As String Public Sub Form_Open() FMain.Resizable = False If Clipboard.Type <> Clipboard.None Then '-- Clipboard not empty? FMain.Caption = "ClipBoard MIME-Type: " & Clipboard.Formats[0] Endif ' Clipboard.Type <> Clipboard.None ? '-- Mustertext: sText = "Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming " sText &= "id quod mazim placeratfacer possim assum. Lorem ipsum dolor sit amet, consectetuer " sText &= "adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna " sText &= "aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation " sText &= "ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat." txaArea1.Text = sText txaArea1.Wrap = True txaArea2.Wrap = True End Public Sub btnShowFormats_Click() Dim sElement As String txaArea2.Clear() For Each sElement In Clipboard.Formats txaArea2.Insert(sElement & gb.NewLine) Next End Public Sub btnReset_Click() Clipboard.Clear() txaArea2.Clear() txaArea1.Text = sText txaArea1.SetFocus() FMain.Caption = "ClipBoard" End Public Sub btnTextArea2Clear_Click() txaArea2.Clear End Public Sub btnClose_Click() FMain.Close End '-- Menu declaration --------------------------------------------------------------------------------------- Public Sub txaArea1_Menu() Dim mnuContextTextArea1 As Menu Dim mnuMenuItem As Menu '-- A new pop-up menu object is created for the textarea1. mnuContextTextArea1 = New Menu(FMain, False) '-- 1st entry in the screen pop menu of the component TextArea1 mnuMenuItem = New Menu(mnuContextTextArea1) As "mnuSelectAll" mnuMenuItem.Text = "Highlight the entire text." mnuMenuItem.Picture = Stock["select"] '-- 2nd entry in the pop-up menu mnuMenuItem = New Menu(mnuContextTextArea1) As "mnuSelectedCopy" mnuMenuItem.Text = "Copy selected text to the clipboard." mnuMenuItem.Picture = Stock["copy"] '-- 3rd entry in the pop-up menu mnuMenuItem = New Menu(mnuContextTextArea1) As "mnuSelectedCut" mnuMenuItem.Text = "Cut selected text " & String.Chr(187) & " Clipboard." mnuMenuItem.Picture = Stock["cut"] '-- Assign screen pop menu for TextArea_1 mnuContextTextArea1.Popup End '-- The 3 actions that are triggered are now programmed here, '-- if the corresponding screen pop menu item of TextArea1 was selected. '-- Screen pop menu item 1 - Component TextArea1 Public Sub mnuSelectAll_Click() txaArea1.SelectAll() txaArea2.Clear() End '-- Screen pop menu item 2 - Component TextArea1 Public Sub mnuSelectedCopy_Click() If txaArea1.Selection.Text <> Null Then txaArea2.Clear() Clipboard.Copy(txaArea1.Selection.Text) '-- Alternative: TextArea1.Copy() FMain.Caption = "ClipBoard MIME-Type: " & Clipboard.Formats[0] Endif End '-- Screen pop menu item 3 - Component TextArea1 Public Sub mnuSelectedCut_Click() If txaArea1.Selection.Text <> Null Then txaArea2.Clear() Clipboard.Copy(txaArea1.Selection.Text) FMain.Caption = "ClipBoard MIME-Type: " & Clipboard.Formats[0] txaArea1.Cut() Endif End Public Sub txaArea2_Menu() Dim mnuContextTextArea2 As Menu Dim mnuP2T As Menu mnuContextTextArea2 = New Menu(FMain, False) mnuP2T = New Menu(mnuContextTextArea2) As "mnuPasteToTextArea2" mnuP2T.Text = "Insert text from ClipBoard..." mnuP2T.Picture = Stock["paste"] If Clipboard.Type = Clipboard.Text Then mnuContextTextArea2.Popup '-- Assign screen pop menu for TextArea_2 FMain.Caption = "ClipBoard MIME-Type: " & Clipboard.Formats[0] Else mnuP2T.Enabled = False mnuContextTextArea2.Popup '-- Assign screen pop menu for TextArea_2 Endif End Public Sub mnuPasteToTextArea2_Click() txaArea2.Clear() '-- Can also be omitted... txaArea2.Insert(Clipboard.Paste("text/plain")) txaArea2.SetFocus() End

