Multiple forms in delphi
In my Delphi Project i want to have a 'Settings' button that when clicked, opens a second form (i think this is the correct term, i essentially want a new window to open) for settings.
When the user has finished changing the settings on this new form, i want the form to c开发者_C百科lose on a button click.
The settings the user types in will also need to be accessible to the first, 'main' form.
So, for example, if my programme consisted of a main form, that calculated 'A' + 'B' (A and B being integer variables), with the settings form allowing the user to set values for A and B, how would i do this?
That's not too difficult if you understand Delphi's object model. A form is an object that descends from TObject, backed by a DFM file to make setting up the layout easier. The controls on it are other objects, and by default they're publicly visible to other objects from other units, like your other form. There are two ways to do this.
The easy way is to have your other form's code read the values from the controls directly once you're done with the first form. Just use stuff like MyString := Form2.EditBox.Text;
. This isn't particularly good style, but it works.
The right way to do it is to either put public properties on your form that will retrieve the values of the controls, or a function that will read them and return some sort of object or record containing all the settings. This takes a bit more work, but it results in cleaner code that's less likely to cause trouble if you modify things down the road.
EDIT: In response to the question in the comment:
To make one form show and hide another, you call Show and Hide on it. Or if you want it to show up in a modal dialog box, call the ShowModal method, which takes care of the hiding for you, as long as you create a button that sets ModalResult. (See the helpfile for details on how these methods work.)
Of course, the form has to have been created first. Either it can be autocreated by the DPR, which is good for simple programs but not so great once you get a lot of forms in your app, or you can create it in code. Henk has an example of how to do that, though I wouldn't recommend using the with keyword. And if you created the form yourself, make sure to Free it afterwards.
I usually design the settings form and add a class function execute to it, that changes a record containing fields for the settings like this:
Tf_MySettings = class(TForm)
// ...
private
procedure SetData(const _Settings: TSettingsRec);
procedure GetData(out _Settings: TSettingsRec);
public
class function Execute(_Owner: TComponent; var _Settings: TSettingsRec): boolean;
end;
implementation
function Tf_MySettings.Execute(_Owner: TComponent; var _Settings: TSettingsRec): boolean;
var
frm: Tf_MySettings;
begin
frm := Tf_MySettings.Create(_Owner);
try
frm.SetData(_Settings);
// for this to work, the OK button must have ModalResult=mrOK
Result := frm.ShowModal = mrOK;
if Result then
frm.GetData(_Settings);
finally
frm.Free;
end;
end;
procedure Tf_MySettings.SetData(const _Settings: TMySettingsRec);
begin
ed_Name.Text := _Settings.Name;
// ...
end;
procedure Tf_MySettings.GetData(out _Settings: TMySettingsRec);
begin
_Settings.Name := ed_Name.Text;
// ...
end;
You call it like this:
if Tf_MySettings.Execute(self, _Settings) then begin
// settings have been changed
end;
I would create a Settings Objects in form1 and send it to form 2:
TSettings = class
private
FPropA: integer;
FPropB: string;
published
property PropA: integer read FPropA write FPropA:
property PropB: string read FPropB write FPropB:
end;
...
form2.EditSettings(ASettings);
精彩评论