Quintin96
Quintin96

Reputation: 25

I want to change the caption of the label on one form via clicking a button on another form Delphi 7

I have to work on a prediction game for school. In order to make 2 random teams be playing against each other I have done the following code on form 1 :

procedure TfrmUserInput.FormCreate(Sender: TObject);
const
arrT1 : array[1..6] of string = ('Blue Bulls','Griquas','EP Kings','Sharks','Cheetahs','Valke');
arrT2 : array[1..6] of string = ('Lions','Pumas','Leopards','Western Province','Kavaliers','Eagles');
begin
Randomize;
sTeam1 := arrT1[Random(5)+1];
Randomize;
sTeam2 := arrT2[Random(5)+1];
lblT1Pred.Caption := (sTeam1 + ' predicted score :');
lblT2Pred.Caption := (sTeam2 + ' predicted score :');
rbTeam1.Caption := sTeam1;
rbTeam2.Caption := sTeam2;
end;

And on the second form I have the following :

procedure TfrmAdminInput.FormCreate(Sender: TObject);
begin
rbT1.Caption := sTeam1;
rbT2.Caption := sTeam2;
end;

sTeam1 and sTeam2 are Global variables.

Now on the 4th form, I click a button to start the prediction of the next game - therefore I need to select 2 other random teams, at first I wanted to just create duplicate arrays and use the following code, but it gives me a problem of 'Undeclared identifier : lblT1Pred' - this problem is the same for lblT2Pred and the labels on the second form (rbT1.Caption and rbT2.Caption) aswell as the radio button captions on form 1. The code is as follows:

sTeam1 := arrT1[Random(5)+1];
sTeam2 := arrT2[Random(5)+1];
frmUserInput.lblT1Pred.Caption := (sTeam1 + ' predicted score :');
frmUserInput.lblT2Pred.caption := (sTeam2 + ' predicted score :');
frmUserInput.rbTeam1.Caption := sTeam1;
frmUserInput.rbTeam2.Caption := sTeam2;
frmAdminInput.rbT1.Caption := sTeam1;
frmAdminInput.rbT2.Caption := sTeam2;

Form 1 is frmUserInput, Form 2 is frmAdminInput and Form 4 is frmWinners.

So just to revise what I want to do is change the captions of labels and radio buttons on form 1 and form 2 via the click of a button on form 4 (this button will also hide form 4 and show form 1).

Upvotes: 0

Views: 2899

Answers (1)

David Heffernan
David Heffernan

Reputation: 612964

If you have a global variable named Form1 defined in Unit1, and that form has a label named Label1, then you access it from another unit like so:

  • Add Unit1 to the uses clause of the other unit.
  • Refer to the label as Form1.Label1.

To avoid circular references you may need to add Unit1 to the uses clause in the implementation section of your other unit.

That said, I would prefer it for the form to offer a public method to do the work rather than letting all and sundry play with its private parts.

Upvotes: 5

Related Questions