Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Procedures between 2 forms?

Status
Not open for further replies.

pierrotsc

Programmer
Nov 25, 2007
358
0
0
US
I am trying to keep my code clean. I have a main form that has a lot of Tedit component. each tedit as a onexit,onchange and onkeypress event.
I would like to add all these events to a different unit and not put all these lines of codes into the main unit.
Let's say that that my unit is called Fmain.
I have my other unit called umain.
Now when i press the onchange arrow, i only see the procedures coded in fmain. Is there a way i can see the ones in umain?
Thanks.
PO
 
In fmain, after the implementation line add:

Code:
uses umain

Alternatively, you can make a new unit, i.e. uShared.pas and put all the procedures in this new unit. Then in both fmain and umain, add:
Code:
uses uShared
I'd recommend you do this way if you are going to be using lots of functions from fmain.
 
Let me explain better because i still cannot see the edit001exit(sender:tobject); procedure when i click on the onexit pulldown arrow from my tedit located in my fmaim unit.

How do i have to write the procedure in the umain unit? It's not a form, just a unit. I tried:
procedure fmain.Edt002Exit(Sender: TObject);

But it's not working.

I hope i'm making some sense.
PO
 
The Delphi code editor (and the VCL connected to it) is pretty limited in terms of what you can do with it, I've found. That's one of them, it generally expects certain definitions within the descendant definition of TForm to be within the same unit as the definition.

To that end, if you do want to have code defined into the form's events as you describe, you have to define a procedure in your other unit, and then call that procedure for each event in the unit belonging to the form. For example, in the latest program I'm working on, I have 4 events which do (basically) the same thing. So I define the thing in a separate unit as procedure and then call it 4 times in the forms unit.

(that being said, this code I mention is pretty standardized - I wonder how well writing a derivative TForm class would work out in the editor. Probably not well)
 
How do i have to write the procedure in the umain unit? It's not a form, just a unit. I tried:
procedure fmain.Edt002Exit(Sender: TObject);

Take out the reference to fmain, so it is like this:
Code:
procedure Edt002Exit(Sender: TObject);

 
That's fine but when i go to my fmain unit and i click on the onexit event, i do not see it.
So my question is: can you share procedures between a vcl form and just aplain unit.
When i click on the onexit event in my fmain form, i would like to see the Edt002Exit.
I would like to put fmain.Edt002Exit(Sender: TObject);
into another unit and not in the fmain unit.

Thanks.
PO
 
I have a unit (no form attached) call TrialSubRoutines, it currently has a single function, CanContinue.
I use this unit in many different places:
Code:
[navy][i]// for automatic syntax highlighting see faq102-6487 
[/i][/navy][b]unit[/b] TrialSubRoutines;

[b]interface[/b]

[b]uses[/b] SysUtils, Dialogs;

[b]function[/b] CanContinue(TrialNum : [b]string[/b]): boolean;

[b]implementation[/b]

[b]uses[/b] dm_JMS;

[b]function[/b] CanContinue(TrialNum : [b]string[/b]): boolean;
[b]var[/b]
  ACasPre, ACasNum : [b]string[/b];
[b]begin[/b]
  [b]with[/b] dmJMS.qryCanContinue [b]do[/b]
  [b]begin[/b]
    SQL.Clear;
    SQL.Add([teal]'SELECT * FROM JMPTRIAL WHERE TRIALNUM = '[/teal] + TrialNum);
    Active := True;
    ACasPre := FieldByname([teal]'CASPRE'[/teal]).AsSTring;
    ACasNum := FieldByName([teal]'CASNUM'[/teal]).AsString;

    [b]if[/b] ACasPre <> [teal]'CV'[/teal] [b]then[/b]
    [b]begin[/b]
      SQL.Clear;
      SQL.Add([teal]'SELECT DISTINCT STSCOD FROM CMLIB/CMPHERMF WHERE CASPRE = '[/teal] +
       QuotedStr(ACasPre) + [teal]' AND CASNUM = '[/teal] + ACasNum +
       [teal]' AND HERTYP = '[/teal][teal]'SJ'[/teal][teal]' AND CHAR(HERNGDAT) = (SELECT MAX(STARTDAT) '[/teal] +
       [teal]'FROM JMPTRLDAYS WHERE TRIALNUM = '[/teal] + TrialNum + [teal]')'[/teal]);
      Active := True;

      If FieldByName([teal]'STSCOD'[/teal]).AsInteger <> [purple]4[/purple] [b]then[/b]
        Result := False
      [b]else[/b]
        Result := True;
      Active := False;
    [b]end[/b]
    [b]else[/b]
      Result := True;
  [b]end[/b];
