Solved

How to get and set the position of the cursor in a textbox.

Posted on 2011-03-25
18
255 Views
Last Modified: 2013-11-28
I have a textbox control that is 40 characters wide and 50 lines high.

How can I use code to tell which line the cursor is currently on?
How can I find the number of the next line that contains a given string?
How can I use code to move the cursor to the start of another line?
0
Comment
Question by:Milewskp
[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
  • 9
  • 4
  • 2
  • +3
18 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 35219134
0
 
LVL 44

Expert Comment

by:GRayL
ID: 35219216
LineOn = Int((Forms!Form1.Text1.Properties("SelStart").Value)+1)/40

Remember,  SelStart is zero based.  There are three properties of interest, SelStart, SelText, and SelLength.

Instr(Forms!Form1!Text1.Value, "Search for me") will give you the start of where "Search for me" is located in the string.  As that start is zero based, add one and divide by 40 and take the integer value:
Int((Instr(Forms!Form1!Text1.Value, "Search for me") + 1)/40)

With this start, using SelStart you should be able t answer Q3.
0
 
LVL 17

Expert Comment

by:JezWalters
ID: 35220991
Unless your text box is using a non-proportionally spaced font (such as Courier), I suspect that the number of characters per line is variable.

If this is the case then you'll have to parse the text box's .Text or .Value property, counting new line characters.
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 17

Accepted Solution

by:
JezWalters earned 500 total points
ID: 35221511
The attached sample database should do everything you're after:
Q26912674.mdb
0
 
LVL 17

Expert Comment

by:JezWalters
ID: 35221533
If you type some multi-line text into the Text box and then click anywhere within the Text (or use the cursor keys, or just type), you'll see the Current Line value gets updated accordingly.

The Next line containing 'Hello' value is also set - but bear in mind that I've currently made this case-sensitive, though it's very easy to change this.

Finally, if you change the Current line value and then click the Goto Current Line button, the cursor will be positioned at the beginning of the specified line within the Text box.

Any questions, let me know!  :-)
0
 
LVL 17

Expert Comment

by:JezWalters
ID: 35221537
It might be worth setting the Access "Behaviour entering field" Option to "Go to start of field" too.  You can do this in code - let me know if you'd like to know how!  :-)
0
 
LVL 17

Expert Comment

by:JezWalters
ID: 35221714
Oh yeah, and I forgot to mention that (unless I'm missing something) GRayL's idea will only work if every line has exactly 40 characters.  No offense GRayL!  :-)
0
 
LVL 44

Expert Comment

by:GRayL
ID: 35222763
JezWalters:  None taken.  Having stated the width was 40 characters, I assumed the asker was using a fixed width font.  
0
 
LVL 17

Expert Comment

by:JezWalters
ID: 35224392
Just for the benefit of Milewskp, whether you're using a non-proportionally spaced font or not, you'll still have some lines that are less than 40 character - which means you have to parse the text to work out line numbers.

Hope that makes sense!  :-)
0
 
LVL 44

Expert Comment

by:GRayL
ID: 35224494
my benefit too, thanks jez.
0
 
LVL 75
ID: 35224512
Guys ... may I offer a suggestion here.  Wait for Milewskp to respond before making any further posts :-)

mx
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 35228856
;-)
0
 
LVL 1

Author Comment

by:Milewskp
ID: 35376306
Hi Jez,
Excellent work, Thanks!

<It might be worth setting the Access "Behaviour entering field" Option to "Go to start of field" too.  You can do this in code - let me know if you'd like to know how!  >
Yes, I'd like to know.

(Sorry I didn't response earlier.)
0
 
LVL 17

Expert Comment

by:JezWalters
ID: 35376764
Is it time to accept an answer/close the question then - or is there something else I can help with?
0
 
LVL 44

Expert Comment

by:GRayL
ID: 35380099
I->I/We?
0
 
LVL 17

Expert Comment

by:JezWalters
ID: 35382066
The "Behaviour entering field" options are the second set of Advanced Access Options:

1. Click the Office Button (top left)
2. Click the "Access Options" button (bottom right)
3. Click "Advanced" in the category list on the left hand side
4. Find "Behaviour entering field" in the "Editing" section and click "Go to start of field"
0
 
LVL 17

Expert Comment

by:JezWalters
ID: 35382906
You can set the option in code like this:
SetOption "Behavior Entering Field", 1

Open in new window

0
 
LVL 1

Author Comment

by:Milewskp
ID: 35487555
Hi Jez,
Thanks again for your help. Full points (sorry for the delay)!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

751 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