ComboBox - colocando dentro de uma grid |
Top Previous Next |
|
// Exemplo completo de como colocar uma combo dentro de uma grid
TForm1 = class(TForm) StringGrid1: TStringGrid; ComboBox1: TComboBox; procedure FormCreate(Sender: TObject); procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); procedure ComboBox1Change(Sender: TObject); end;
var Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject); begin // ajusta a altura do ComboBox com a altura da linha do StringGrid StringGrid1.DefaultRowHeight := ComboBox1.Height; end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); var R: TRect; begin // testa se entrou na coluna certa // e se não é linha de título if (ACol = 3) and (ARow <> 0) and (gdFocused in State) then begin // pega o retângulo da célula R := StringGrid1.CellRect(ACol, ARow); // posiciona em relação à Form 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.SetBounds(R.Left + 1,R.Top + 1, (R.Right + 1) - R.Left,(R.Bottom + 1) - R.Top); // posiciona a combobox no valor da célula Combobox1.ItemIndex := Combobox1.Items.IndexOf(StringGrid1.Cells[ACol,ARow]); Combobox1.Visible := True; ComboBox1.BringToFront; end else Combobox1.Visible := False; end;
procedure TForm1.ComboBox1Change(Sender: TObject); begin with StringGrid1 do Cells[Col, Row] := ComboBox1.Text; end; |