By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,668 Members | 1,552 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,668 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
<bi********@yahoo.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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

Nov 17 '05 #2

P: n/a
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.

Nov 17 '05 #3

P: n/a
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.

Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.