TopherAPC
asked on
Passing data from main form to dialog box?
What is the best way to pass data from the mainform to a dialog box and then send data from the dialog back to the main form.
I want a CheckBoxlist on my dialog which gets populated by data which is accessible in the mainform. Then when the user makes his/her selection and clicks ok, the list of selected items needs to be sent back to the mainform to be processed.
???
I tried adding a parameter in the dialog form's create method, hoping I could pass in a TStringsList, but that didn't seem to work. So how Can I do this??
I want a CheckBoxlist on my dialog which gets populated by data which is accessible in the mainform. Then when the user makes his/her selection and clicks ok, the list of selected items needs to be sent back to the mainform to be processed.
???
I tried adding a parameter in the dialog form's create method, hoping I could pass in a TStringsList, but that didn't seem to work. So how Can I do this??
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
use the roberts way - also how I have it :)
How I do it is:
In MainForm:
...
if (not assigned(myDialog1)) then AppliCation.CreateForm(TMy Dialog,MyD ialog1);
MyDialog.CheckBoxList1.Cle ar;
//fill in checklistbox items from here
for i:=0 to mydataSet.recordcount-1 do
begin
MyDialog.CheckBoxList1.Ite ms.Add(myD ataSet.Fie lds [1].AsString);
myDataset.next;
end;
if MyDialog1.ShowModal=mrOK then
begin
for i:=0 to MyDialog1.CheckListBox1.It ems.Count- 1 do
begin
strListItem:=MyDialog1.Che ckListBox1 .Items[i];
//do something with selected value
end;
sometimes, I create array of String dynamic array for this case , so I can destroy dialog when I am finished with reading values from the checklistbox.
In MainForm:
...
if (not assigned(myDialog1)) then AppliCation.CreateForm(TMy
MyDialog.CheckBoxList1.Cle
//fill in checklistbox items from here
for i:=0 to mydataSet.recordcount-1 do
begin
MyDialog.CheckBoxList1.Ite
myDataset.next;
end;
if MyDialog1.ShowModal=mrOK then
begin
for i:=0 to MyDialog1.CheckListBox1.It
begin
strListItem:=MyDialog1.Che
//do something with selected value
end;
sometimes, I create array of String dynamic array for this case , so I can destroy dialog when I am finished with reading values from the checklistbox.
Sorry Robert, I repeated your answer...what I am doing is actually what you are proposing...
Force Accepted
SpideyMod
Community Support Moderator @Experts Exchange
SpideyMod
Community Support Moderator @Experts Exchange
I presume you're using modal form as a dialog.
Well, if you TStringsList, one solution could be
like this:
(say Form1 is your main form, and Form2 is your dialog form)
In your dialog form (Form2)add method GetResults():
type
TForm2 = class(TForm)
Button1: TButton;
private
{ Private declarations }
public
{ Public declarations }
procedure GetResults(var Result: TStringList);
end;
(.....)
procedure TForm2.GetResults(var Result: TStringList);
begin
// Do something with Result, passed from main form:
Button1.Caption:=Result.St
// ..... use data ....
ShowModal;
// Store whatever you want in Result:
Result.Clear;
Result.Add('some data');
Result.Add('some more data');
//.... etc....
end;
In your main form(Form1), you then call your dialog like this:
procedure TForm1.Button1Click(Sender
var Results: TStringList;
begin
try
Results:=TStringList.Creat
Results.Add('Press me!');
// This is the way to show your dialog form and send it data:
Form2.GetResults(Results);
// Results now has data from dialog form:
Button1.Caption:=Results.S
// ...etc...
finally
Results.Free;
end;
end;