Search code examples
c#textboxcursorposition

How to set the cursor position of a text box on click


I'm required to have a textbox pre-filled with some text, and want the cursor to default to the beginning of the textbox when it is focused.

private void txtBox_Enter(object sender, EventArgs e)
{
    if (this.txtBox.Text == "SOME PREFILL TEXT")
    {
        this.txtBox.Select(0, 0);
    }
}

I'm capturing _Enter as above and it actually does work if I tab into the text box, but if I mouse-click into the text box, the cursor appears wherever the mouse click was performed, indicating that it is handled after the _Enter event, effectively "overwriting" what I did. To combat this, I hooked in the _Click event to call the txtBox_Enter handler as well, but no luck.

Is there any work around for this?

Thanks, -Ben


Solution

  • What is it the you are trying to accomplish - changing default functionality (such as a click which would normally select the cursor location) is asking for User Experience problems..

    Perhaps something along the lines of SETCUEBANNER is what you are trying for?