请教一下大家,我有三个窗口,一个form1,一个form2,另外一个attribform,其中from1和form2中各有一个dbgrideh,而且这二个窗口都有事件打开attribform窗口,现在我想要做的就是,当我show显示attrib窗口,我要知道attribform窗口是从那个窗口打开的,好调用源窗口dbgrideh里的字段名。我在from1,form2打开attribform的事件中写了一条语名:
attribform.ckmc:=窗口名.name;然后在attribform中定义全局变量 :
ckmc:Tform;在attribform的onShow事件中写到:
procedure Tattribform.FormShow(Sender: TObject);
var
  i:integer;
begin
  combobox1.Clear;
  for i:=0 to ckmc.dbgrideh1.Columns.Count-1 do
  begin
    combobox1.Items.add(distform.DBGridEh1.Columns[i].FieldName);
  end
end;
怎么示是错误了。
[Error] attrib.pas(71): Undeclared identifier: 'dbgrideh1'求大家给个方示,谢谢

解决方案 »

  1.   

    ckmc的类型有问题
    dbgrideh1如果是属于Form1的
    就得是
    ckmc:TForm1;
    是属于属于Form2的
    就得是
    ckmc:TForm2;
    而不能笼统的写为
    ckmc:TForm;
    TForm是个基类肯定不包含dbgrideh1了
      

  2.   

    简单的方法是:加一个公用成员creator: string;  其实也就是加个标识.attribform:= tattribform.create(nil);
    attribform.creator:= 'form1'; or 'form2'
    attribform.showmodal;
      

  3.   

    再简单点,用Form的Tag属性:
    AttribForm := TattribForm.Create(nil);
    AttribForm.Tag =1; \\如果是Form2 则Tag的值可设为2
    AttribForm.ShowModal;
      

  4.   

    procedure Tdistform.N2Click(Sender: TObject); ////////////////这是form1窗口
    begin
      application.CreateForm(Tattribform,attribform);
      attribform.Tag:=1;
      attribform.Show;
    end;
    procedure Tdist_cform.N2Click(Sender: TObject);/////////////这是form2窗口
    begin
      application.CreateForm(Tattribform,attribform);
      attribform.Tag:=2;
      attribform.Show;
    end;procedure Tattribform.FormShow(Sender: TObject);/////这是attribform窗口的显示事件
    var
      i:integer;
      a:Tdistform;
      b:Tdist_cform;
    begin
      combobox1.Clear;
      if attribform.Tag=1 then
      begin
        for i:=0 to a.dbgrideh1.Columns.Count-1 do
        begin
          combobox1.Items.add(a.DBGridEh1.Columns[i].FieldName);
        end
      end
      else if attribform.Tag=2 then
      begin
        for i:=0 to b.dbgrideh1.Columns.Count-1 do
        begin
          combobox1.Items.add(b.DBGridEh1.Columns[i].FieldName);
        end
      end
    end;
    楼上的大侠,我这样写了为什么报错了 
    ---------------------------
    Debugger Exception Notification
    ---------------------------
    Project Project1.exe raised exception class EAccessViolation with message 'Access violation at address 0041E828 in module 'Project1.exe'. Read of address 00000008'. Process stopped. Use Step or Run to continue.
    ---------------------------
    OK   Help   
    ---------------------------麻烦你了,谢谢
      

  5.   


    procedure Tattribform.FormShow(Sender: TObject);
    var
      i:integer;
      a:string;
    begin
      combobox1.Clear;
      a:=distfomr;
        for i:=0 to a.dbgrideh1.Columns.Count-1 do
        begin
          combobox1.Items.add(a.DBGridEh1.Columns[i].FieldName);
        end这位高人,你看我这样写跟你的方法是不是一样的,但这样,变量a中根本引用不出dbgrideh,所以就报错
      

  6.   

    procedure Tattribform.FormShow(Sender: TObject);
    var
      i:integer;
      a:string;
    begin
      combobox1.Clear;
      a:=distform;  //////////// 这里打错了
      for i:=0 to a.dbgrideh1.Columns.Count-1 do
      begin
      combobox1.Items.add(a.DBGridEh1.Columns[i].FieldName);
      end纠正一下,上面打错了