Solved

Text Box Cursor

Posted on 1997-09-08
7
211 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
  • 3
  • 2
  • 2
7 Comments
 
LVL 9

Accepted Solution

by:
cymbolic earned 100 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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 Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now