Forum > General

[SOLVED] Highlight a particular line of a memo

(1/3) > >>

w click:
How would you highlight, say, the fifth line of a memo without assigning selstart using pos or utf8pos.
 

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---s:=memo1.lines[5];Memo1.selstart:=UTF8Pos(s,Memo1.text)-1;memotext.sellength:=length(s);Memotext.Perform(EM_SCROLLCARET, 0, 0);Memotext.SetFocus;Works fine, except when line 2 is the same as line 5 or has line 5 as a sub-string, then it highlights line 2.
I just want a select line x.

winni:
Hi!

The following solution works only if wordwrap is set to false.  Otherwise the numbers of lines might not be the same like the index of memo.lines


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button3Click(Sender: TObject);var count,i, neededLine : integer;begincount := 0;neededLine := 3; // zero based for i := 0 to MEMO1.Lines.Count - 1 do  begin  if i = neededLine then    begin    Memo1.SelStart:= count;    Memo1.SelLength:=   length(Memo1.lines[i]);    exit;    end;  count := count + length(Memo1.lines[i])+length(LineEnding);  end;end;                    
Winni

dje:
Use CaretPos to move to a line? This works for me under Linux.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  AMemo.CaretPos := Point(0, ALine);  AMemo.SelLength := AMemo.Lines[ALine].Length;  
* Obviously add in bound error checking

w click:
Derek, I can't get 'Point' to work at all.

Winni, good idea, though I modified it a little.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---s:=Memo1.lines[target];label6.caption:=inttostr(target)+'. <'+s+'> len='+inttostr(length(s))+' utf8len='+inttostr(utf8length(s)); if (target>-1)and(target<=Memo1.lines.count-1) then begin  count:=0; i:=0;  while i<target do begin    count:=count+length(Memo1.lines[i]);    i:=i+1;  end;  Memo1.selstart:=count;  Memo1.SelLength:=length(Memo1.lines[target]);  Memo1.setfocus;end; But it veers off, because the length isn't giving the right result.  I tried utf8length, but again an error creeps in.  Is it something to do with smart quotes (and other characters)?

rick2691:
w click,

I assume that you are using a utf8 font, since you tried utf8length. But afterward you are still doing ascii lengths ... length(Memo1.lines. Your length must always be utf8length, because utf8 uses multiple characters for one character.

Rick

Navigation

[0] Message Index

[#] Next page

Go to full version