How to retrieve data from Modal forms

I have for example a unit A that call a modal form in Unit B. In Unit B, items are selected using Multiselect in the DBGrid shown in unit B. What I need is to process the items selected in Unit A after the form in unit B is closed. How to go about it?

SA/\/\
"If you have always done it that way, it is probably wrong."
LVL 1
samchanAsked:
Who is Participating?
 
kambizConnect With a Mentor Commented:
You probably close DataSet on Hide event of your form.
0
 
millerwCommented:
I normally have my modal forms created when used.  That is why the following TfrmSaveSearch is being created then freed.  The "txtName.Text" is being read of the TfrmSaveSearch.  Just access them by direct calls.  JUST DON'T SET THE FORMCLOSE EVENT TO CAFREE OR THIS WILL TOTALLY MESS YOU UP--ACCESS VIOLATIONS GALORE.  Exit out of the form by setting ModalResult to something like mrOK or mrCancel (the regular buttons do this automatically but Speed buttons don't).  Free up the form after you read everything you need--as you can see below.

With TfrmSaveSearch.Create(Self) do
try
  If ShowModal = mrOK then
  begin
    FSearch.Save(txtName.Text, ssUser);
  end;
finally
  Free;
end;

Scott
0
 
samchanAuthor Commented:
I did try that. However, those SelectedRows of ItemDBGrid somehow do not have any values after the ShowModal line! But those SelectedRows are still available if I check it in the Modal Unit.

The codes I am trying is as follows:

begin
 with TfrmItemSelection.Create(Self) do
  try
    if ShowModal = mrOk then
    begin
      if ItemDBGrid.SelectedRows.Count>0 then
      begin
        with ItemDBGrid.DataSource.DataSet do
        begin
          for i:=0 to ItemDBGrid.SelectedRows.Count-1 do
          begin
            GotoBookmark(pointer(ItemDBGrid.SelectedRows.Items[i]));
            for j := 0 to FieldCount-1 do
            begin
              if (j>0) then s:=s+', ';
              s:=s+Fields[j].AsString;
            end;
            Listbox1.Items.Add(s);
              s:= '';
          end;
        end;
      end;
    end;
  finally
    Free;
  end;
end;
0
2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

 
samchanAuthor Commented:
Yup. You're right. I closed the Dataset.
Thank you.
Hmm... now who should I give the point too.
0
 
millerwCommented:
Go for it kambiz
0
 
samchanAuthor Commented:
Okay. Then I will give the point to kambiz. However, thanks to you millerw because your example encouraged me to try the solution which i've tried but thought was not working.
0
 
millerwCommented:
No problem.

Good luck and happy coding!
0
 
kambizCommented:
Thank you Samchan and thank you Millerw.
0
All Courses

From novice to tech pro — start learning today.