WM_RBUTTONDOWN WM_RBUTTONUP selects text I don't want selected in a textbox

B

bitwxtadpl

I want to programmatically set the position of a cursor in a textbox
that has focus based on a X and Y value.

However, when I send a WM_RBUTTONDOWN and a WM_RBUTTONUP message to the
textbox it selects the text from where the cursor is to where I want
the cursor to be. All I want is to move the position of the cursor
based on a X and Y values, not select the text.

The code to recreate the issue.

const int WM_LBUTTONDOWN = 0x0201;
const int WM_LBUTTONUP = 0x0202;

textBox1.Focus();

int X = 23;
int Y = 6;

SendMessage(textBox1.Handle, WM_LBUTTONDOWN, Y,X);
SendMessage(textBox1.Handle, WM_LBUTTONUP, Y,X);

Is there another message I can send?
Thanks,
Brian
 
N

Nicholas Paldino [.NET/C# MVP]

Brian,

I think you are confusing the terms somewhat. I think what you really
want is to move the caret position (the blinking sliver or box), and not the
cursor.

In order to set the caret position, just call the Select method on the
TextBox, passing the index you want to start selecting at, and then the
length of the text you want to select (set to zero in this case). You can
also achieve the same thing by setting the SelectionStart and
SelectionLength properties on the textbox.

If you are using .NET 2.0, you can find the index to send by calling the
GetCharIndexFromPosition method on the textbox. If you don't have .NET 2.0,
you can call SendMessage to the textbox, passing the EM_CHARFROMPOS message
(0xd7) to get the index of the character.

Hope this helps.
 
B

bitwxtadpl

Thanks for the feed back.
Sorry for the confusion on the terms - Yes I meant the blinking cursor
in the text box not the mouse cursor.
I don't have access to the text box since the type of the control is
not known at design time.

Ok, so let me explain the situation a bit more precisely.
I have a control named "Outer" that when I click on it with the
mouse I want to pass the mouse click into a new control named
"Inner". The "Inner" control is created and added to the
"Outer" control in the OnMouseClick event and the exact type of the
"Inner" control is unknown at design time.

To pass the mouse click onto the "Inner" control I send
WM_RBUTTONDOWN and a WM_RBUTTONUP messages to it.
This seems to work well. However, when the "Inner" control is a
TextBox, half of the time the text gets selected from the start of the
TextBox to where I clicked.

I suspect that there is a message I'm not sending, or a way to
unselect the text.
Any ideas?
Thanks,
Brian.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top