0

I need to set Selection start of a text box based on the mouse position, i tried to load the text box on Double Click, once the text box is loaded, i need to set the selection start based on the Mouse position. (i.e) if a text box contains some values like "abcdef", if the mouse cursor is near "c" when textbox is loaded, then the selection start should be after "c".

I have also tried this

textBox.GetCharIndexFromPosition(e.Location);

but i didn't get it right,

Thanks in advance.

Regards,

Venkatesan R

venkatesan r
  • 249
  • 3
  • 15
  • 1
    textBox1.SelectionStart= textBox1.GetCharIndexFromPosition(e.Location); – Reza Aghaei Jan 22 '16 at 04:08
  • Do you want to change the selStart on every mousemove? Or should it happen only once? What use is it anyway, when the position of the doubleclick is used but the text is not loaded yet? I doubt the whole idea is well thought through.. – TaW Jan 24 '16 at 12:52
  • Hi @TaW, when a text box got focus and the selection start should set at middle if the cursor is at the middle of the textbox, only one time i need to set. i did this in WPF using textBox loaded event but i couldn't find such event also in WinForms – venkatesan r Jan 25 '16 at 09:47
  • Hi @RezaAghaei Sorry i clearly mentioned that code is not working for me – venkatesan r Jan 25 '16 at 09:48
  • 1
    @Reza's Code works just fine! You probably used the wrong event: `private void textBox1_MouseDoubleClick(object sender, MouseEventArgs e) { textBox1.Text = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; textBox1.SelectionStart = textBox1.GetCharIndexFromPosition(e.Location); textBox1.SelectionLength = 0; }` You need the `MouseDoubleClick`, not the simple `DoubleClick` or else you miss the `e.Location` param! – TaW Jan 25 '16 at 10:03

1 Answers1

1

Putting @Reza's code in the correct event will work just fine:

private void textBox_MouseDoubleClick(object sender, MouseEventArgs e)
{
    textBox.Text = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";  // load the text data here
    // now position the caret onto the mouse position
    textBox.SelectionStart = textBox.GetCharIndexFromPosition(e.Location);
    // and clear a selection
    textBox.SelectionLength = 0;
}

Note that you need to use the MouseDoubleClick, not the simple DoubleClick or else you miss the e.Location param!

This is the simplest and most direct way to get the mouse coordinates relative to the TextBox.

If your loading method is complex you can call it by passing in the MouseEventArgs e but simply calling it instead of the textBox.Text = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; is the most natural way.

If you want to you could also use

textBox.SelectionStart = textBoxtextBox1.PointToClient(Control.MousePosition));

This will work in any event or method. PointToClient will calulate the relative position from the screen position Control.MousePosition.

TaW
  • 53,122
  • 8
  • 69
  • 111
  • hi @TaW, it will work fine, but i couldn't get MouseEventArgs in the method i used to load the textbox in my application, i need to pass these args also to my method which will be not good. Can i get the solution by using Control.MousePosition? – venkatesan r Jan 28 '16 at 06:51
  • That is one way, but note that you need to convert it from absolute screen position to relative textbox coordinates (using [Control.PointToClient](https://msdn.microsoft.com/en-US/en-en/library/system.windows.forms.control.pointtoclient%28v=vs.110%29.aspx)). But you can pass the MouseEventArgs from the MouseDoubleClick to your method if you need to. But even easier: replace the textBox.Text = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; line by the call to your method! – TaW Jan 28 '16 at 07:01
  • Control.PointToClient in the sense, whether it will return the mouse point of the textbox? – venkatesan r Jan 28 '16 at 07:16
  • `Console.WriteLine("mouse at: " + e.Location);`in the `MouseDoubleClick` (or `MouseClick` or `MouseMove`) is exactly the same as `Console.WriteLine("Mouse at: " + textBox1.PointToClient(Control.MousePosition));` when called from anywhere.. Note that __Control != Control__ !! `Control.Location` is referring the Controller ie the mouse but `control.PointToClient` refers to some control, like the `TextBox` to which the coordinates are relative!! - But passing out the arguments or just the location is still the simplest way to go imo. – TaW Jan 28 '16 at 08:21
  • hi @TaW, i tried this but it doesn't returns the correct position. I tried by using mousemove and set selection start and i used a flag to skip if the selection start is set. Whether it's a right approach to do so? – venkatesan r Jan 29 '16 at 04:10
  • Not sure about the flag (and the reason for it!) . Maybe seeing more of your current code would help.. – TaW Jan 29 '16 at 07:13