Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Text Box Cursor

Posted on 1997-09-08
7
Medium Priority
?
222 Views
Last Modified: 2012-05-04
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
0
Comment
Question by:normpos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 9

Accepted Solution

by:
cymbolic earned 200 total points
ID: 1434231
Yes.  The SelStart property reports the current underlying character postion when text is not selected.  When selected, the combination of .selstart and .selLength can be used to get you oriented.  Also, on focus or at aother event times, you can change selstart to keep you user off certain characters or positions in the string  
0
 

Author Comment

by:normpos
ID: 1434232
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?
0
 
LVL 9

Expert Comment

by:cymbolic
ID: 1434233
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)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:normpos
ID: 1434234
Thank you.  I appreciate your help.  --  Norm.
0
 

Expert Comment

by:RobinM
ID: 1434235
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.

0
 

Author Comment

by:normpos
ID: 1434236
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
0
 

Expert Comment

by:RobinM
ID: 1434237
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"
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question