Solved

Text Box Cursor

Posted on 1997-09-08
7
217 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Added a column screws up code 5 63
Advice in Xamarin 21 96
Export Data to Different .csv Files 26 114
VBA: Insert New column with specific format type 12 38
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…

792 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