Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Forms 按窗体名称操作窗体组件_Forms_Delphi_Ado - Fatal编程技术网

Forms 按窗体名称操作窗体组件

Forms 按窗体名称操作窗体组件,forms,delphi,ado,Forms,Delphi,Ado,我有一个表单类,用于从中生成多个表单对象。我重命名了生成的每个表单 我不知道如何访问指定的表单组件而不影响其他生成的表单组件 我使用过此代码,但最后创建的表单的组件始终受到影响: procedure TmainForm.OFBloqueButtonClick(Sender: TObject); begin if (mainForm.tabSheetBool = True) AND (mainForm.OFID <> '') then begin if OF_bl

我有一个表单类,用于从中生成多个表单对象。我重命名了生成的每个表单

我不知道如何访问指定的表单组件而不影响其他生成的表单组件

我使用过此代码,但最后创建的表单的组件始终受到影响:

procedure TmainForm.OFBloqueButtonClick(Sender: TObject);
begin
    if (mainForm.tabSheetBool = True) AND (mainForm.OFID <> '') then begin
       if OF_bloque = False then begin
           Application.CreateForm(TOFOperationsForm, OFOperationsForm);
           OFOperationsForm.Caption := 'Bloquer OF';
           OFOperationsForm.Name := 'OFBloqueForm';
           OF_bloque := True;
       end;
       OFOperationsForm.BringToFront;
       OFOperationsForm.ADOOF.SQL.Text := 'SELECT * FROM OFTab WHERE ID=''' + OFID + '''';
       OFOperationsForm.ADOOF.Open;
    end;
end;

我的目标是操作所需表单的指定ADO查询


谢谢。

创建表单类的多个实例时,不应在表单单元中使用IDE声明的变量。事实上,我认识的大多数Delphi开发人员在创建新表单时都会删除该var,只有应用程序的主表单会保留它,因为dpr需要它

此外,您不需要使用FindWindow查找表单,甚至不需要迭代应用程序或屏幕的表单。创建本地变量时只需使用它:

procedure TmainForm.OFBloqueButtonClick(Sender: TObject);
var
  NewForm: TOFOperationsForm;
begin
    NewForm := nil;
    if (mainForm.tabSheetBool = True) AND (mainForm.OFID <> '') then begin
       if OF_bloque = False then begin
           NewForm := TOFOperationsForm.Create(Application);
           NewForm.Caption := 'Bloquer OF';
           NewForm.Name := 'OFBloqueForm';  //<== You'll need to make this unique!
           OF_bloque := True;
       end;
       if Assigned(NewForm) then begin
         NewForm.BringToFront;
         NewForm.ADOOF.SQL.Text := 'SELECT * FROM OFTab WHERE ID=''' + OFID + '''';
         NewForm.ADOOF.Open;
       end;
    end;
end;
并将按钮单击处理程序中的代码更改为:

procedure TmainForm.OFBloqueButtonClick(Sender: TObject);
begin
    if (mainForm.tabSheetBool = True) AND (mainForm.OFID <> '') then begin
       if OF_bloque = False then begin
           FLastFormCreated := TOFOperationsForm.Create(Application);
           FLastFormCreated.Caption := 'Bloquer OF';
           FLastFormCreated.Name := 'OFBloqueForm';
           OF_bloque := True;
       end;
       if Assigned(FLastFormCreated) then begin
         FLastFormCreated.BringToFront;
         FLastFormCreated.ADOOF.SQL.Text := 'SELECT * FROM OFTab WHERE ID=''' + OFID + '''';
         FLastFormCreated.ADOOF.Open;
       end;
    end;
end;

我已经在指定的支票上离开了,因为没有剩下的代码,我无法判断在逻辑上是否可能在FLastFormCreated至少收到一次引用的情况下到达BringToFront,即在其他地方设置为True。

我认为现在发生的是创建多个表单,删除指向以前表单的指针,最后你会得到一个指向最后创建的表单的指针

您可以迭代Screen.Forms以获得对所需表单的引用,下面是一个示例:

procedure TForm1.Button1Click(Sender: TObject);
var
  i: integer;
  NewForm: TForm2;
begin
  // create two forms, then show the first form
  Application.CreateForm(TForm2, NewForm);
  NewForm.Name := 'FirstForm';
  NewForm.Caption := 'This form was created 1st';
  Application.CreateForm(TForm2, NewForm);
  NewForm.Name := 'SecondForm';
  NewForm.Caption := 'This form was created 2nd';
  // iterate Screen.Forms to access the first form by name
  for i := 0 to Screen.FormCount - 1 do
    if Screen.Forms[i].Name = 'FirstForm' then
      Screen.Forms[i].Show;  // shows the first form
end;
解决办法是:

unit main;

interface

uses
    Windows, Messages, ... , OF_operations;

type
    TmainForm = class(TForm)
    ...
    ...
    ...
    private 
    ...
    public
         OFBloqueForm, OFCancelForm, OFDelaisClientForm, OFInspectionForm: TOFOperationsForm;
    end;

// Bloquer OF
procedure TmainForm.OFBloqueButtonClick(Sender: TObject);
begin
    if (mainForm.tabSheetBool = True) AND (mainForm.OFID <> '') then begin
        if OF_bloque = False then begin
            OFBloqueForm := TOFOperationsForm.Create(Application);
            OFBloqueForm.Caption := 'Bloquer OF';
            OFBloqueForm.Name := 'OFBloqueForm';
            OF_bloque := True;
        end;
        OFBloqueForm.BringToFront;
        OFBloqueForm.ADOOF.SQL.Text := 'SELECT * FROM OFTab WHERE ID=''' + OFID + '''';
        OFBloqueForm.ADOOF.Open;
    end;
end;

如果以后的代码不依赖于新表单的Name属性,则不需要设置该属性,因此也可以从此处的代码示例中删除。我命名该表单是为了按其名称调用它。TLama我猜of_bloque将阻止创建第二个表单。但不确定。问题的标题是按表单名称操纵表单组件-您的代码在哪里/如何使用表单名称?这未显示。其他方法是否使用FindComponent,使用表单名称而不是表单变量?抱歉,此问题没有提供有关主要问题的足够信息。OF_bloque是一个标志,用于指示“已存在表单,请勿创建另一个表单”我的目标是手动填充所需表单的指定ADO查询。-这需要使变量可以从其他方法访问+1我认识的大多数Delphi开发人员在创建新表单时立即删除该变量…但我得到了访问冲突错误。NewForm是此例程的本地变量。因此,“_bloque”是多余的,当为真时会导致OP的AV。让NewForm成为TMainForm或其单位的成员。问题是什么?仅仅更改引用的单位就解决了吗?
unit main;

interface

uses
    Windows, Messages, ... , OF_operations;

type
    TmainForm = class(TForm)
    ...
    ...
    ...
    private 
    ...
    public
         OFBloqueForm, OFCancelForm, OFDelaisClientForm, OFInspectionForm: TOFOperationsForm;
    end;

// Bloquer OF
procedure TmainForm.OFBloqueButtonClick(Sender: TObject);
begin
    if (mainForm.tabSheetBool = True) AND (mainForm.OFID <> '') then begin
        if OF_bloque = False then begin
            OFBloqueForm := TOFOperationsForm.Create(Application);
            OFBloqueForm.Caption := 'Bloquer OF';
            OFBloqueForm.Name := 'OFBloqueForm';
            OF_bloque := True;
        end;
        OFBloqueForm.BringToFront;
        OFBloqueForm.ADOOF.SQL.Text := 'SELECT * FROM OFTab WHERE ID=''' + OFID + '''';
        OFBloqueForm.ADOOF.Open;
    end;
end;