Solved

passing values to another form in SDI Or to make a form set values differently depending on which button launched it.

Posted on 2008-10-07
6
197 Views
Last Modified: 2012-05-05
Hi Experts,
I have lets say a 'MainForm" which has among a lot of other things two buttons, lets say 'buttonorig' and 'buttonnew'. Clicking 'buttonorig' launches a form lets say 'FormSEC'. 'FormSEC' has many components which are set in its "OnCreate" procedure. I want to set some of the components in 'FormSEC' to different values if 'buttonnew' is clicked. Can I pass there values to the 'FormSEC'? if not can I set the components differently in the "OnCreate" procedure depending on which button was clicked?
0
Comment
Question by:QurbanDurrani
  • 3
  • 2
6 Comments
 
LVL 14

Accepted Solution

by:
SteveBay earned 500 total points
ID: 22663593
I would suggest setting a pubilc variable in MainForm based upon which button was clicked:

procedure TForm1.ButtonClick(Sender: TObject);
begin
     if Sender = ButtonOrig then
          WhichButton := 0
     else if Sender = ButtonNew then
          WhichButton := 1;

end;

then in the OnFormCreate in FormSec do this:

procedure TFormSec.FormCreate(Sender: TObject);
begin
     if MainForm.WhichButton = 0 then
          // do this stuff
     else if MainForm.WhichButton = 1 then
          // do that stuff
end;
0
 

Author Comment

by:QurbanDurrani
ID: 22664037
Declaration of variable 'whichbutton" as shown in the attachment throws an error saying "'END' expected but 'VAR' found'. Should I be declaring it somewhere else?

<other Procedure declaration above. 

procedure IPCollectionSheetShow(Sender: TObject);

    procedure IPCollectionSheetHide(Sender: TObject);

    procedure ViewGroupClick(Sender: TObject);

    procedure ViewCOTClick(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

    var

      whichbutton : integer;

  end;

Open in new window

0
 

Author Comment

by:QurbanDurrani
ID: 22664090
Ok I figured out where to declare the variable. Will post again after adding the rest.
Thanks
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:QurbanDurrani
ID: 22664564
Thanks SteveBay for all you help.
That works.
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 22666620
my 2 cents:
you will have circular references and many subforms dependent on your mainform
you can only use the SecForm in combination with your mainform
if you want to use your secform from any form
do it this way
<see code snippet>

and use it from the mainform (or any form) like

procedure TMainForm.ButtonClick(Sender: TObject);
begin
  if ShowSecForm(Self, IfThen(Sender = ButtonOrig, 0, 1)) then
    ShowMessage('Result of form = mrOk');
end;

Why ?
1: No circular reference
2: If you work with 2 monitors and set Position of the form to poOwnerFormCenter
the SecForm will stay in the same monitory as the Form calling it



interface
 

type

  TSecForm = class(TForm)

  ...

  private

    fVar1: integer;

  public  

  end;
 

var SecForm: TSecForm;
 

function ShowSecForm(AOwner: TComponent; AButton: Integer);

var frm: TSecForm;

begin

  Result := False;

  frm := TSecForm.Create(AOwner);

  try

    frm.fVar1 := aButton;

    if frm.ShowModal = mrOk then 

      Result := True;    

  finally

    FreeAndNil(frm);

  end;

end;

Open in new window

0
 
LVL 14

Expert Comment

by:SteveBay
ID: 22668868
Geert,
Your solution allows to SecForm to know WhichButton during the OnShow event. QurbanDurrani asked how to know in the OnCreate Event.

I had originally started answering this question with a solution similar to yours but I changed it because QurbanDurrani was looking for a way that made sense with the way the FormSEC was currently being implemented. I can think of no way to assign values in an TForm prior to its OnCreateEvent.

I can imagine though, that we could have a lively discussion about how to create dynamic forms - OnCreate vs. OhShow events. Personally I agree with you - I prefer do to the heavy lifting in the OnShow using form-local variables, not just to avoid circular reference but because it a clearer way to code.

sb
 
 
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 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

13 Experts available now in Live!

Get 1:1 Help Now