Solved

Add my property to TForm

Posted on 1999-01-01
8
367 Views
Last Modified: 2010-04-04
i would like to add my property to TForm properties page, can i and how ? Please give an example code here..
0
Comment
Question by:dumper
[X]
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
  • 2
  • +1
8 Comments
 
LVL 8

Expert Comment

by:ZifNab
ID: 1354087
Hi dumper,

A very stupid example and I hope that I understood your question correctly :

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  private
    { Private declarations }
    FTest : String;
  public
    { Public declarations }
    property test:string read FTest write FTest;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
 FTest := 'This is a test';
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
 Caption := Test;
end;

end.

Regards, Zif.
0
 
LVL 5

Expert Comment

by:heathprovost
ID: 1354088
I dont think that is what he meant, I think he wants the property to be published in the object inspector.  Here is some code samples

The following unit would be your custom form with your new properties:

unit MyNewForm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, DSGNIntf;
              //^this unit is what allows registering with Delphi as custom form
   
type
  TMyNewForm = class (TForm)
  private
    fValue: Integer;
    fOnChangeValue: TNotifyEvent;
    procedure SetValue (Value: Integer);
  published
    property Value: Integer
      read fValue write SetValue;
    property OnChangeValue: TNotifyEvent
      read fOnChangeValue write fOnChangeValue;
    //Adding new property and event here
  end;

procedure Register;

implementation

procedure TMyNewForm.SetValue (Value: Integer);
begin
  if Value <> fValue then
  begin
    fValue := Value;
    if Assigned (fOnChangeValue) then
      fOnChangeValue (self);
  end;
end; //just property implementation method

procedure Register;
begin
  RegisterCustomModule (TMyNewForm, TCustomModule);
  //This is what does the magic - it installs the form as a system module so it can be seen by the ToolsApi - getting to that part
end;

end.



Now for the other piece of the puzzle:

unit FormWizard;

interface

uses
  ExptIntf, EditIntf, Windows;

type
  TMyNewFormExpert = class (TIExpert)
  public
    function GetStyle: TExpertStyle; override;
    function GetName: string; override;
    function GetAuthor: string; override;
    function GetComment: string; override;
    function GetPage: string; override;
    function GetGlyph: HICON; override;
    function GetState: TExpertState; override;
    function GetIDString: string; override;
    function GetMenuText: string; override;
    procedure Execute; override;
  end;

procedure Register;

implementation

uses
  Dialogs, ToolIntf, SysUtils;

function TMyNewFormExpert.GetStyle: TExpertStyle;
begin
  // show up in the Help menu
  Result := esStandard;
end;

function TMyNewFormExpert.GetName: String;
begin
  // official name
  Result := 'Form Wizard'
end;

function TMyNewFormExpert.GetAuthor: string;
begin
  //You!
  Result := 'Me';
end;

function TMyNewFormExpert.GetComment: String;
begin
  //Whatever you want
  Result := 'Installs custom form';
end;

function TMyNewFormExpert.GetPage: string;
begin
  //Leave like this
  Result := '';
end;

function TMyNewFormExpert.GetGlyph: HICON;
begin
  //Leave like this
  Result := 0;
end;

function TMyNewFormExpert.GetState: TExpertState;
begin
  // always enabled
  Result := [esEnabled];
end;

function TMyNewFormExpert.GetIDString: String;
begin
  // must be unique
  Result := 'FormWizForMyNewForm'
end;

function TMyNewFormExpert.GetMenuText: String;
begin
  // the text of the menu item
  Result := '&Create a MyNewForm'
end;

procedure TMyNewFormExpert.Execute;
//This is what actually installs the form into the IDE.  It queries the system for the module (you must install Form first!) and adds a menu item to the help menu which allows you to install it

var
  ModuleName, FormName, FileName: string;
  ModIntf: TIModuleInterface;
begin
  ToolServices.GetNewModuleAndClassName ('MyNewForm', ModuleName, FormName, FileName);
  ModIntf := ToolServices.CreateModuleEx (FileName, FormName, 'MyNewForm', '',
  nil, nil,    [cmNewForm, cmAddToProject, cmUnNamed]);
  ModIntf.ShowSource;
  ModIntf.ShowForm;
  ModIntf.Release;
end;

procedure Register;
begin
  RegisterLibraryExpert(TMyNewFormExpert.Create);
end;

end.


Whew!  Now that that is done, lets move on.  You must follow the following steps carefully to get this right:

1. Save both of the above units somewhere in your search path after making whatever modifications you want to them.

2. Open the MyNewForm Unit in Delphi and click Component>Install Component.  Go ahead and compile.  If you have bug in your code you can address it here.

3. Once that is done let the package library rebuild, close the MyNewForm unit and open the FormWizard unit.  Repeat step 2 on it.

4. Now close all files and goto the help menu - you should see an entry that says "Create a MyNewForm" (or whatever you changed it to).  Click it and your form should pop up in the IDE, new properties and all!  But you arent finished yet

5. You must now a "MyNewForm" to the end of the uses clause of the form code that just popped up. Go ahead and save this form now somewhere (it will be a template)

6. Right click on the forms client area and select "Add to repository".  Fill in the blanks.  If you ALWAYS want to use this form you can check the mainform box.

7. Now to use the form all you have to do is click file>new form and select it from the list.  If you want it to be the MainForm the easiest thing to do is just create a new project, add your form to it, and remove the original TForm.


I hope this was all clear enough for you to understand.  It is very difficult to explain clearly.  Hope this helps
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1354089
Ha, yes now I understand the q'n too. Zif.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:heathprovost
ID: 1354090
BTW - you didnt clarify and I didnt think about this earlier.  Are you running Delphi 3?  If not then the above code will not work.  I am not sure how to do this in Delphi 2 or even if it can be done at all.  I dont think it can.
0
 

Author Comment

by:dumper
ID: 1354091
heathprovost, you are right...please paste as answer...
0
 

Author Comment

by:dumper
ID: 1354092
heathprovost answer is most correct
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1354093
Heathprovost: I am impressed!

Cheers,

Raymond.
0
 
LVL 5

Accepted Solution

by:
heathprovost earned 20 total points
ID: 1354094
Sorry, I was away for a while - glas I could help.  See earlier comment for complete answer.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

729 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