Solved

Text Box Cursor

Posted on 1997-09-08
7
221 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 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
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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month4 days, 13 hours left to enroll

636 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