Recent

Author Topic: [Solved] What is the shortest path for search control in a char?  (Read 1774 times)

loaded

  • Hero Member
  • *****
  • Posts: 878
[Solved] What is the shortest path for search control in a char?
« on: February 12, 2022, 09:31:55 am »
Hi
I'm in a situation where I need to find the current word at mouse position in the Memo.
Line 22 in the code below gives an error. I can solve this in the cin function that I have pacified above, but I also wanted to get your opinions because I think there is definitely an internal solution to the issue. Your ideas and opinions are always valuable to me. Respects.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Memo1MouseUp(Sender: TObject; Button: TMouseButton;Shift: TShiftState; X, Y: Integer);
  2.   //function cin(C:char;cp:array of char):Boolean;
  3.   //begin
  4.   //result:=true;
  5.   //end;
  6.  
  7.  var
  8.     separ:array[1..9] of char=(' ','.',',','(',')','[',']',':',';');
  9.     Lines:String;
  10.     LineRow,LineCol: LongInt;
  11.     sst,stp:integer;
  12. begin
  13.   if Button=mbRight then
  14.   begin
  15.    LineRow:=Memo1.CaretPos.y;
  16.    LineCol:=Memo1.CaretPos.x;
  17.    Lines:=inttostr(LineRow)+'-' + inttostr(LineCol) +  ' ' + memo1.lines.Strings[LineRow];
  18.    Caption:=lines;
  19.  
  20.      for sst:=LineCol downto 0 do
  21.      begin
  22.      if (lines[sst] in separ) then break;
  23.      end;
  24.  
  25.   end;
  26. end;  
« Last Edit: February 12, 2022, 11:25:36 am by loaded »
The more memory computers have, the less memory people seem to use. 😅

balazsszekely

  • Guest
Re: What is the shortest path for search control in a char?
« Reply #1 on: February 12, 2022, 09:37:19 am »
@loaded

I windows only solution, not exactly what are you after but pretty close. You do the rest, it only takes a few lines of code:
Code: Pascal  [Select][+][-]
  1. uses windows;
  2.  
  3. procedure TForm1.Memo1MouseMove(Sender: TObject; Shift: TShiftState; X,
  4.   Y: Integer);
  5. var
  6.   CharPos: DWord;
  7.   Row, Col: Integer;
  8. begin
  9.   CharPos := SendMessage(Memo1.Handle, EM_CHARFROMPOS, 0, MakeLParam(X, Y));
  10.   Row := (CharPos shr 16) and $FFFF;
  11.   Col  := (CharPos and $FFFF) - SendMessage(Memo1.Handle, EM_LINEINDEX, Row, 0);
  12.   Form1.Caption := ' Row: ' + inttostr(Row) + ' Col: ' + inttostr(Col);
  13. end;    

Avinash

  • Full Member
  • ***
  • Posts: 126
Re: What is the shortest path for search control in a char?
« Reply #2 on: February 12, 2022, 10:59:48 am »
Line 22 in the code below gives an error

Change

Code: Pascal  [Select][+][-]
  1. var separ:array[1..9] of char=(' ','.',',','(',')','[',']',':',';');
to
Code: Pascal  [Select][+][-]
  1. var separ: Set of Char = [' ','.',',','(',')','[',']',':',';'];
or
Code: Pascal  [Select][+][-]
  1. const separ = [' ','.',',','(',')','[',']',':',';'];
« Last Edit: February 12, 2022, 11:03:13 am by Avinash »

loaded

  • Hero Member
  • *****
  • Posts: 878
Re: What is the shortest path for search control in a char?
« Reply #3 on: February 12, 2022, 11:25:18 am »
Dear Master GetMem, thank you very much for the reply.
My goal is to detect the current word in the row where the mouse is located.
An obvious situation;
The mouse position can sometimes be in the middle of the word. For this, I need to determine the beginning and end of the word separately.
For this, I transferred the space and other characters used to separate words into a character type array.
I'm trying to find the beginning by going backwards from the mouse position, and the end by going forward. During this process, I had to compare the characters of the word with the array elements I defined.

Dear brother Avinash, thank you very much for your reply.
Thanks to you, my problem has been solved and I have learned a very important lesson.

I'm done at this stop in my project, now I can move on to the next stop.
Let's put the code here, maybe someone will need it someday.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Memo1MouseUp(Sender: TObject; Button: TMouseButton;  Shift: TShiftState; X, Y: integer);
  2. const
  3.   separ = [' ', '''', '<', '>', '/', '\', '=', '.', ',', '(', ')', '[', ']', '{', '}', ':', ';'];
  4. var
  5.   Lines, search: string;
  6.   LineRow, LineCol: longint;
  7.   sst, stp: integer;
  8. begin
  9.   if Button = mbleft then
  10.   begin
  11.     LineRow := Memo1.CaretPos.y;
  12.     LineCol := Memo1.CaretPos.x;
  13.     Lines := memo1.Lines.Strings[LineRow];
  14.  
  15.     if Lines <> '' then
  16.     begin
  17.       for sst := LineCol downto 0 do
  18.       begin
  19.         if (Lines[sst] in separ) then
  20.           break;
  21.       end;
  22.       for stp := LineCol to high(Lines) do
  23.       begin
  24.         if (Lines[stp] in separ) then
  25.           break;
  26.       end;
  27.       search := copy(Lines, sst + 1, stp - sst - 1);
  28.       Caption := search;
  29.     end;
  30.   end;
  31.  
  32. end;  



The more memory computers have, the less memory people seem to use. 😅

loaded

  • Hero Member
  • *****
  • Posts: 878
Re: [Solved] What is the shortest path for search control in a char?
« Reply #4 on: February 12, 2022, 11:43:52 am »
A word of caution as @GETMEM pointed out in a way that maybe you didn't understand why.
It is very normal that I do not understand, Because;  ;D
Master GetMem is an eagle at the peak of genius, and I am a sparrow in the bush.

Thank you very much for your warnings, Mr. Jamie.
« Last Edit: February 12, 2022, 11:45:25 am by loaded »
The more memory computers have, the less memory people seem to use. 😅

 

TinyPortal © 2005-2018