首页 > 解决方案 > Cross-form variable with Multiple forms

问题描述

I'm a Highschool student working with Delphi 2010 and currently working on a projects. I'm having some trouble using a variable assigned a value on one form, on another whilst using "ShowModal" to view the second.

Here's what I have on the First Form (only 1 Click procedure is shown as it repeats):

  unit frmSkill_u;

    interface

    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ExtCtrls, Buttons, jpeg, dmMusiektukke_u, DBGrids, PAT, frmMusiek_u  ;

    type
     TfrmSkill = class(TForm)
    Panel1: TPanel;
    pnlBegin: TPanel;
    bitbtnMain: TBitBtn;
    pnlEasy: TPanel;
    pnlInter: TPanel;
    pnlAdv: TPanel;
    Image1: TImage;
    Label1: TLabel;
    procedure bitbtnMainClick(Sender: TObject);
    procedure pnlBeginClick(Sender: TObject);
    procedure pnlEasyClick(Sender: TObject);
    procedure pnlInterClick(Sender: TObject);
    procedure pnlAdvClick(Sender: TObject);
    private

    { Private declarations }
  public
  iLevel : integer ;
    { Public declarations }
  end;

var

  frmSkill: TfrmSkill;


implementation



{$R *.dfm}


procedure TfrmSkill.bitbtnMainClick(Sender: TObject);
begin
frmInstru.Visible := True ;
end;

procedure TfrmSkill.pnlBeginClick(Sender: TObject);
begin

   iLevel := 0 ;
 frmMusic.ShowModal ;

end;

A button will be clicked to assign which level the person can play an instrument on (Beginner level = 0) and then the second form will show using the iLevel variable in a Case statement to filter a Database according to which level the person selected.

The second form :

 unit frmMusiek_u;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, dmMusiektukke_u, Grids, DBGrids, StdCtrls, Buttons, frmSkill_u;

type
  TfrmMusic = class(TForm)
    dbgMusiekstukke: TDBGrid;
    BitBtn1: TBitBtn;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public

    { Public declarations }
  end;

var

  frmMusic: TfrmMusic;

implementation



{$R *.dfm}

procedure TfrmMusic.FormCreate(Sender: TObject);
begin
  case frmSkill.iLevel of
    0:
      begin
        with dmMusiekstukke do
        begin
           tblMusiekstukke.Filter := 'Difficulty = ''Beginner''' ;
          tblMusiekstukke.Filtered := True;
         end;
      end;
  end;

end;
end.

However, the post's method I've seen on using variable across different forms (Passing the variable to another Form) cannot work here until I figure out how to get rid of the [DCC Fatal Error] frmSkill_u.pas(7): F2047 Circular unit reference to 'frmSkill_u' error when adding "frmSkill_u" to the Uses of the second form.

How can I still use such a variable and show the second form contemporarily?

Thank you very much!

标签: delphidelphi-2010

解决方案


我假设你和我一样是南非的高中生。(使用 Delphi 和南非荷兰语。)您使用Form.Create而不是Form.Showon frm.Musiek_u;。假设您的表单 ( frmMusiek_u;) 已经创建。iLevel 不会分配值,并且会引发错误。

我可以看到的第二件事是您已将所有单位手动添加到您的使用字段中。这是跟踪正在使用的单位的好习惯,但是对于我假设您正在执行的 PAT 的情况,我建议您前往

文件 -> 使用单位...

并从那里选择你的单位。

如果您的 PAT 仍有问题,请随时给我发电子邮件寻求帮助 Marclevin.sa@gmail.com


推荐阅读