[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 312
  • Last Modified:

How to jump from Form to TMainFrm-procedure ?

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
mdadnan
Asked:
mdadnan
  • 9
  • 3
  • 3
1 Solution
 
kretzschmarCommented:
>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
 
esoftbgCommented:
//............................ 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
 
esoftbgCommented:
It does not matter where is a TForm created, it could be disabled for example:
 
procedure TMainForm.XYZ;
begin
  Form2.Enabled := False;
end;
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
esoftbgCommented:
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
 
mdadnanAuthor Commented:
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
 
esoftbgCommented:
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
 
esoftbgCommented:
I will make and send an example immediately ....
0
 
mdadnanAuthor Commented:
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
 
kretzschmarCommented:
i'm sure you get it sorted with esoftbg's advice, so i'm still listening . . .
0
 
esoftbgCommented:
mdadnan,
I have some problems with my computer, but now I sent an example to your e-mail address ....
0
 
esoftbgCommented:
kretzschmar,
thanks for your responce about my  request.
Emil
0
 
mdadnanAuthor Commented:
Thank you esoftbg for sending an excellent example.
0
 
esoftbgCommented:
You are welcome mdadnan !
0
 
kretzschmarCommented:
you should post your sample here, esoftbg ;-)
0
 
esoftbgCommented:
//........ 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

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 9
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now