Custom InPlace Editor in StringGrid

by Mar 26, 1998

 Question and Answer Database

FAQ2434D.txt   Custom InPlace Editor in StringGrid
Category   :VCL
Platform    :All
Product    :Delphi 3.x  

Question:
How can I popup my own inplace editor (such as a combobox)
in a string grid cell?


Answer:
The following example demonstrates popping up a Combobox
as an inplace editor to a Stringrid component.

Example:

procedure TForm1.FormCreate(Sender: TObject);
begin
 {The combobox height is not settable, so we will}
 {instead size the grid to fit the combobox!}
  StringGrid1.DefaultRowHeight := ComboBox1.Height;
 {Hide the combobox}
  ComboBox1.Visible := False;
end;

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
 {Get the ComboBox selection and place in the grid}
  StringGrid1.Cells[StringGrid1.Col,
                    StringGrid1.Row] :=
    ComboBox1.Items[ComboBox1.ItemIndex];
  ComboBox1.Visible := False;
  StringGrid1.SetFocus;
end;

procedure TForm1.ComboBox1Exit(Sender: TObject);
begin
 {Get the ComboBox selection and place in the grid}
  StringGrid1.Cells[StringGrid1.Col,
                    StringGrid1.Row] :=
    ComboBox1.Items[ComboBox1.ItemIndex];
  ComboBox1.Visible := False;
  StringGrid1.SetFocus;
end;

procedure TForm1.StringGrid1SelectCell(Sender: TObject; Col,
 Row: Integer;  var CanSelect: Boolean);
var
  R: TRect;
begin
  if ((Col = 3) AND
      (Row <> 0)) then begin
   {Size and position the combo box to fit the cell}
    R := StringGrid1.CellRect(Col, Row);
    R.Left := R.Left + StringGrid1.Left;
    R.Right := R.Right + StringGrid1.Left;
    R.Top := R.Top + StringGrid1.Top;
    R.Bottom := R.Bottom + StringGrid1.Top;
    ComboBox1.Left := R.Left + 1;
    ComboBox1.Top := R.Top + 1;
    ComboBox1.Width := (R.Right + 1) - R.Left;
    ComboBox1.Height := (R.Bottom + 1) - R.Top;
   {Show the combobox}
    ComboBox1.Visible := True;
    ComboBox1.SetFocus;
  end;
  CanSelect := True;
end;




7/16/98 4:31:28 PM
 

Article originally contributed by