Solved

How to jump from Form to TMainFrm-procedure ?

Posted on 2004-10-26
300 Views
Last Modified: 2010-04-05
Hi,

I need solutions to some small problem I am having with Delphi. Please provide a detailed resolutions (if possible)

1. Please give an illustratation on how to jump to TMainFrm procedure XYZ from Form1.
2. Also how can I disable a form (event driven) so its not created with TMainFrm (OnCreate) event?

Thanks!

- Adnan Afzal
0
Question by:mdadnan
    15 Comments
     
    LVL 27

    Expert Comment

    by:kretzschmar
    >Please provide a detailed resolutions
    too less time, too less points

    >1. Please give an illustratation on how to jump to TMainFrm procedure XYZ from Form1.
    ......
    implementation
    uses MainFormUnitNameHere;

    procedure foo;
    begin
      MainFormNameHere.SomeMethod(Params);  //call a mainform-method
    end;


    >2. Also how can I disable a form (event driven) so its not created with TMainFrm
    >(OnCreate) event?

    have a global var maybe named
    CreateFormX : Boolean;

    use in the onCreate-event

    ....
      If CreateFormX then
        //Create the Form
    ....

    meikl ;-)
    0
     
    LVL 12

    Expert Comment

    by:esoftbg
    //............................ MainForm
    type
      TMainForm=class(TForm)
          MainMenu1: TMainMenu;
          Panel1: TPanel;
          Label1: TLabel;
          Button1: TButton;
          procedure Button1Click(Sender: TObject);
        private  
        { Private declarations }
        public    
          procedure XYZ; // it must be created into the public section or above private section as procedure Button1Click();
        { Public declarations }
      end ;
    //........

    procedure TMainForm.XYZ;
    // var ........
    begin
    //........
    end;

    //............................ Form1
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      MainForm.XYZ;                            // calling this procedure from MainForm
      MainForm.Button1Click(MainForm) // calling this procedure from MainForm
    end;
    0
     
    LVL 12

    Expert Comment

    by:esoftbg
    It does not matter where is a TForm created, it could be disabled for example:
     
    procedure TMainForm.XYZ;
    begin
      Form2.Enabled := False;
    end;
    0
     
    LVL 12

    Expert Comment

    by:esoftbg
    Hi kretzschmar,

    excuse me, can you take a look at:

    http://experts-exchange.com/Programming/Programming_Languages/Delphi/Q_21173968.html

    http://experts-exchange.com/Programming/Programming_Languages/Delphi/Q_21180393.html

    There I got all the points, but I didn't deserve in fullest ....
    You may correct these.
    0
     

    Author Comment

    by:mdadnan
    Hi again,

    I am getting these errors:

     MainFrm.XYZ;                               [Error] Unit2.pas(28): Undeclared identifier: 'MainFrm'
     MainFrm.Button1Click(MainFrm);  [Error] Unit2.pas(29): Missing operator or semicolon

    Please note procedure XYZClick(Sender: TObject); is already defined public in unit1.

    If possible please zip send me the working  example at mdadnan@gmail.com.

    Thanks.
    0
     
    LVL 12

    Expert Comment

    by:esoftbg
    Ooops, I forgot to mention that before call a mettod from another unit, you need to declare this unit into the uses-clause of the unit where will be the call written:

    //............................ Form1

    implementation

    {$R *.DFM}

    uses
      MainUnit; // the unit of the MainForm

    procedure TForm1.Button1Click(Sender: TObject);
    begin
      MainForm.XYZ;                            // calling this procedure from MainForm
      MainForm.Button1Click(MainForm) // calling this procedure from MainForm
    end;
    0
     
    LVL 12

    Expert Comment

    by:esoftbg
    I will make and send an example immediately ....
    0
     

    Author Comment

    by:mdadnan
    Hi again,

    I am having a hard time figuring this out. I had already added the main form reference under 'uses' as suggested by kretzschmar in his first post.

    esoftbg, I appreciate your prompt responses but this is not working out here. I am receiving the same errors explained in my last post above. :-\

    Thanks for all the help.
    0
     
    LVL 27

    Expert Comment

    by:kretzschmar
    i'm sure you get it sorted with esoftbg's advice, so i'm still listening . . .
    0
     
    LVL 12

    Accepted Solution

    by:
    mdadnan,
    I have some problems with my computer, but now I sent an example to your e-mail address ....
    0
     
    LVL 12

    Expert Comment

    by:esoftbg
    kretzschmar,
    thanks for your responce about my  request.
    Emil
    0
     

    Author Comment

    by:mdadnan
    Thank you esoftbg for sending an excellent example.
    0
     
    LVL 12

    Expert Comment

    by:esoftbg
    You are welcome mdadnan !
    0
     
    LVL 27

    Expert Comment

    by:kretzschmar
    you should post your sample here, esoftbg ;-)
    0
     
    LVL 12

    Expert Comment

    by:esoftbg
    //........ The Project ........
    program Q_21182908;

    uses
      Forms,
      MainUnit in 'MainUnit.pas' {MainForm},
      Unit1 in 'Unit1.pas' {Form1},
      Unit2 in 'Unit2.pas' {Form2};

    {$R *.res}

    begin
      Application.Initialize;
      Application.CreateForm(TMainForm, MainForm);
      Application.CreateForm(TForm1, Form1);
      Application.CreateForm(TForm2, Form2);
      Application.Run;
    end.

    //........ MainUnit ........
    unit MainUnit;

    interface

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

    type
      TMainForm = class(TForm)
        Button1: TButton;
        Button2: TButton;
        Memo1: TMemo;
        Memo2: TMemo;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        private
        { Private declarations }
        public
          procedure XYZ;
          procedure ZYX;
        { Public declarations }
      end;

    var
      MainForm: TMainForm;

    implementation

    uses
      Unit1, Unit2;

    {$R *.dfm}

    procedure TMainForm.Button1Click(Sender: TObject);
    begin
      if Form1.Visible then
        Form1.Hide
      else
        Form1.Show;
    end;

    procedure TMainForm.Button2Click(Sender: TObject);
    begin
      if Form2.Visible then
        Form2.Hide
      else
        Form2.Show;
    end;

    procedure TMainForm.XYZ;
    begin
      Memo2.Lines.Assign(Memo1.Lines);
    end;

    procedure TMainForm.ZYX;
    begin
      Memo2.Clear;
    end;

    end.

    //........ MainForm ........
    object MainForm: TMainForm
      Left = 247
      Top = 114
      Width = 570
      Height = 288
      Caption = 'MainForm'
      Color = clBtnFace
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      OldCreateOrder = False
      PixelsPerInch = 96
      TextHeight = 13
      object Button1: TButton
        Left = 32
        Top = 16
        Width = 96
        Height = 25
        Caption = 'Show Form1'
        TabOrder = 0
        OnClick = Button1Click
      end
      object Button2: TButton
        Left = 32
        Top = 64
        Width = 96
        Height = 25
        Caption = 'Show Form2'
        TabOrder = 1
        OnClick = Button2Click
      end
      object Memo1: TMemo
        Left = 160
        Top = 16
        Width = 185
        Height = 224
        Lines.Strings = (
          'Today is a cool, sunny day without '
          'rain. ')
        TabOrder = 2
      end
      object Memo2: TMemo
        Left = 352
        Top = 16
        Width = 185
        Height = 224
        TabOrder = 3
      end
    end

    //........ Unit1 ........
    unit Unit1;

    interface

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

    type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        Button3: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
        private
        { Private declarations }
        public
        { Public declarations }
      end;

    var
      Form1: TForm1;

    implementation

    uses
      MainUnit, Unit2;

    {$R *.dfm}

    procedure TForm1.Button1Click(Sender: TObject);
    begin
      MainForm.XYZ;
    end;

    procedure TForm1.Button2Click(Sender: TObject);
    begin
      MainForm.ZYX;
    end;

    procedure TForm1.Button3Click(Sender: TObject);
    begin
      Form2.Enabled := not Form2.Enabled;
    end;

    end.

    //........ Form1 ........
    object Form1: TForm1
      Left = 243
      Top = 304
      Width = 222
      Height = 204
      Caption = 'Form1'
      Color = clBtnFace
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      FormStyle = fsStayOnTop
      OldCreateOrder = False
      PixelsPerInch = 96
      TextHeight = 13
      object Button1: TButton
        Left = 16
        Top = 22
        Width = 75
        Height = 25
        Caption = 'XYZ'
        TabOrder = 0
        OnClick = Button1Click
      end
      object Button2: TButton
        Left = 14
        Top = 62
        Width = 75
        Height = 25
        Caption = 'ZYX'
        TabOrder = 1
        OnClick = Button2Click
      end
      object Button3: TButton
        Left = 118
        Top = 110
        Width = 75
        Height = 25
        Caption = 'Enabled ?'
        TabOrder = 2
        OnClick = Button3Click
      end
    end

    //........ Unit2 ........
    unit Unit2;

    interface

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

    type
      TForm2 = class(TForm)
        Memo1: TMemo;
        private
        { Private declarations }
        public
        { Public declarations }
      end;

    var
      Form2: TForm2;

    implementation

    {$R *.dfm}

    end.

    //........ Form2 ........
    object Form2: TForm2
      Left = 515
      Top = 335
      Width = 283
      Height = 221
      Caption = 'Form2'
      Color = clBtnFace
      Font.Charset = DEFAULT_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'MS Sans Serif'
      Font.Style = []
      FormStyle = fsStayOnTop
      OldCreateOrder = False
      PixelsPerInch = 96
      TextHeight = 13
      object Memo1: TMemo
        Left = 6
        Top = 12
        Width = 223
        Height = 143
        Lines.Strings = (
          'You can edit if Form2.Enabled is True.')
        TabOrder = 0
      end
    end
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    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…
    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…
    In this Experts Exchange video Micro Tutorial, I'm going to show how small business owners who use Google Apps can save money by setting up what is called a catch-all email address in their Gmail accounts. By using the catch-all feature, small busin…
    This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

    933 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

    11 Experts available now in Live!

    Get 1:1 Help Now