Mega Code Archive

 
Categories / Delphi / VCL
 

Placing a grid in edit mode programatically

Question: How can I programmatically place a grid into edit mode and move the cursor to a predetermined position in the edit box? Answer: Place the grid in editor mode, then get a handle to the edit box and send a EM_SETSEL message, passing the initial position where you want the cursor to appear, and the positon of the ending selection (if you wish to highlight text). The following example sets the cursor to the second character in the edit control, and does not select any text. Example: procedure TForm1.Button1Click(Sender: TObject); var h : THandle; begin Application.ProcessMessages; DbGrid1.SetFocus; DbGrid1.EditorMode := true; Application.ProcessMessages; h := Windows.GetFocus; SendMessage(h, EM_SETSEL, 2, 2); end;