469,645 Members | 1,634 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,645 developers. It's quick & easy.

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?
May 20 '09 #1
1 5101
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:

http://www.codeproject.com/KB/cs/Tex...bclassing.aspx
May 20 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by Steve | last post: by
1 post views Thread by objectref | last post: by
5 posts views Thread by Tina | last post: by
2 posts views Thread by Gene Hubert | last post: by
4 posts views Thread by Neil Robbins | last post: by
4 posts views Thread by Brandon McCombs | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.