Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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
Medium Priority
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?
Question by:QurbanDurrani
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 14

Accepted Solution

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

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


then in the OnFormCreate in FormSec do this:

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

Author Comment

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 declarations }
    { Public declarations }
      whichbutton : integer;

Open in new window


Author Comment

ID: 22664090
Ok I figured out where to declare the variable. Will post again after adding the rest.
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Author Comment

ID: 22664564
Thanks SteveBay for all you help.
That works.
LVL 38

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);
  if ShowSecForm(Self, IfThen(Sender = ButtonOrig, 0, 1)) then
    ShowMessage('Result of form = mrOk');

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

  TSecForm = class(TForm)
    fVar1: integer;
var SecForm: TSecForm;
function ShowSecForm(AOwner: TComponent; AButton: Integer);
var frm: TSecForm;
  Result := False;
  frm := TSecForm.Create(AOwner);
    frm.fVar1 := aButton;
    if frm.ShowModal = mrOk then 
      Result := True;    

Open in new window

LVL 14

Expert Comment

ID: 22668868
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.


Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA:…
Suggested Courses

688 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