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.
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.
Oops!
MyQuery.First
While not MyQuery.EOF do
begin
Item:= LIstView.Items.Add;
Item.Caption:= MyQuery.FieldByName('MyFie ld1').asSt ring;
Item.SubItems.Add(MyQuery. FieldByNam e('MyField 2').asStri ng);
//note add additional subitems for each additional field MyField3.....and so on
MyQuery.Next; ///************
end;
Shane
MyQuery.First
While not MyQuery.EOF do
begin
Item:= LIstView.Items.Add;
Item.Caption:= MyQuery.FieldByName('MyFie
Item.SubItems.Add(MyQuery.
//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.lvFoundOrde rs.Selecte d... // get whatever You want
end;
// Free frmFoundOrders here or at application ends
// create frmFoundOrders here or at application start
frmFoundOrders. // set whatever You want
if frmFoundOrders.ShowModal = mrOK then
begin
frmFoundOrders.lvFoundOrde
end;
// Free frmFoundOrders here or at application ends
Another oops!
if MyNewFOrm.SHowModal = mrOK then
begin
MainForm.FillOrder(MyNewFo rm.ListVie w1.Selecte d);
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 MyNewFOrm.SHowModal = mrOK then
begin
MainForm.FillOrder(MyNewFo
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
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"
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"
ASKER
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.ListVi ew1.Select ed);
end;
Shane
or you can do it by code
procedure TForm1.Button1Click(Sender
begin
ModalResult:= mrOK ;
Close;
end;
so then your main form tests it
if MyNewFOrm.SHowModal = mrOK then
begin
FillOrder(MyNewForm.ListVi
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.
ASKER
can you give any example for usage of datamodule?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(ADataS et : 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].FieldNa me;
if UpperCase(ADataSet.Fields[ i].FieldNa me) = UpperCase(ResultFieldName) then
result := i;
end;
end
else raise Exception.Create('Invalid Dataset or Dataset closed');
end;
procedure TForm2.FillContent(ADataSe t : 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].AsStrin g;
for i := 1 to ADataSet.FieldCount-1 do
l.SubItems.Add(ADataSet.Fi elds[i].As String);
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,Re sultFieldN ame);
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.Selec ted.Captio n)
else
result := strtoint(f.ListView1.Selec ted.SubIte ms[resultF ieldIndex- 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(TForm 2.Select(s elf,adoque ry1,'AID') ));
end;
meikl ;-)
----- 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(ADataS
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].FieldNa
if UpperCase(ADataSet.Fields[
result := i;
end;
end
else raise Exception.Create('Invalid Dataset or Dataset closed');
end;
procedure TForm2.FillContent(ADataSe
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].AsStrin
for i := 1 to ADataSet.FieldCount-1 do
l.SubItems.Add(ADataSet.Fi
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,Re
f.FillContent(ADataSet);
if f.ShowModal = mrOK then
begin
if (resultFieldIndex = -1) or (not(assigned(f.ListView1.
//nothing
else
if resultFieldIndex = 0 then
result := strtoint(f.ListView1.Selec
else
result := strtoint(f.ListView1.Selec
end;
finally
f.Release;
end;
end
else raise Exception.Create('Invalid Dataset or Dataset closed');
end;
end.
------------------------ usage sample ----------
procedure TForm1.Button1Click(Sender
begin
adoquery1.open;
//form2 must not created
showmessage(inttostr(TForm
end;
meikl ;-)
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('MyFie
Item.SubItems.Add(MyQuery.
//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.ListVi
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