Thank you for the tips Arnaud. The second one is especially useful, as it's exactly what I need. I thought that LineNumAtCharPos() was based on the number of eol-separated lines, not the number of 'visible' lines, created by the wrapping of the text.
Octave Le 10 mai 07, à 21:09, Arnaud Nicolet a écrit : > Le 10 mai 07 à 20:43 Soir, Octave Julien a écrit: > >> But besides SelChange() and selStart(), I don't see any other >> option to >> find out where the caret is. Do you have something else in mind, when >> you say I can use 'any function' ? Maybe I was a bit unclear : I >> wasn't >> talking about events here, just functions that return a position. > > No, it's just that you said "the only function I can use is..." while > you didn't say what you wanted to achieve. > >> Thinking further about the keyDown event, I understand it makes sense >> if it runs its code before the key is processed with a 'return false' >> statement. But that means that, should I want to get selStart() in the >> keyDown event, I would have first to code the normal behaviour of a >> key >> (i.e., moving the caret if one hits an arrow, inserting a character at >> the right place if one hits a letter, and any other key-related >> action)? > >> That seems like a tedious work ! Is there any way to tell RealBasic, >> in the keyDown event, "Do what you do normally, and then do what I >> say."? > > Well, that can't be done directly. > The problem is logical: > 1: the OS provides three events: KeyDown when a key is down, > SelChange when the selection changes and TextChange when the text has > changed (I can't be clearer here). > 2: the key pressed parameter is only valid in the KeyDown event > (logical, too). > > So, if you want to proceed the key after its action has been done, > you have to use the TextChange event or SelChange event (because > these are the events that occur after the key has been processed; > either one, the other, or both of these events) and remember the last > key. > > For example, you may add a property to your window: "MyPressedKey As > String". > Then, in the KeyDown event: > MyPressedKey=Key > > and in the SelChange/TextChange event, use MyPressedKey as you would > use the Key parameter in the KeyDown event. > > After that, you may make a class to group the property and events, > but that's another story. > >> By the way, my original goal was to find out not the position of the >> caret but the number of the line where it is, in other terms, the line >> number and not the character number. That seems pretty trivial now >> with >> LineNumAtCharPos(), but if you see an easier way to do that, please >> tell me. > > Ah, I see. LineNumAtCharPos returns the number of the line as seen on > the screen (meant: on the EditField) but you want to get the number > of the line in the text (without considering wrapping), I am correct? > > Then, you have to count how many end of lines you have before the > insertion point (or caret, as you said) and the beginning of the text. > > This function should work: > > Function GetRealLineAtCharPos(MyEF As EditField) As Integer > 'Parameter: MyEF is the EditField which you want to use > > dim i As Integer > dim j As Integer > dim k As Integer > dim l As Integer > dim s As String > > s=EditField1.Text > k=EditField1.SelStart 'The start of the insertion point. You may > decide how you want to handle a selection longer than 0. > j=CountFields(s,EndOfLine) 'How many lines are in the edit field. > > for i=1 to j 'Check every line... > l=l+len(NthField(s,EndOfLine,i))+1 'In "l", add the length of > "ith" line+1. At every loop, we get the count of characters from the > beginning to the "ith" end of line. > > 'When "l" is greater than "k", that means the "ith" end of line is > after the insertion point. > if l>k then Return i > Next > Return 1 'No end of line found; the current line is the first one. > End Function > >> Many thanks again for your help. > > You're welcome. _______________________________________________ Unsubscribe or switch delivery mode: <http://www.realsoftware.com/support/listmanager/> Search the archives: <http://support.realsoftware.com/listarchives/lists.html>
