Sending Messages

Simple Commands

All the commands that tell a control to do something begin with WM_ so enter WM_ into the text box, and it will bring you to the point in the list with all those constants in. One of the easiest things to try out sending messages on is a text box. There are constants that will allow you to perform basic functions by sending a command to a text box, or Rich Text Box to Cut, Copy, Paste, Clear and Undo. These are WM_CUT, WM_COPY, WM_PASTE, WM_CLEAR and WM_UNDO. Their values are listed below:

Const WM_CUT = &H300 ' Cut the selected text to the clipboard (Ctrl+X)
Const WM_COPY = &H301 ' Copy the selected text to the clipboard (Ctrl+C)
Const WM_PASTE = &H302 ' Pastes the text from the clipboard (Ctrl+V)
Const WM_CLEAR = &H303 ' Clears the selected text (Del)
Const WM_UNDO = &H304 ' Undos the last action (Ctrl+Z)

To send a command to the text box to do any of these things, use the following syntax:

SendMessage(TextBoxName.hWnd, Constant, 0, 0& )

Where TextBoxName is the name of your text box, and Constant is one of the constants listed above. The other two properties are set to 0 and 0& (Null) as in this case we are telling the TextBox to do something, not to set a property and so they are not needed. The following example cuts the text from Text1 onto the Clipboard, when Command1 is pressed:

Private Const WM_COPY = &H301
Private Const WM_PASTE = &H302
Private Const WM_CUT = &H300

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Sub Command1_Click()
    SendMessage Text1.hwnd, WM_CUT, True, 0&
End Sub

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“C++: an octopus made by nailing extra legs onto a dog.” - Steve Taylor