Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 1998-12-06
8
Medium Priority
?
166 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
[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 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
Technology Partners: 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 240 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

670 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