• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 178
  • Last Modified:

Controls in a Grid Enviroment

I would like to place a TCombobox in a TCustomGrid enviroment. As you fill out the data in the grid, the user can use these controls. As you add a row a new control is available to be filled in.
0
kehansen
Asked:
kehansen
  • 3
1 Solution
 
intheCommented:
here a example using stringgrid:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Grids;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    ComboBox1: TComboBox;
    procedure FormCreate(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
    procedure ComboBox1Exit(Sender: TObject);
    procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

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; ACol,ARow: Integer;  var CanSelect: Boolean);
var
  R: TRect;
begin
// if ((ACol = 1) AND
  //    (ARow <> 0)) then begin
   {Size and position the combo box to fit the cell}
    R := StringGrid1.CellRect(ACol, ARow);
    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;
    CanSelect := True;
  end;
//end;

end.

maybe helps
Regards Barry
0
 
kehansenAuthor Commented:
Thanks for your input.  I could make this work but with multiple rows and controls on the fly it would be cumbersome.  I was looking for a more elegent solution. I seem to remember something about hanging a control on a pointer to the cell.  
0
 
rwilson032697Commented:
You could take a look a TAdvStringGrid (downloadable from here: http://www.tmssoftware.com/).

Cheers,

Raymond.
0
 
kehansenAuthor Commented:
Thanks for your suggestion, but I actually have a custom component I need to use.  I knew if someone could get it to work with a TCombobox.  I could get it to work with mine.
0
 
kehansenAuthor Commented:
My expectation were a little higher.  I was hoping I would find someone who had done this before and would have lots of code.  This will work. Thanks
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now