• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 171
  • Last Modified:

14 Forms from one but write a unique procedure to each?

Using Delphi4,I want to write a unique procedure to each unit/form as the new form is being created from a model.The goal is to reduce the units/forms contained within the project.The specific procedure references a keypress event for the specific unit/form.  The model unit contains the onkeypress procedure and checked in the model's object inspector. It does not work, I think,  unless the unit# is entered in the procedure in that specific unit/form. What are the lines of code written to produce this effect in the multiple form creator.?
the model unit/form  that needs to be created in each begins as:
unit1......


procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
  If key = char(105) then
    begin
     Unit1.Form1.Top :=  Unit1.Form1.Top - 1 ;
    end;

Etc.
the unit number for each must be changed.

0
delphi3
Asked:
delphi3
  • 3
  • 2
  • 2
  • +1
1 Solution
 
RomanianCommented:
Create 14 procedures with KeyPress event handler for each form and use next code to create form:

procedure CreateFormFromModel(NumberOfForm: Integer);
var
  F: TForm1;
begin
  F := TForm1.Create(Application);
  F.Tag := NumberOfForm;
  case NumberOfForm of
   1: F.OnKeyPress := FormKeyPress1;
   2: F.OnKeyPress := FormKeyPress2;
.....
  14: F.OnKeyPress := FormKeyPress14;
  end;
end;
0
 
BlackManCommented:
Create a Virtual procedure, that are called in the FormKeyPress method, and override it for every instance you create of your form.. Something like this:

Type
  TMyFormModel = Class(TForm)
    Procedure HandleKeyDown(Sender : TObject; Var Key : Char); Virtual;
   ...
  End


  TForm1 = Class(TMyFormModel)
    Procedure HandleKeyDown(Sender : TObject; Var Key : Char); Override;    
  End;

Then in the TMyFormModel.KeyPress method, you just call HandleKeyDown and it will execute HandleKeyDown for the active form...
0
 
dwwangCommented:
Why not use form inheritance?
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
RomanianCommented:
dwwang:
I think Delphi3 need minimize form count so form inheritance not good because you must create all 14 forms. You can create Template form with common keypress handler and create one derived form with changing onkeypress handlers.
0
 
dwwangCommented:
Just write a single event handler and  assign other forms'
OnKeyPress Event to it:

procedure TForm1.MyOwnFormKeyPressHandler(Sender: TObject; var Key: Char);
   begin
     If key = char(105) then
       begin
        (sender as TForm).Top :=  (sender as TForm).Top - 1 ;
   end;

then in Form1's code:

uses unit2(contains TForm2, etc.)...

Form2.OnKeyPress:=MyOwnFormKeyPressHandler;
..

The core idea is that the "Sender" parameter of the OnKeyPress
event will be the current form that the key is pressed.
0
 
delphi3Author Commented:
dwwang,
Thanks for your response >>11:40PM PST<<. When I entered the procedure supplied into my program it built with no errors
however reading and using your next hint :

then in Form1's code(the sample from which all others are being made):

uses unit2(contains TForm2, etc.)...

During the build of the project an error exists  because, and I am only guessing, that unit2 and so on,  do not exist in the  development but during runtime.
Sorry,
Delphi3
0
 
dwwangCommented:
It's impossible for a unit not existing during design time.
However, I do not mean you must use those units, just that to let the code be able to access those forms which you want to assign the event handler.

I guess you dynamically create forms, right? So you needn't use unit2 etc.
Just create your forms and assign the event handler.

for example:

myform:tmyform;
.
myForm:=TMyForm.Create;
MyForm.OnKeyPress:=MyOwnFormKeyPressHandler;
0
 
delphi3Author Commented:
Dwwang, ( restatement of the first comment) + new additions

    Thanks for your response >>11:40PM PST<<. When I entered the procedure
     supplied into my program it built with no errors
     however reading and using your next hint :

     then in Form1's code(the sample from which all others are being made):

     uses unit2(contains TForm2, etc.)...

     During the build of the project an error exists  because, and I am only guessing,
     that unit2 and so on,  do not exist in the  development but during runtime.
     Sorry,
     Delphi3


then an later update  :

After cleaning up some other issues,  the uses statement given by you  was not used and not needed. Then further tweeking of the code, with your first procedure in place,  the intended result was obtained.  Cheers and a BIG THANKS.

 By the way it was a part of a coolform puzzle project that had 14 individual shapes launched at one time,  that move independently  both with the mouse for approximate positioning   and keypresses for a  micro move for precise manuvering. A personal challenge project.

Delphi3




0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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