Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-03-25
18
Medium Priority
?
258 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 17

Accepted Solution

by:
JezWalters earned 2000 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

688 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