我用下面的方法创建里一组checkbox在stringgrid里面   可是创建以后的checkbox 点击无效啊 就是无法用鼠标改变其   checked 属性  但是如果 cbList[i].Parent:=Form1; 就可以正长点击了,这在StringGrid里该怎么做呢?
  还有 cbList[i].Parent:=StringGrid1;时候 连onclick事件也不响应了 form1里
  就可以的,  还有动态控件数组的事件里如何区分他们的 对应的是哪个控件呢 ?
  就是 我click一下 怎么知道这是 数组里具体哪个对象呢?
  因为cbList[i].Parent:=Form1 我就无法准确控制checkbox在stringgrid中的位置了
  所以只有用此办法了
  
  for i :=0 to Length(cbList)-1 do
  begin
    cbList[i]:=TcheckBox.Create(StringGrid1);
    cbList[i].Create(StringGrid1);
    cbList[i].Name :='cb'+inttostr(i);
    cbList[i].Parent:=StringGrid1;  //zheli 
    cbList[i].Width :=20;
    cbList[i].OnClick:=CheckboxClik;
    cbList[i].Caption:='';
    cbList[i].Top :=i*22+1;
    cbList[i].left :=100 ;
  end;
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, StdCtrls;type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Button3: TButton;
  private
     { Private declarations }
  public
    cbList:array of TCheckBox;
    procedure CheckboxClik(sender:TObject);
  end;
。。
procedure TForm2.CheckboxClik(sender: TObject);
var i:integer;
begin  这里如何知道响应哪个对象的事件呢
  cbList[i].Parent:=StringGrid1时 这里还不响应怎么办法?
end;

解决方案 »

  1.   

    不要使用动态创建的,你可以直接放一个,设置为不可见,然后在stringGrid的onedit事件中
    让他显示,然后在combobox的onExit事件里面写stringGrid的值,就好了,很简单的,如果需要我写的一套控件里面可以实现,只要你设定combobox的item值,以后别的都是由我的控件来实现,如果需要,留下mail
      

  2.   

    用那么多CheckBox太浪费资源,我认为应该用两幅图片画出来,下面是我写的测试程序
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, Grids;type
      TForm1 = class(TForm)
        StringGrid1: TStringGrid;
        procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
          Rect: TRect; State: TGridDrawState);
        procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
          var CanSelect: Boolean);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
        procedure DrawCheckBox(ARect:TRect;IsChecked:Boolean);
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    var lRect :Trect;
    begin
      if (Acol=1) and (Arow<>0) then
      begin
        if StringGrid1.Cells[aCol,ARow]='0' then
          DrawCheckBox(Rect,False)
        else
          DrawCheckBox(Rect,True);
      end;
    end;procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
      ARow: Integer; var CanSelect: Boolean);
    var lRect :TRect;
    begin
      if (Acol=1) and (Arow<>0) then
      begin
        if StringGrid1.Cells[aCol,aRow]='0' then StringGrid1.Cells[aCol,aRow]:='1'
        else StringGrid1.Cells[aCol,aRow]:='0';
      end;
    end;procedure TForm1.DrawCheckBox(ARect:TRect;IsChecked:Boolean);
    var lRect :TRect;
    begin
      lrect := Classes.rect(0,0,12,12);
      OffsetRect(lRect,ARect.Left,ARect.Top);
      OffsetRect(lRect,(Arect.Right-ARect.Left-12) div 2,(Arect.Bottom-Arect.Top-12) div 2);
      with StringGrid1 do
      begin
        Canvas.Brush.Color := Canvas.Brush.Color;
        Canvas.FillRect(aRect);
        Canvas.Brush.Color := clBlack;
        Canvas.FrameRect(lRect);
        if IsChecked then
        begin
          Canvas.Pen.Color := clBlack;
          Canvas.Polyline([Point(lRect.Left,lRect.Bottom-5),Point(lRect.Left+4,lRect.Bottom-1),Point(lRect.Right,lRect.Top+3)]);
        end;
      end;
    end;procedure TForm1.FormCreate(Sender: TObject);
    var i:Integer;
    begin
      for i:=1 to StringGrid1.RowCount-1 do
        StringGrid1.Cells[1,i]:= '0';
    end;
      

  3.   

    wxjh(农民)的方法比较方便。。:)