Search code examples
delphidelphi-2010

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!


Solution

  • You used Form.Create instead of Form.Show on frm.Musiek_u;. Assuming that your form (frmMusiek_u;) has already been created. iLevel won't have a value assigned and will throw an error.

    The second thing I can see is that you have manually added all your units to your 'uses' fields, instead I would recommend going to:

    File -> Use Unit...

    and selecting your units from there.