Solved

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

Posted on 1998-12-06
8
159 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PDF library for Delphi 2 107
How to use Hashing sha1 in Delphi 2010 4 206
Create a path if not exists 7 69
Dynamically Created Query 3 50
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now