Link to home
Start Free TrialLog in
Avatar of bilgehanyildirim
bilgehanyildirim

asked on

Passing a parameter to new opened form.

Hi folks,

Here is my proplem. There is a main form (frmMain). There is a text box (edtOrderNumber). When you enter whole order number (99000123) or just a part of it (123) it searchs the mysql database and returns the value. if it finds only one result it fills all fields like edtBillingName, edtBillingSurname etc. The thing I want to do is if it finds more than one result it will open a new form (frmFoundOrders) with a ListView on the form (lvFoundOrders) and show all found results on this new form with OrderNumber and CustomerSurname values. When user clicks on one of these results this form (frmFoundOrders) closes and the ordernumber value passes to main form (frmMain) and fills the fields (edtBillingName, edtBillingSurname etc. ).

I could manage to do if the result just one.

As you can understand I'm trying a develop the admin page of our ecommerce site on Delphi 7. Order numbers are uniqe.

Thanks in advance for your help.
Avatar of shaneholmes
shaneholmes

So then what you  want to do is Create a New form (formStyle = fsDialog) with a TListView  component - with ViewSTyle property set to vsReport.

Right click on it and set up your columns.

Then add the code to fill in the ListView with your QUery returned set.

var
 Item: TListItem;

 MyQuery.First
 While not MyQuery.EOF do
 begin
  Item:= LIstView.Items.Add;
  Item.Caption:=  MyQuery.FieldByName('MyField1').asString;
  Item.SubItems.Add(MyQuery.FieldByName('MyField2').asString);
  //note add additional subitems for each additional field MyField3.....and so on
 end;

call the form like

 if MyNewFOrm.SHowModal = mrOK then
 begin
  FIllOrder(MyNewForm.ListView1.Selected);
 end;

You can create a button with the modal result = mrOK or create code so when you double click a ListView Item, you send the ModalResult = mrOK

procedure FIllOrder(Item: TListItem)
begin
 MyEdit1.Text:= Item.Caption;
 MyEdit2.Text:= Item.SubItems[0];
 MyEdit3.Text:= Item.SubItems[1];
 .... and so on.....
end;


Shane
Oops!

MyQuery.First
 While not MyQuery.EOF do
 begin
  Item:= LIstView.Items.Add;
  Item.Caption:=  MyQuery.FieldByName('MyField1').asString;
  Item.SubItems.Add(MyQuery.FieldByName('MyField2').asString);
  //note add additional subitems for each additional field MyField3.....and so on

  MyQuery.Next;  ///************

 end;


Shane
Hi

// create frmFoundOrders here or at application start
frmFoundOrders.      // set whatever You want
if frmFoundOrders.ShowModal = mrOK then
  begin
  frmFoundOrders.lvFoundOrders.Selected... // get whatever You want
  end;
// Free frmFoundOrders here or at application ends
Another oops!

if MyNewFOrm.SHowModal = mrOK then
 begin
  MainForm.FillOrder(MyNewForm.ListView1.Selected);
 end;

procedure MainFOrm.FillOrder(Item: TListItem)
begin
  MyEdit1.Text:= Item.Caption;
  MyEdit2.Text:= Item.SubItems[0];
  MyEdit3.Text:= Item.SubItems[1];
  .... and so on.....
end;

Shane


If You've got Query use rather DBGrid on Your frmFoundOrders
Avatar of bilgehanyildirim

ASKER

Thanks all of you! I was quicker than I expected :)

Shane, could you please show me how can I both close the new form and send the value to mainform?

You said
"You can create a button with the modal result = mrOK or create code so when you double click a ListView Item, you send the ModalResult = mrOK"
I'm using different component than the one which comes with D7 and to be hones I hate DBGrid component :)

You may hate DBGrid but in such a case use other DB aware controls
A Button has a ModalResult property which you can set to mrOK, so if you hvae a form style of fsDialog, cllicking this button will pass the ModalResult back to form


or you can do it by code

procedure TForm1.Button1Click(Sender: TObject);
begin
 ModalResult:= mrOK ;
 Close;
end;


so then your main form tests it

 if MyNewFOrm.SHowModal = mrOK then
 begin
  FillOrder(MyNewForm.ListView1.Selected);
 end;

Shane




It is convenient to have data module. You don't need then to send results back. Results selected in Your frmFoundOrders form will be readable in main form.
can you give any example for usage of datamodule?
ASKER CERTIFIED SOLUTION
Avatar of mokule
mokule
Flag of Poland image

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 kretzschmar
just to show another way

----- the selectform (generic) ----------------

unit frmSelect_u;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, db;

type
  TForm2 = class(TForm)
    ListView1: TListView;
    Button1: TButton;  //modalresult set to mrOK
    Button2: TButton;  //modalresult set to mrCancel
  private
    function BuildColumns(ADataSet : TDataSet; ResultFieldName : String) : Integer;
    procedure FillContent(ADataSet : TDataSet);
  public
    class function Select(AOwner : TComponent; ADataSet : TDataSet; ResultFieldName : String) : Integer;
  end;


var
  Form2: TForm2;

implementation

{$R *.dfm}

function TForm2.BuildColumns(ADataSet : TDataSet; ResultFieldName : String) : Integer;
var
  i : integer;
  t : TListColumn;
begin
  result := -1;
  if assigned(ADataset) and (AdataSet.Active) then
  begin
    for i := 0 to ADataSet.FieldCount-1 do
    begin
      t := listview1.Columns.Add;
      t.Caption := ADataSet.Fields[i].FieldName;
      if UpperCase(ADataSet.Fields[i].FieldName) = UpperCase(ResultFieldName) then
        result := i;
    end;
  end
  else raise Exception.Create('Invalid Dataset or Dataset closed');
end;


procedure TForm2.FillContent(ADataSet : TDataSet);
var
  l : TListItem;
  i : integer;
begin
  if assigned(ADataset) and (AdataSet.Active) then
  begin
    ADataSet.First;
    while not ADataSet.eof do
    begin
      l := listview1.Items.Add;
      l.Caption := ADataSet.Fields[0].AsString;
      for i := 1 to ADataSet.FieldCount-1 do
        l.SubItems.Add(ADataSet.Fields[i].AsString);
      ADataset.Next;
    end;
  end
  else raise Exception.Create('Invalid Dataset or Dataset closed');
end;


class function TForm2.Select(AOwner : TComponent; ADataSet : TDataSet; ResultFieldName : String) : Integer;
var
  f : TForm2;
  t : TListColumn;
  i : integer;
  resultFieldIndex : Integer;
begin
  result := -1;
  if (assigned(ADataSet)) and (ADataSet.Active) then
  begin
    f := Create(AOwner);
    try
      resultFieldIndex := f.BuildColumns(ADataset,ResultFieldName);
      f.FillContent(ADataSet);
      if f.ShowModal = mrOK then
      begin
        if (resultFieldIndex = -1) or (not(assigned(f.ListView1.Selected))) then
          //nothing
        else
          if resultFieldIndex = 0 then
            result := strtoint(f.ListView1.Selected.Caption)
          else
            result := strtoint(f.ListView1.Selected.SubItems[resultFieldIndex-1]);
      end;
    finally
      f.Release;
    end;
  end
  else raise Exception.Create('Invalid Dataset or Dataset closed');
end;

end.

------------------------ usage sample ----------

procedure TForm1.Button1Click(Sender: TObject);
begin
  adoquery1.open;
  //form2 must not created
  showmessage(inttostr(TForm2.Select(self,adoquery1,'AID')));
end;


meikl ;-)