Solved

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

Posted on 1998-12-06
8
158 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
Comment Utility
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
Comment Utility
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
Comment Utility
Why not use form inheritance?
0
 
LVL 1

Expert Comment

by:Romanian
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 4

Expert Comment

by:dwwang
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

7 Experts available now in Live!

Get 1:1 Help Now