views:

26

answers:

1

Hi,

I have a small application that displays a listbox under the cursor position when the user uses a shortcut key.

When the user double clicks a selection from the listbox I want to insert that selected text at the curser position of that opened window.

Example: user has microsoft word open. He/she uses a shortcut key that displays a listbox just under the cursor position. The listbox has a collection of text. When the user double clicks a selection that selected text is inserted at the cursor position.

I tried the following:

Private Sub ListBox1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
Text.Insert(Cursor.Position, ListBox1.SelectedItem)
End Sub

But that doesn't work.

Any help will be sincerely appreciated.

A: 

The best (most generic) approaches will be to trick the application into thinking you have entered some text. For example:

  • Send keypress windows messages for all of the characters you wish to "type" to the target window (e.g. with WM_KEYDOWN or WM_CHAR type messages. Some experimentaiton may be needed to find the approach that works best).

  • Copy the text onto the clipboard and send a single ctrl+V keypress message to the application. (This will overrite the clipboard and may not work in apps that don't support that key shortcut though)

If you know the specfic application (e.g. MS Word) then you may be able to use application-specific automation (OLE, etc) interfaces to insert text.

Jason Williams
Many thanks for your response. I am sorry to say I have no idea of how to do what you suggested. Could you please give me an example or point me in the right direction?
mazrabul
It's always heard to know what level to pitch an answer at. To send keyboard messages you need to use the SendMessage function. I suggest googling on "VB send WM_CHAR WM_KEYDOWN" to find examples of how to do this - there is a lot of discussion of this topic around the web that should be plenty to get you started.
Jason Williams
again, many thanks. I am researching he subject now.
mazrabul