Solved

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

Posted on 2011-03-25
18
246 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
  • 9
  • 4
  • 2
  • +3
18 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
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
 
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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Familiarize people with the process of utilizing SQL Server views 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 Microsoft Access…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

911 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

23 Experts available now in Live!

Get 1:1 Help Now