Professional Documents
Culture Documents
Introduction to OO basics
Problems
23 24 25 26 27 28 29 30 31 32
procedure TfrmMain.radAuxShowClick(Sender: TObject); begin if frmAuxiliary = nil then begin ShowMessage ('Auxiliary Form does not exist'); radAuxShow.Checked := False; end else frmAuxiliary.Show; end; // end procedure TfrmMain.radAuxShowClick
Chapter 1, Page 1
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
procedure TfrmMain.radAuxHideClick(Sender: TObject); begin if frmAuxiliary = nil then begin ShowMessage ('Auxiliary Form does not exist'); radAuxHide.Checked := False; end else frmAuxiliary.Hide; end; // end procedure TfrmMain.radAuxHideClick procedure TfrmMain.btnCreateClick(Sender: TObject); begin if frmAuxiliary = nil then frmAuxiliary := TfrmAuxiliary.Create(Self) else ShowMessage ('Auxiliary Form already exists'); end; // end procedure TfrmMain.btnCreateClick procedure TfrmMain.btnFreeClick(Sender: TObject); begin if frmAuxiliary = nil then ShowMessage ('Auxiliary Form does not exist') else begin frmAuxiliary.Hide; frmAuxiliary.Free; frmAuxiliary := nil; end; end; // end procedure TfrmMain.btnFreeClick
Chapter 1, Page 2
procedure TfrmMain.rgpAuxFormClick(Sender: TObject); begin try case rgpAuxForm.ItemIndex of 0: if frmAuxiliary = nil then begin frmAuxiliary := TfrmAuxiliary.Create(Self); ShowMessage ('Auxiliary Form created'); // optional extra end else ShowMessage ('Auxiliary Form already exists'); 1: frmAuxiliary.Show; 2: frmAuxiliary.Hide; 3: begin frmAuxiliary.Hide; frmAuxiliary.Free; frmAuxiliary := nil; ShowMessage ('Auxiliary Form freed'); // optional extra end; else ShowMessage ('Unrecognised operation'); end; except ShowMessage ('Auxiliary Form does not exist'); end; end; // end procedure TfrmMain.rgpAuxFormClick procedure TfrmMain.frmAuxiliaryShow(Sender: TObject); begin frmAuxiliary.Left := Random (600); frmAuxiliary.Top := Random (400); end; // end procedure TfrmAuxiliary.FormShow procedure TfrmMain.btnBlueClick(Sender: TObject); begin frmAuxiliary.Color := clSkyBlue; end; // end procedure TfrmMain.btnBlueClick procedure TfrmMain.btnGreenClick(Sender: TObject); begin ((Sender as TButton).Parent as TForm).Color := clMoneyGreen; end; // end procedure TfrmMain.btnGreenClick initialization Randomize; end. // end unit MainForm
Chapter 1, Page 3
The start up behaviour changes. If our first action is to click on the Create button there is an exception stating that the Form already exists. We can still go on to Show, Hide, Free and Create the auxiliary form as before.
Well start from problem 1.3, which has two units, one for each of the RAD forms. Since well now create frmAuxiliary directly in program code we no longer need the second unit. By removing the second unit, we also remove the declarations for frmAuxiliary and its two buttons. So we need to declare these and the event handlers as part of TfrmMain in unit MainForm:
type TfrmMain = class(TForm) rgpAuxForm: TRadioGroup; procedure rgpAuxFormClick(Sender: TObject); private // now declared by the programmer frmAuxiliary: TForm; // data fields btnBlue, btnGreen: TButton; procedure frmAuxiliaryShow(Sender: TObject); // event handlers procedure btnBlueClick(Sender: TObject); procedure btnGreenClick(Sender: TObject); end; // end TfrmMain = class(TForm)
Chapter 1, Page 4
1: 2: 3:
{ Announce successful Free} ShowMessage ('Auxiliary Form freed'); end; else ShowMessage ('Unrecognised operation');
Chapter 1, Page 5
end; except ShowMessage ('Auxiliary Form does not exist'); end; end; // end procedure TfrmMain.rgpAuxFormClick procedure TfrmMain.frmAuxiliaryShow(Sender: TObject); begin frmAuxiliary.Left := Random (600); frmAuxiliary.Top := Random (400); end; // end procedure TfrmAuxiliary.FormShow procedure TfrmMain.btnBlueClick(Sender: TObject); begin frmAuxiliary.Color := clSkyBlue; end; // end procedure TfrmMain.btnBlueClick procedure TfrmMain.btnGreenClick(Sender: TObject); begin ((Sender as TButton).Parent as TForm).Color := clMoneyGreen; end; // end procedure TfrmMain.btnGreenClick initialization Randomize; end. // end unit MainForm
Notice that we do not assign a parent for frmAuxiliary, and so by default the Application becomes the parent. If we were to assign frmMain as the parent, frm Auxiliary would appear on frmMain and not as a separate form. 2. Draw the matching UML diagrams, showing composition explicitly. Class diagram: One needs to think a bit about this. If we refer to the code, we notice that we define only one class, TfrmMain, and then create several objects (frmAuxiliary, btnBlue and btnGreen) as part of this class. So, we have one class that carries references to several objects of various classes.
Chapter 1, Page 6
Object diagram: As part of the RAD process, frmMain automatically creates rgpAuxForm. As part of rgpAuxFormClick it creates frmAuxiliary, btnBlue and btnGreen.
Sequence diagram: There are a number of possible user actions, of which we show two in the following diagram:
Chapter 1, Page 7