Page 1 of 1

Select a whole line in TSynEdit, TMemo or TRichEdit

PostPosted: August 4th, 2004, 2:03 pm
by m_b
How can I select a whole line in TSynEdit, TMemo or TRichEdit if I've got it's index?
I know that I can use the SelStart and SelLength, but when I do that if the line is longer than the component width - the component scrolls to the end of the line, and this is really annoying to the user - he has to scroll back, what I want is to select the whole line without scrolling to the end of it. So, any suggestions???

m_b

PostPosted: August 4th, 2004, 3:09 pm
by Kambiz
The following code may help you.

Code: Select all
function SelectLine(EditHandle: THandle; LineIndex: Integer; CaretInView: Boolean): Boolean;
var
  LineStart: Integer;
  LineLength: Integer;
begin
  Result := False;
  LineStart := SendMessage(EditHandle, EM_LINEINDEX, LineIndex, 0);
  if LineStart >= 0 then
  begin
    LineLength := SendMessage(EditHandle, EM_LINELENGTH, LineStart, 0);
    SendMessage(EditHandle, EM_SETSEL, LineStart, LineStart + LineLength);
    if CaretInView then SendMessage(EditHandle, EM_SCROLLCARET, 0, 0);
    Result := True;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if not SelectLine(Memo1.Handle, SpinEdit1.Value, False) then
    ShowMessage('Line index out of bounds');
end;

PostPosted: August 5th, 2004, 11:07 am
by m_b
I'm sorry Kambiz, but this function does nothing, I've tried it with SynEdit and Memo, and nothing gets selected... Is there another way?

PostPosted: August 5th, 2004, 11:34 am
by Kambiz
I have no idea about SynEdit, but I'm sure the function works for TMemo and TRichEdit.

Maybe the HideSelection property of the memo is True, and you don't see the selection.

PostPosted: August 5th, 2004, 4:18 pm
by m_b
You know, that may be possible, I'll check that and let you know if it worked...

PostPosted: August 8th, 2004, 2:35 pm
by m_b
Yep, it works with memo & richedit, but not in synedit... Does anyone know how to get it to work with synedit?