[b]end[/b];

[b]end[/b].

I have a form, called CurrentTrials which uses the CanContinue function. The CurrentTrials unit is set up like this:
Code:
[b]unit[/b] CurrentTrials;

[b]interface[/b]

[b]uses[/b]
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, StrUtils;

[b]type[/b]
  TfCurrentTrials = [b]class[/b](TForm)
    lbCases: TListBox;
    Label1: TLabel;
    rgWhatToDo: TRadioGroup;
    [b]procedure[/b] lbCasesDblClick(Sender: TObject);
    [b]procedure[/b] FormClose(Sender: TObject; [b]var[/b] Action: TCloseAction);
  [b]private[/b]
    [navy][i]{ Private declarations }[/i][/navy]
  [b]public[/b]
    [navy][i]{ Public declarations }[/i][/navy]
  [b]end[/b];

[b]var[/b]
  fCurrentTrials: TfCurrentTrials;

[b]implementation[/b]

[b]uses[/b] ContinueTrial, [COLOR=red]trialsubroutines[/color], RecordCrtmuse;

above the unit that I need to reference for the CanContinue function is in the USES clause in the IMPLEMENTATION section.

To use the function:
Code:
[b]procedure[/b] TfCurrentTrials.lbCasesDblClick(Sender: TObject);
[b]begin[/b]
  [b]if[/b] rgWhatToDo.ItemIndex < [purple]0[/purple] [b]then[/b]
  [b]begin[/b]
    ShowMessage([teal]'Please select what task you are performing!'[/teal]);
    Exit;
  [b]end[/b]
  [b]else[/b]
  [b]begin[/b]
    [b]if[/b] rgWhatToDo.ItemIndex = [purple]0[/purple] [b]then[/b]
    [b]begin[/b]
      [b]if[/b] [COLOR=red]trialsubroutines.CanContinue[/color](LeftStr( lbCases.Items[lbCases.ItemIndex], pos([teal]'-'[/teal], lbCases.Items[lbCases.ItemIndex]) - [purple]1[/purple] )) [b]then[/b]
      [b]begin[/b]
        frmContinueTrial.SetUpTrialInfo(LeftStr( lbCases.Items[lbCases.ItemIndex], pos([teal]'-'[/teal], lbCases.Items[lbCases.ItemIndex]) - [purple]1[/purple]  ));
        frmContinueTrial.Show;
        SElf.close;
      [b]end[/b]
      [b]else[/b]
      [b]begin[/b]
        ShowMessage([teal]'That case has not been docketed as '[/teal][teal]'Continued'[/teal][teal]'.  Please update the AS400 and try again.'[/teal]);
        self.close;
      [b]end[/b];
    [b]end[/b]
    [b]else[/b] [b]if[/b] rgWhatToDo.ItemIndex = [purple]1[/purple] [b]then[/b]
    [b]begin[/b]
      frmRecordCrtmUse.SetUpTrialInfo(LeftStr( lbCases.Items[lbCases.ItemIndex], pos([teal]'-'[/teal], lbCases.Items[lbCases.ItemIndex]) - [purple]1[/purple]  ));
      frmRecordCrtmUse.Show;

      Self.Close;
    [b]end[/b];
  [b]end[/b];
[b]end[/b];

Does that help you see where you need to make modifications to your code in order to use a function from a unit in a form?





Leslie

In an open world there's no need for windows and gates
 
I see...So really you do not see the cancontinue function when you pull down an event arrow. The thing is that i'm not calling a function, i am calling a procedure in an onexit event.

I have a Tedit that has 3 events: Onchange, onkeypress and onexit.

Right now, if i click on the down arrow of the onexit, i will see all the onexit procedures defined in my current unit. Now can i move this procedure from my main unit to another one (that is not a form) and still see it appearing in my drop down onexit event?

Thanks. Sorry to be a pain...
PO
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top