ComboBox em StringGrid

15/04/2004

0

Estou tentando usar o código abaixo para inserir um combobox dentro de uma celula do StringGrid quando este recebe o foco, porém o resultado não é o esperado. (As colunas não correspondem ou não aparece). Aguém pode me ajudar?
procedure TForm1.ST1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
Var
R: Trect;
begin
if (ACol = 1) and (ARow <> 0) and (gdFocused in State) then
begin
R:= St1.CellRect(Acol,ARow);
R.Left := R.Left + St1.Left;
R.Right := R.right + St1.Left;
R.Top := R.Top + St1.Top;
R.Bottom := R.Bottom + St1.Top;
Cb1.SetBounds(R.Left + 1,R.Top +1,
(R.Right +1) - R.Left,(R.Bottom +1) - R.Top);
CB1.ItemIndex := CB1.Items.IndexOf(St1.Cells[Acol,ARow]);
Cb1.Visible := true;
Cb1.BringToFront;
end
else
Cb1.Visible := False;
end;


Ronangledson

Ronangledson

Responder

Posts

15/04/2004

Lucas Silva

Esse stringGrid está dentro de alguma coisa? tipo groupBox, e etc..


Responder

15/04/2004

Ronangledson

[quote=´Lucas Alves Silva´]Esse stringGrid está dentro de alguma coisa? tipo groupBox, e etc..[/quote
Sim ele esta dentro de um painel]


Responder

15/04/2004

Lucas Silva

Tenta esse código ai, se não der certo me dá o toque.
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
ComboBox1: TComboBox;
 
procedure FormCreate(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
procedure ComboBox1Exit(Sender: TObject);
procedure StringGrid1SelectCell
(Sender: TObject; Col, Row: Integer;
var CanSelect: Boolean);
 
private
{ Private declarations }
 
public
{ Public declarations }
 
end;
 
var
Form1: TForm1;
 
implementation
 
{$R *.DFM}
 
// Evento OnCreate do Form
procedure TForm1.FormCreate(Sender: TObject);
begin
{ Ajusta a altura do ComboBox com a altura da linha do StringGrid}
StringGrid1.DefaultRowHeight := ComboBox1.Height;
{Esconde o ComboBox}
ComboBox1.Visible := False;
end;
 
// Evento OnChange do componente ComboBox
procedure TForm1.ComboBox1Change(Sender: TObject);
begin
StringGrid1.Cells[StringGrid1.Col,StringGrid1.Row] := ComboBox1.Items[ComboBox1.ItemIndex];
ComboBox1.Visible := False;
StringGrid1.SetFocus;
end;
 
// Evento OnExit do componente ComboBox
procedure TForm1.ComboBox1Exit(Sender: TObject);
begin
StringGrid1.Cells[StringGrid1.Col,StringGrid1.Row] := ComboBox1.Items[ComboBox1.ItemIndex];
ComboBox1.Visible := False;
StringGrid1.SetFocus;
end;
 
// Evento OnSelectCell do componente StringGrid
procedure TForm1.StringGrid1SelectCell(Sender: TObject; Col, Row: Integer;
var CanSelect: Boolean);
var
R: TRect;
begin
if ((Col = 3) AND
(Row <> 0)) then begin
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;
ComboBox1.Visible := True;
ComboBox1.SetFocus;
end;
CanSelect := True;
end;



Responder

15/04/2004

Ronangledson

[quote:2a563602b9=´Lucas Alves Silva´]Tenta esse código ai, se não der certo me dá o toque.
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
ComboBox1: TComboBox;
 
procedure FormCreate(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
procedure ComboBox1Exit(Sender: TObject);
procedure StringGrid1SelectCell
(Sender: TObject; Col, Row: Integer;
var CanSelect: Boolean);
 
private
{ Private declarations }
 
public
{ Public declarations }
 
end;
 
var
Form1: TForm1;
 
implementation
 
{$R *.DFM}
 
// Evento OnCreate do Form
procedure TForm1.FormCreate(Sender: TObject);
begin
{ Ajusta a altura do ComboBox com a altura da linha do StringGrid}
StringGrid1.DefaultRowHeight := ComboBox1.Height;
{Esconde o ComboBox}
ComboBox1.Visible := False;
end;
 
// Evento OnChange do componente ComboBox
procedure TForm1.ComboBox1Change(Sender: TObject);
begin
StringGrid1.Cells[StringGrid1.Col,StringGrid1.Row] := ComboBox1.Items[ComboBox1.ItemIndex];
ComboBox1.Visible := False;
StringGrid1.SetFocus;
end;
 
// Evento OnExit do componente ComboBox
procedure TForm1.ComboBox1Exit(Sender: TObject);
begin
StringGrid1.Cells[StringGrid1.Col,StringGrid1.Row] := ComboBox1.Items[ComboBox1.ItemIndex];
ComboBox1.Visible := False;
StringGrid1.SetFocus;
end;
 
// Evento OnSelectCell do componente StringGrid
procedure TForm1.StringGrid1SelectCell(Sender: TObject; Col, Row: Integer;
var CanSelect: Boolean);
var
R: TRect;
begin
if ((Col = 3) AND
(Row <> 0)) then begin
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;
ComboBox1.Visible := True;
ComboBox1.SetFocus;
end;
CanSelect := True;
end;
[/quote:2a563602b9]



Valeu meu amigo espero porder retribui-lo agum dia.


Responder

Assista grátis a nossa aula inaugural

Assitir aula

Saiba por que programar é uma questão de
sobrevivência e como aprender sem riscos

Assistir agora

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar