Solved

Passing data from main form to dialog box?

Posted on 2002-06-18
6
281 Views
Last Modified: 2010-08-05
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??
0
Comment
Question by:TopherAPC
6 Comments
 
LVL 7

Expert Comment

by:Cynna
ID: 7091245
TopherAPC,

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.Strings[0];
   // ..... 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: TObject);
var Results: TStringList;
begin
  try
    Results:=TStringList.Create;
    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.Strings[0];
    // ...etc...
  finally
    Results.Free;
  end;
end;


0
 
LVL 11

Accepted Solution

by:
robert_marquardt earned 50 total points
ID: 7091841
I prefer it this way:

  with TMyDialog.Create do
    try
      // fill the controls of the dialog
      if ShowModal = mrOK then
      begin
        // pull the values from the controls
      end;
    finally
      Free;
    end;

If you use the dialog often then overwrite the ShowModal and add some parameters for the fill/pull actions. Fill and pull then reside in the TMyDialog unit.
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 7092606
use the roberts way - also how I have it :)
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 1

Expert Comment

by:bogiboy
ID: 7093639
How I do it is:

In MainForm:

...
if (not assigned(myDialog1)) then AppliCation.CreateForm(TMyDialog,MyDialog1);
MyDialog.CheckBoxList1.Clear;
//fill in checklistbox items from here
for i:=0 to mydataSet.recordcount-1 do
  begin
   MyDialog.CheckBoxList1.Items.Add(myDataSet.Fields    [1].AsString);
   myDataset.next;
  end;
  if MyDialog1.ShowModal=mrOK then
   begin
    for i:=0 to MyDialog1.CheckListBox1.Items.Count-1 do
      begin
     strListItem:=MyDialog1.CheckListBox1.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.
     



0
 
LVL 1

Expert Comment

by:bogiboy
ID: 7097379
Sorry Robert, I repeated your answer...what I am doing is actually what you are proposing...
0
 

Expert Comment

by:SpideyMod
ID: 8280704
Force Accepted

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now