Solved

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

Posted on 1998-12-06
8
163 Views
Last Modified: 2010-08-05
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
Comment
Question by:delphi3
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 1

Expert Comment

by:Romanian
ID: 1349608
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
 
LVL 7

Expert Comment

by:BlackMan
ID: 1349609
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
 
LVL 4

Expert Comment

by:dwwang
ID: 1349610
Why not use form inheritance?
0
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!

 
LVL 1

Expert Comment

by:Romanian
ID: 1349611
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
 
LVL 4

Expert Comment

by:dwwang
ID: 1349612
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
 
LVL 4

Author Comment

by:delphi3
ID: 1349613
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
 
LVL 4

Accepted Solution

by:
dwwang earned 60 total points
ID: 1349614
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
 
LVL 4

Author Comment

by:delphi3
ID: 1349615
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

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

Suggested Solutions

Title # Comments Views Activity
Error E2158 compiling with Delphi XE10 Seattle 2 123
Dev express lookupcombo 3 44
firemonkey keyboard covers the controls 1 49
MS Access from Delphi 31 77
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…

679 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