如何使用TStringGrid中的Objects?如何在其中嵌入其它控件?请简单举例。(50分)

  • 主题发起人 主题发起人 yanghaijun
  • 开始时间 开始时间
在TStringGrid中嵌入TComboBox:

unit Unit1;

interface

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

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

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.CMDialogKey(var msg: TCMDialogKey);
begin
If Activecontrol = Combobox1 Then Begin
If msg.CharCode = VK_TAB Then Begin
// set focus back to the grid and pass the tab key to it
stringgrid1.setfocus;
stringgrid1.perform( WM_KEYDOWN, msg.charcode, msg.keydata );
// swallow this message
msg.result := 1;
Exit;
End;
End;
inherited;
end;

procedure TForm1.ComboBox1Exit(Sender: TObject);
begin
with sender as TCombobox do begin
hide;
if itemindex >= 0 then
with stringgrid1 do
cells[ col, row ] := items[itemindex];
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
combobox1.visible := false;
end;

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);
var
R: TRect;
org: TPoint;
begin
With Sender As TStringgrid Do
If (ACol = 2) and (ARow >= FixedRows) Then Begin
// entered the column associated to the combobox
// get grid out of selection mode
perform( WM_CANCELMODE, 0, 0 );
// position the control on top of the cell
R := CellRect( Acol, Arow );
org:= Self.ScreenToClient( ClientToScreen( R.topleft ));
With combobox1 do begin
setbounds( org.X, org.Y, r.right-r.left, height );
itemindex := Items.IndexOf( Cells[ acol, arow ] );
Show;
BringTofront;
// focus the combobox and drop down the list
SetFocus;
DroppedDown := true;
end;
End;
end;

end.
 
unit Unit1;

interface

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

type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
procedure FormCreate(Sender: TObject);
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
with StringGrid1 do
begin
Objects[1,1]:=TCombobox.Create(Self);
with Objects[1,1] as TCombobox do
begin
Parent:=StringGrid1;
visible:=False;
Items.Add('Item 1');
Items.Add('Item 2');
Items.Add('Item 3');
Items.Add('Item 4');
end;
end;
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
with StringGrid1 do
begin
if Objects[ACol,ARow]<>nil then
begin
with Objects[Acol,ARow] as TCombobox do
begin
if not visible then
begin
SetBounds(Rect.Left,Rect.Top,ColWidths[ACol],100);
Visible:=True;
end;
end;
end;
end;
end;

end.
 
to pyramid:

如果我把TCombobox换成TCheckBox,如何可以让CheckBox的Checked随Click变化?
 
你是需要改变CheckBox在Grid的位置(在Grid的SelectCell事件中处理,即可将CheckBox放在指定位置),还是改变CheckBox的Checked的值(只要你Click,它的值一定会改变的)。
 
我要改变Checked值,Click时不变!
如何实现?谢谢!
 
Click时,Checked值一定是改变的,只不过你可能没有保存checked的值,在CheckBox的OnClick事件中加入下面的处理:
with StringGrid1 do
begin
if (Sender as TCheckBox).Checked then
Cells[Col,Row]:='1'
else
Cells[Col,Row]:=''
end;
在放置CheckBox的同时做如下处理:
CheckBox1.Checked:=StringGrid1.Cells[ACol,ARow]='1'
即可
我的OICQ:1713617,若有问题请直接与我交流。
 
to pyramid:

多谢!
可是当Checkbox是动态创建时,如何定义其OnClick事件呢?我Click时没执行我定义的事件。Email:wang_y_g@263.net
 
多人接受答案了。
 
后退
顶部