Forum > LCL

[SOLVED] TMemo & Line Index

(1/2) > >>

madref:
I am doing a search in a Tmemo with the following code

--- 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";}};} ---function TForm_Test.FindInMemo(AMemo: TMemo; AString: String; StartPos: Integer): Integer;begin  Result := PosEx(AString, AMemo.Text, StartPos);  if Result > 0 then begin    AMemo.SelStart := UTF8Length(PChar(AMemo.Text), Result - 1);    AMemo.SelLength := Length(AString);    AMemo.SetFocus;  end;  // ifend;     // FindInMemoThis gives me a number which is from the start of the Tmemo.


But how can I determine the linenumber from this?
I found this but this is for Delphi and not Lazarus.

--- 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";}};} ---mi := Memo_Test.Perform(EM_LINEFROMCHAR, i, 0);

lucamar:
Only way I know of is to read CaretPos after you set SelStart but before setting SelLength.

It may or may not work for all platforms: In GTK, for example, setting selections sometimes works ... "funny". :)

engkin:

--- Quote from: madref on March 24, 2019, 11:21:32 pm ---I am doing a search in a Tmemo with the following code

--- 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";}};} ---function TForm_Test.FindInMemo(AMemo: TMemo; AString: String; StartPos: Integer): Integer;begin  Result := PosEx(AString, AMemo.Text, StartPos);  if Result > 0 then begin    AMemo.SelStart := UTF8Length(PChar(AMemo.Text), Result - 1);    AMemo.SelLength := Length(AString);    AMemo.SetFocus;  end;  // ifend;     // FindInMemoThis gives me a number which is from the start of the Tmemo.

--- End quote ---
FindInMemo has a little bug:

--- 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.SelLength := UTF8Length(AString);

--- Quote from: madref on March 24, 2019, 11:21:32 pm ---But how can I determine the linenumber from this?
I found this but this is for Delphi and not Lazarus.

--- 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";}};} ---mi := Memo_Test.Perform(EM_LINEFROMCHAR, i, 0);
--- End quote ---
For Windows, use SendMessage:

--- 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";}};} ---  mi := SendMessage(Memo_Test.Handle, EM_LINEFROMCHAR, i, 0);i has to be for UTF16, but if you pass -1 instead of i, you get the line that has the caret/selection, without caring about UTF16.

madref:
But unfortunately I don't have a windows version.
I am using a Mac on OSx Mojave


Therefor EM_LINEFROMCHAR is not defined anywhere

sstvmaster:
Something like that?


--- 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";}};} ---function TForm_Test.FindInMemo(AMemo: TMemo; AString: String; StartPos: Integer): Integer;var  vPos: integer;begin  AMemo.HideSelection := false; // no focus needed to show selected  vPos := PosEx(AString, AMemo.Text, StartPos);  if vPos > 0 then begin    AMemo.SelStart := UTF8Length(PChar(AMemo.Text), vPos - 1);    AMemo.SelLength := UTF8Length(AString);    Result := AMemo.CaretPos.Y; // !!! It starts from 0 (zero) !!!  end;end;
Greetings Maik

Navigation

[0] Message Index

[#] Next page

Go to full version