Solved

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

Posted on 2011-03-25
18
239 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
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…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

705 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

17 Experts available now in Live!

Get 1:1 Help Now