表单创建相同的时间

时间:2014-10-24 11:09:18

标签: delphi

我有两种形式,我想首先创建主窗体,如果用户单击按钮,发送变量并创建第二种形式。

但是在我的代码中,当我开始应用程序时,它尝试创建两个表单,但是应用程序崩溃,因为第二个表单还没有参数。

主要表格电话:

procedure TForm1.listItemClick(const Sender: TObject;
  const AItem: TListViewItem);
var
  item: TListViewItem;
begin
  item:= AItem;
  zipId:= item.Text;
  Form2.Show;
end;

第二种形式:

var
  Form2: TForm2;
  zipId: String;

implementation

{$R *.fmx}

uses main;


procedure TForm2.ImageClick(Sender: TObject);
var
  Image: TImage;
  i: Integer;
  begin
  Image:= Sender as TImage;
  if rects[Image.Tag-1].Fill.Color = TAlphaColorRec.Gray then
    begin
      rects[Image.Tag-1].Fill.Color:= TAlphaColorRec.Blue;
      rects[Image.Tag-1].Tag:= 1;
    end
  else
    begin
      rects[Image.Tag-1].Fill.Color:= TAlphaColorRec.Gray;
      rects[Image.Tag-1].Tag:= 0;
    end;

end;

procedure TForm2.FormCreate(Sender: TObject);
var
  path: String;
  stream: TBytesStream;
  Z: TZipFile;
  SR: TSearchRec;
  Dest: TStringList;
  i,j: Integer;
  getZipId: String;
begin
  url:= 'http://192.168.1.5:3030/api/db/';
  client.BaseURL:= url + 'Control';
  req.Method:= TRESTRequestMethod.rmPOST;
  req.AddParameter('getZip', Form1.zipId);
  req.Execute;
  path:= '/storage/emulated/0/';
  stream := TBytesStream.Create(DecodeBase64(res.Content));
  try    //ANDROID - System.IOUtils.TPath.GetDocumentsPath + System.SysUtils.PathDelim
    stream.SaveToFile(path + 'myfile.zip');
    Z := TZipFile.Create;
    try
        Z.Open(path + 'myfile.zip', zmRead);
        Z.ExtractAll(path + 'Library');

      finally
        Z.Free;
      end;
  finally
    stream.Free;
  end;
  if FindFirst(path + 'Library/'+'*.*', faAnyFile, SR) = 0 then
  repeat
    if ExtractFileExt(SR.Name) = '.jpg' then
      Dest.Add(SR.Name);
  until FindNext(SR) <> 0;
  FindClose(SR);

  SetLength(imgs, Dest.Count);
  SetLength(rects, Dest.Count);

  for i := 0 to Dest.Count-1 do
  begin
    rects[i]:= TRectangle.Create(Form2);
    with rects[i] do begin
      Parent:= flow;
      Width:= 146;
      Height:= 112;
      Margins.Left:= 10;
      Margins.Right:=10;
      Margins.Top:= 5;
      Tag:= 0;
      Fill.Color := TAlphaColorRec.Gray
    end;
    imgs[i]:= TImage.Create(Form2);
    with imgs[i] do begin
    Parent:= rects[i];
    Bitmap.LoadFromFile(path + 'Library/'+Dest[i]);
    Width:= 146;
    Height:= 112;
    Position.X:= 5;
    Position.Y:= 5;
    OnClick:= ImageClick;
    Tag:= i+1;
    end;
  end;
  if RemoveDir(System.IOUtils.TPath.GetDocumentsPath + System.SysUtils.PathDelim + 'Library') then
    ShowMessage('Silindi');
end;

end.

我是delphi的新手,我不明白为什么在应用程序启动时会创建两个表单。

1 个答案:

答案 0 :(得分:0)

在Project中的delphi中 - &gt;查看源代码

你有类似的东西

program YourProgram;

uses
  Forms,
  uTForm1 in 'uTForm1.pas' {TForm1},
  uTForm2 in 'uTForm2.pas' {TForm2};

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm1, Form1);
  Application.CreateForm(TForm2, Form2);
  Application.Run;
end.

删除Application.CreateForm(TForm2, Form2);行 所以在启动时只会创建第一个表单。

然后当您单击按钮打开第二个表单时,创建第二个表单。

procedure TForm1.listItemClick(const Sender: TObject;
  const AItem: TListViewItem);
var
  item: TListViewItem;
  frm : TForm2;
begin
  item:= AItem;
  zipId:= item.Text;
  frm := TFrame2.Create(nil);
  frm .Show;
end;

示例:     //项目源代码     程序Project1;

    uses
      Forms,
      Unit1 in 'Unit1.pas' {Form1},
      Unit2 in 'Unit2.pas' {Form2};

    {$R *.res}

    begin
      Application.Initialize;
      Application.MainFormOnTaskbar := True;
      Application.CreateForm(TForm1, Form1); // creates only first form on start up
      Application.Run;
    end.


unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses Unit2; 

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  frm : TForm2; // make an TForm2 object
begin
   frm := TForm2.Create(nil); // create the object 
   frm.Show; // show the object ( which in our case is a form )
end;

end.