How to set the cursor to a TEdit field lazarus / 21-General ---------------------------------------------------------------------- Markus · 2025-04-07 14:04 I have a form with several edit fields (TEdit). Each Edit field uses OnEditingDone to check the input and give an error message in case the input was wrong (e. g. a letters instead of a number). How can I set the cursor into the incorrectly filled in edit field afterwards? If one uses the mouse or TAB to go to the next field, the cursor is then placed to the next edit field and the error message is displayed.  When clicking on the first field, the error message appears again, which is annoying. Edit.SetFocus does not change the cursor position. ---------------------------------------------------------------------- dsiders · 2025-04-07 14:52 Quote from: Markus on April 07, 2025, 04:04:13 pmI have a form with several edit fields (TEdit). Each Edit field uses OnEditingDone to check the input and give an error message in case the input was wrong (e. g. a letters instead of a number). How can I set the cursor into the incorrectly filled in edit field afterwards? If one uses the mouse or TAB to go to the next field, the cursor is then placed to the next edit field and the error message is displayed.  When clicking on the first field, the error message appears again, which is annoying. Edit.SetFocus does not change the cursor position. For TEdit, a couple of options come to mind. You can use the CaretPos property - or use SelStart and SelLength. https://lazarus-ccr.sourceforge.io/docs/lcl/stdctrls/tcustomedit.html ---------------------------------------------------------------------- Markus · 2025-04-07 16:08 CaretPos expects a TPoint. Not sure how to use this. Setting the value for example to the position of the Edit field (x=Left, y=Top) does not change the cursor position. SelStart and SelEnd does not the job either. ---------------------------------------------------------------------- dsiders · 2025-04-07 17:16 Quote from: Markus on April 07, 2025, 06:08:39 pmCaretPos expects a TPoint. Not sure how to use this. Setting the value for example to the position of the Edit field (x=Left, y=Top) does not change the cursor position. Then you did not read the help topic. It's character and line coordinates. Quote from: Markus on April 07, 2025, 06:08:39 pmSelStart and SelEnd does not the job either. There is no SelEnd... so no surprise that doesn't work. Do you Have AutoSelect enabled? Turn it off and SelStart works just fine. ---------------------------------------------------------------------- cdbc · 2025-04-07 17:17 Hi How about "Self.ActiveControl:= (Sender as TEdit);" in the 'EditingDone' event handler...? Regards Benny ---------------------------------------------------------------------- Bart · 2025-04-07 17:29 Quote from: Markus on April 07, 2025, 04:04:13 pm Each Edit field uses OnEditingDone to check the input and give an error message in case the input was wrong (e. g. a letters instead of a number). There are controls that enforce validation by themself. T(Float)SpinEdit(Ex) for numeric input. TMaskEdit for e.g. telephone numbers (with a fixed format), binary or hexadecimal input, ZIP-codes etc. If that doesn't suit your needs, do as others suggested: Set focus to the control (should be Sender), set AutSelect to False, use SelStart to set the correct position (note: SelStart counts in "graphemes", not in bytes, e.g. € counts as 1, most of the times roughly the same as Utf8Length/Utf8Pos). You can take a look a TMaskEdit's validation process: it will reset control to the offending edit. Bart ---------------------------------------------------------------------- dseligo · 2025-04-07 17:47 Maybe he set it up something like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Edit1EditingDone(Sender: TObject);begin  If Edit1.Text <> 'x' then  begin    ShowMessage('error in edit1');    Edit1.SetFocus;  end;end; procedure TForm1.Edit2EditingDone(Sender: TObject);begin  If Edit2.Text <> 'y' then  begin    ShowMessage('error in edit2');    Edit2.SetFocus;  end;end; When he goes to Edit2 he gets message 'error in edit1' and focus goes to Edit1. But then he immediately also receives 'error in edit2' (because focus has already shifted in Edit2, and EditingDone for Edit1 is called after that). I suggest that Markus make small demo program which shows what he wants to do. ---------------------------------------------------------------------- Markus · 2025-04-08 10:47 Sure. Here comes my code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TFrmMain.EdVBatteryCapacityEditingDone(Sender: TObject);var  status: integer;  P: Tpoint;begin  with Options do begin    val1(EdVBatteryCapacity.text,VBatteryCapacity,status);      if status<>0 then begin        EdVBatteryCapacity.Color:=ErrorColor;              // None of the following 6 lines do the job         P.x:=0; P.y:=0;         EdVBatteryCapacity.SetFocus;        EdVBatteryCapacity.SelStart:=0; // value 1 does not work either      EdVBatteryCapacity.Sellength:=length(EdVBatteryCapacity.text);        EdVBatteryCapacity.CaretPos:=P;        FrmMain.ActiveControl:=EdVBatteryCapacity;         ErrorMessage('Battery capacity must be a real number.','Error');      exit;      end else        EdVBatteryCapacity.Color:=clDefault;  end;end;  procedure TFrmMain.EdVBatteryEfficiencyEditingDone(Sender: TObject);var  status: integer;begin  with Options do begin    val1(EdVBatteryEfficiency.text,VBatteryEfficiency,status);      if status<>0 then begin        EdVBatteryEfficiency.Color:=ErrorColor;       ErrorMessage('Battery Efficiency must be a real number.','Error');        exit;      end else        EdVBatteryEfficiency.Color:=clDefault;  end;end; I like to have my own Error-checking procedure, for instance where the problematic edit field is changing color in case of an error.