Setting cursor position in Textbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sypher04
    New Member
    • May 2009
    • 3

    Setting cursor position in Textbox

    Hi, new here as a member, but have visited frequently as a guest. I'm a C# developer from Canada. I primarily work on ASP.net and Pocket PC.

    I am currently in the process of writing a new mobile app with the .NET Compact framework, but have hit a snag I'm hoping someone here will know something more about than myself.

    So, I have a textbox, and whenever said textbox gains focus, I'd like to set the current cursor position to a specific place. I know this can be achieved by using either of the Select() method, and SelectionStart property. As such, I've done so using the former.

    Problem is that, if the textbox is clicked in to gain focus it sets the SelectionStart to the right value when it gains focus, but after my GotFocus method has completed, it changes to the position that was clicked inside the box.

    In another test, I tried running the same code from my GotFocus method from elsewhere.. on formLoad specifically, and it worked exactly as expected. So, the click on the box is definitely what is throwing it off.

    Any thoughts on how I can get around my problem?
  • sypher04
    New Member
    • May 2009
    • 3

    #2
    I figured out a solution to my issue right after posting, so you can disregard my above my post. However, for anyone who may run into this problem in another project of their own. Here's my solution:

    I used subclassing to provide myself with a click event for the textbox control, and then moved the code from my GotFocus event into the Click. For my purposes, having a click event is even better because I need to be able to keep the cursor locked to a position even after gaining focus.

    Anyway, here's a link for on subclassing:

    Comment

    Working...