Library tutorials & articles

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

Comments

  1. 27 Jun 2005 at 08:24
    Is ther any one who has been worked on organiation chart in vb.net
  2. 20 Sep 2004 at 12:43

    Dude i have got a software which does all the thing u told but its for sale just 1.2$ or 50Rs
    if u wish u can mail me humanprocessor@yahoo.co.in

  3. 07 Apr 2004 at 13:07

    I wanted to use sendmessage in VB.net
    with following declaration
    <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
       Public Shared Function SendMessage( _
           ByVal handle As IntPtr, _
           ByVal message As Integer, _
           ByVal wParam As IntPtr, _
           ByVal lParam As String _
      &nbsp As Integer
    end function
    But I am not getting the required text but I am getting windows text.
    Actually I am trying to get user typed URL from the address bar of IE.
    But the functions returns IE windows text
    Could any one suggest me how to solve this problem

  4. 16 Sep 2002 at 07:21
    Hi,
    I want to store all the keystrokes from diiferent windows programs. Like i am typing in word, writing code in VB, editing a Txt file or filling information on web as i am doiing now.....I want to get all the keystrokes....stored in a file.
    Please reply me....
  5. 01 Jan 1999 at 00:00

    This thread is for discussions of Sending Messages.

Leave a comment

Sign in or Join us (it's free).

James Crowley 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 audience ...

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

We'd love to hear what you think! Submit ideas or give us feedback