1

I have function that returns the index of a character GetCharFromPos(Pt: TPoint): Integer;

now i wanted to get character of that position. like GetCharByIndex(Index: Integer): Char;

XBasic3000
  • 3,418
  • 5
  • 46
  • 91

2 Answers2

6

The efficient way to do this using pure VCL is to use SelStart, SelLength and SelText.

function GetCharByIndex(Index: Integer): Char;
begin    
  RichEdit.SelStart := Index;
  RichEdit.SelLength := 1;
  Result := RichEdit.SelText[1];
end;

You'll likely want to save away the selection before modifying it, and then restore it once you have read the character.


This is however a rather messy way to read a character. If you are prepared to use raw Win32 API then you can make use of EM_GETTEXTRANGE.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
1

Here is how you return the character at a given index from a TRichEdit:

Result := RichEdit1.Text[Index];
Bruce McGee
  • 15,076
  • 6
  • 55
  • 70