Witam!!!
Posiadam komponent Stringrid, w którym mam Obiekt typu Combobox, ale chciałbym , aby po wciśnięciu klawisza taba, aby z komórek, które zawierają listy ComboBox, przeskakiwał od razu do kolejnego pola StringGrida, teraz gdy wcisnę tab, ustawia Focusa na StrinGrida, ale w komórce , w której jest ComboBox oraz aby do komórek StringGrida z ComboBoxami nie można było wpisać innych wartości niż te, które znajdują się na liście ComboBoxa. W tej chwili, w te okienka można wpisać dowolną wartość, niekoniecznie z listy ComboBoxa i zostaje ona tam zapisana.
Poniżej zamieszczam kod programu:
unit StrinGridzComboBoxem;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,Grids,StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
ComboBox1: TComboBox;
procedure FormCreate(Sender: TObject);
procedure ComboBox1CloseUp(Sender: TObject);
procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
var CanSelect: Boolean);
procedure ComboBox1Exit(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.ComboBox1CloseUp(Sender: TObject);
begin
if ComboBox1.ItemIndex <> -1 then
begin
StringGrid1.Cells[StringGrid1.Col, StringGrid1.Row] := ComboBox1.Text;
StringGrid1.SetFocus;
end;
end;
procedure TForm1.ComboBox1Exit(Sender: TObject);
begin
if ComboBox1.ItemIndex <> -1 then
begin
if (StringGrid1.Col = 2) or (StringGrid1.Col = 3)then
begin
StringGrid1.Cells[StringGrid1.Col, StringGrid1.Row] := ComboBox1.Items[ComboBox1.ItemIndex];
StringGrid1.SetFocus;
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
StringGrid1.DefaultRowHeight := ComboBox1.Height;
end;
procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
var CanSelect: Boolean);
var
R: TRect;
begin
if ACol in [2,3] then
begin
R := TStringGrid(Sender).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;
with ComboBox1 do
begin
ItemIndex := Items.IndexOf(StringGrid1.Cells[ACol, ARow]);
Width := R.Right - R.Left;
Left := R.Left + 1;
Top := R.Top + 1;
Visible := True;
end
end
else
ComboBox1.Visible := False;
end;
end.