Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 12-03-2010, 05:58 PM
Jeffijoe Jeffijoe is offline
Senior Member
 
Join Date: Nov 2009
Posts: 149
Default Creating Form1 twice with param?

Hi,

I am making an editor program. I am trying to make a button create my MainForm one more time, but passing a FileName string to it, so it will open that file in its editor. I have no clue on how I handle this.

I was hoping something like this would work:

Code:
if OpenDlg.Execute then
Begin
FFileName := OpenDlg.FileName;
  with TfrmMain.Create(Application) do
   Begin
     Show;
     CodeEdit.Lines.LoadFromFile(FFileName);
   End;
End;
If you dont understand my description of the problem, the code might.. :P
Reply With Quote
  #2  
Old 12-03-2010, 08:06 PM
SilverBoy SilverBoy is offline
Member
 
Join Date: Jun 2010
Posts: 46
Default

You can Overload Constructor of your TfrmMain class to give file name as parameter like this :

Code:
unit Unit2;

interface

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

type
  TTfrmMain = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
    Constructor Create(AOwner: TComponent; Const FileName: String); Overload;
  end;

var
  TfrmMain: TTfrmMain;

implementation

{$R *.dfm}

{ TTfrmMain }

constructor TTfrmMain.Create(AOwner: TComponent; const FileName: String);
begin
  Self.Caption:= FileName;
end;

end.
Now you can call it like this :

Code:
  if OpenDlg.Execute then
  begin
    FFileName := OpenDlg.FileName;
    with TfrmMain.Create(Application, FFileName) do
     Show;
  End;
Reply With Quote
  #3  
Old 12-04-2010, 09:47 AM
Jeffijoe Jeffijoe is offline
Senior Member
 
Join Date: Nov 2009
Posts: 149
Default

Quote:
Originally Posted by SilverBoy View Post
You can Overload Constructor of your TfrmMain class to give file name as parameter like this :

Code:
unit Unit2;

interface

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

type
  TTfrmMain = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
    Constructor Create(AOwner: TComponent; Const FileName: String); Overload;
  end;

var
  TfrmMain: TTfrmMain;

implementation

{$R *.dfm}

{ TTfrmMain }

constructor TTfrmMain.Create(AOwner: TComponent; const FileName: String);
begin
  Self.Caption:= FileName;
end;

end.
Now you can call it like this :

Code:
  if OpenDlg.Execute then
  begin
    FFileName := OpenDlg.FileName;
    with TfrmMain.Create(Application, FFileName) do
     Show;
  End;

I KNEW I could overload the constructor, just didn't know exactly how!

I tried it though, getting an Access Violation

Getting Access Violation 000008 or something..

Last edited by Jeffijoe; 12-04-2010 at 09:53 AM.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 06:05 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.