by T.R. (1 Submission)
Category: String Manipulation
Compatability: Visual Basic 3.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (4 Votes)
I was just poking around this site, and noticed that a lot of folks were making the copy/paste/cut/undo functions more difficult then they really needs to be. Rather then writing your own functions, make use of the SendMessage API and let Windows do the work for you.
Assumes
To try out the code, simply create a form with a Richtextbox named Richtextbox1.
API DeclarationsPrivate Const EM_UNDO = &HC7
Private Const WM_CUT = &H300
Private Const WM_COPY = &H301
Private Const WM_PASTE = &H302
Private Const WM_CLEAR = &H303
Private Const WM_UNDO = &H304
Private Declare Function SendMessage Lib _
"USER32.DLL" Alias "SendMessageA" _
(ByVal hWnd As Long, _
ByVal dwMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Private Sub Command1_Click()
'=============================================
'Defined strictly for the Richtextbox, though
'might work on others.
'=============================================
SendMessage RichTextBox1.hWnd, EM_UNDO, ByVal 0&, ByVal 0&
End Sub
Private Sub Command2_Click()
SendMessage RichTextBox1.hWnd, WM_COPY, ByVal 0&, ByVal 0&
End Sub
Private Sub Command3_Click()
SendMessage RichTextBox1.hWnd, WM_PASTE, ByVal 0&, ByVal 0&
End Sub
Private Sub Command4_Click()
SendMessage RichTextBox1.hWnd, WM_CUT, ByVal 0&, ByVal 0&
End Sub
'=================================================
'This can be used on textboxes and other controls
'=================================================
Private Sub Command5_Click()
SendMessage RichTextBox1.hWnd, WM_UNDO, ByVal 0&, ByVal 0&
End Sub
Private Sub Form_Load()
RichTextBox1.Text = "This is line 1" & vbCrLf & _
"This is line 2" & vbCrLf & _
"This is line 3" & vbCrLf & _
"This is line 4" & vbCrLf
End Sub