Link to home
Start Free TrialLog in
Avatar of samchan
samchan

asked on

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."
Avatar of millerw
millerw

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
Avatar of samchan

ASKER

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;
ASKER CERTIFIED SOLUTION
Avatar of kambiz
kambiz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of samchan

ASKER

Yup. You're right. I closed the Dataset.
Thank you.
Hmm... now who should I give the point too.
Go for it kambiz
Avatar of samchan

ASKER

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.
No problem.

Good luck and happy coding!
Thank you Samchan and thank you Millerw.