Displaying TRichEdit row and column in status bar

211 Views Asked by At

We are adding an ASCII editor to our application, and we are using TRichEdit in Delphi XE7. We need to display the Row and Column in the status bar, but have not found any code that works with the Key down function. The code below is the current code we are using. The code below works perfect with the mouse down, but the key down is not consistent.

Row := SendMessage(asciieditor.Handle, EM_LINEFROMCHAR, asciieditor.SelStart, 0);    
Col := asciieditor.SelStart - SendMessage(asciieditor.Handle, EM_LINEINDEX, Row, 0);
1

There are 1 best solutions below

1
Andreas Rejbrand On

When you want to display the caret position in a Rich Edit control in the status bar, the usual way is to use the editor's OnSelectionChange event:

procedure TForm1.RichEdit1SelectionChange(Sender: TObject);
begin
  StatusBar1.Panels[0].Text := Format('Row: %d  Col: %d',
    [RichEdit1.CaretPos.Y + 1, RichEdit1.CaretPos.X + 1]);
end;

This is fired every time the caret pos or selection end point is moved, which is precisely what you need.

Attempting to keep the status bar updated using mouse and keyboard events is not a good idea. For instance, what if the user is editing or typing without using the mouse or keyboard? (For instance, programmatically or using speech recognition.)

And using OnIdle is a waste of CPU cycles.