The new vbAccelerator Site - more VB and .NET Code and Controls

TextBox control - adding Cut, Copy, Paste, Undo and Delete command support

Author:

Steve McMahon(steve@vbaccelerator.com)

Keywords:

API,Text Box,Windows,Windows Controls

Updated:

18/08/99

Other Tips
All Tips
By Date
By Subject


API (33)
Bit
Manipulation (3)

Clipboard (3)
Combo
Box (5)

Desktop (3)
GDI (13)
Graphics (13)
Internet (2)
Interprocess
Comms (3)

Keyboard (2)
Mouse (1)
Shell (1)
Sprites (1)
Subclassing (3)
Text
Box (2)

Windows (11)
Windows
Controls (10)



Submit


This tip demonstrates how to simply add Cut/Copy/Paste and Undo support to a Visual Basic TextBox.

Start a new project and add a Class module. Rename the Class module to cTextBoxEdit and then add the following code:


Private Declare Function SendMessageLong Lib "USER32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function SendMessageString Lib "USER32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Private Const WM_COMMAND = &H111
Private Const WM_CUT = &H300
Private Const WM_COPY = &H301
Private Const WM_PASTE = &H302
Private Const EM_UNDO = &HC7
Private Const EM_CANUNDO = &HC6
Private Const EM_REPLACESEL = &HC2
Private Declare Function IsClipboardFormatAvailable Lib "USER32" _
      (ByVal wFormat As Long) As Long
Private Const CF_TEXT = 1
Private Const CF_UNICODETEXT = 13
Private Const CF_OEMTEXT = 7

Private m_txtThis As TextBox

Public Property Let TextBox(ByRef txtThis As TextBox)
   Set m_txtThis = txtThis
End Property
Public Sub Cut()
   SendMessageLong m_txtThis.hWnd, WM_CUT, 0, 0
End Sub
Public Sub Copy()
   SendMessageLong m_txtThis.hWnd, WM_COPY, 0, 0
End Sub
Public Sub Paste()
   SendMessageLong m_txtThis.hWnd, WM_PASTE, 0, 0
End Sub
Public Sub Undo()
   If (SendMessageLong(m_txtThis.hWnd, EM_CANUNDO, 0, 0) <> 0) Then
      SendMessageLong m_txtThis.hWnd, EM_UNDO, 0, 0
   End If
End Sub
Public Property Get CanCut() As Boolean
   CanCut = (Not (m_txtThis.Locked) And m_txtThis.SelLength > 0)
End Property
Public Property Get CanCopy() As Boolean
   CanCopy = (m_txtThis.SelLength > 0)
End Property
Public Property Get CanPaste() As Boolean
   If IsClipboardFormatAvailable(CF_TEXT) Then
      CanPaste = True
   ElseIf IsClipboardFormatAvailable(CF_UNICODETEXT) Then
      CanPaste = True
   ElseIf IsClipboardFormatAvailable(CF_OEMTEXT) Then
      CanPaste = True
   End If
End Property
Public Property Get CanUndo() As Boolean
   CanUndo = (SendMessageLong(m_txtThis.hWnd, EM_CANUNDO, 0, 0) <> 0)
End Property
Public Sub ReplaceSelection(ByRef sText As String, Optional ByVal bAllowUndo = True)
   Dim lR As Long
   If (m_txtThis.SelLength > 0) Then
      lR = Abs(bAllowUndo)
      SendMessageString m_txtThis.hWnd, EM_REPLACESEL, lR, sText
   End If
End Sub
Public Sub Delete(Optional ByVal bAllowUndo = True)
   Dim lR As Long
   SendMessageString m_txtThis.hWnd, EM_REPLACESEL, lR, vbNullChar
End Sub

To test out the code, add a TextBox to your project's form. Set the MultiLine property to True and then set up an Edit menu as follows:

  Caption Name Index Shortcut  
  &Edit mnuEditTOP      
  &Undo mnuEdit 0 Ctrl+Z  
  - mnuEdit 1    
  Cu&t mnuEdit 2 Ctrl+X  
  &Copy mnuEdit 3 Ctrl+C     &Paste mnuEdit 4 Ctrl+V     &Delete mnuEdit 5 Del  


Finally, add this code to the form:

Option Explicit

Private m_c As cTextBoxEdit

Private Sub Form_Load()
   Set m_c = New cTextBoxEdit
   m_c.TextBox = Text1
End Sub

Private Sub mnuEdit_Click(Index As Integer)
   Select Case Index
   Case 0
      m_c.Undo
   Case 2
      m_c.Cut
   Case 3
      m_c.Copy
   Case 4
      m_c.Paste
   Case 5
      m_c.Delete
   End Select
End Sub

Private Sub mnuEditTOP_Click()
   mnuEdit(0).Enabled = m_c.CanUndo

   mnuEdit(2).Enabled = m_c.CanCut
   mnuEdit(3).Enabled = m_c.CanCopy
   mnuEdit(4).Enabled = m_c.CanPaste
   mnuEdit(5).Enabled = m_c.CanCut

End Sub


Run the project. Undo, Cut, Copy, Paste and Delete functionality is available from the menu, and the menu items are enabled and disabled at the correct times.

If you also need to show the Undo, Cut, Copy and Paste buttons on a toolbar, then you need to add more code because you must update the status of the buttons in real time. You can set the Undo button enable state by checking whenever the TextBox's Change event occurs. To set the Cut, Copy and Paste states, check on the TextBox's GotFocus, KeyUp and MouseUp events.

&nbsp

Related Tips and Articles:

&nbsp
 

About  Contribute  Send Feedback  Privacy

Copyright © 1998-1999, Steve McMahon ( steve@vbaccelerator.com). All Rights Reserved.
Last updated: 18/08/99