Link to home
Start Free TrialLog in
Avatar of normpos
normpos

asked on

Text Box Cursor

Is there a way to detect cursor position in a text box, and to control its position, in code?  Specifically, I have a one-line text box, and the first character is NOT to be changed by the user.  At this time I am doing this:  Upon receiving focus, I read (with Mid) the first character and save it.  Upon losing focus, I write this saved character back to the first position in the text box.  This works, but it would be neater if the user were prevented from having access to the first character (via backspace, etc.).  In general, I am wondering if cursor position in a text box is available in code.  I can think of other situations where knowledge of the cursor position would be useful. - Norm
ASKER CERTIFIED SOLUTION
Avatar of cymbolic
cymbolic

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of normpos
normpos

ASKER

Thank you.  Further Clarification please:  
Here is a text box with some text in it.  Upon receiving focus, I am using SelStart to place the cursor at the end of the existing string. The user may type from there, or maybe he will backspace.   I wish to prevent him backspacing to the first character. Whatever he does, I understand you to say that SelStart can be used to "report back" to my code where the cursor is (inside the KeyPress event sub, for instance).  If in fact it is at position #1, my code can force it to position #2.  From the viewpoint of the user, it appears that the cursor stops backspacing at position #2.  Is my understanding correct?
Yes.   That's how you do it.  I used this technique to make my own masked edit control (that actually worked, as opposed to the control that come's with VB), to force and preserve formatting characters for numeric input.  Just examine the Selstart property by tracing differenct events and using debug to reset it at different event times (I think I used Keydown)
Avatar of normpos

ASKER

Thank you.  I appreciate your help.  --  Norm.
I have had some trouble in this area ... under VB5, even with SP3 installed.  You can test this out yourself by simply doing a debug.print of the value in frmMain.TextBox.SelStart
If you insert this code into the TextBox_Click routine, the reported position is correct.  If you also add the code into the KeyPressed routine, handling only left and right arrow keys,
the results are horrendous :-
 1. The first keystroke moves the cursor on screen, but fails to
    change the SelStart value.
 2. When you change "direction", eg now move the cursor
    right after a number of "left" movements, the cursor is
    moved correctly, but the SelStart value is another "left".
You should try this out and also be aware that you can set the
cursor position via SelStart to 0 or 1 ... and in both cases the
cursor is placed AFTER character 1.

Maybe this will be fixed in VB6.

Avatar of normpos

ASKER

RobinM: Your comments are quite illuminating, and parallel my experience with this stuff.  It seems that sometimes things don't go the way one is led to expect.  It takes clever work-arounds - and it shouldn't.

What's SP3?

-- Norm
SP3 in the service pack for VB5, which mainly affect things in the development area - like the OCX controls etc.
Unfortunately it didn't fix the hassle I was having.

BTW: I submitted a question about it and received a detailed answer from Felonius.  You may want to look for it with a subject heading of "Mis-reported SelStart values in textbox"