We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Delphi form overriding form inheritance

henryreynolds
on
Medium Priority
917 Views
Last Modified: 2012-06-27
Good day

I have frmMain and frmCapture that are inherited from frmMain.

In frMain on key press event I call forms depending on the short cut key.

But when frmCapture are visible, then frmCapture must ignore the key press event that happens in frmMain, or it must ignore the VK_F5 key.

Is this possible.
Comment
Watch Question

Geert GOracle dba
CERTIFIED EXPERT
Top Expert 2009

Commented:
in frmMain you are using KeyPreview ?
in frmCapture set it off

or provide a little more code so we can see what you ment ...
MerijnBSr. Software Engineer
CERTIFIED EXPERT

Commented:
Like Geert suggests, set frmMain.KeyPreview to true
in frmMain, change the Key to 0 after you've handled it:
procedure frmMain.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
 // do what you want to do with the key
 Key := 0; // stops other controls from handling it
end;

Open in new window

Author

Commented:
in frmMain I have the following

procedure TfrmMain.FormKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
   if Key = VK_F3 then
   begin
      Application.CreateForm(TfrmAlphaSearch, frmAlphaSearch);
      try
         frmAlphaSearch.ShowModal();
      finally
         frmAlphaSearch.Free;
      end;
   end;


   if Key = VK_F5 then
   begin
         Application.CreateForm(TfrmSettlements, frmSettlements);
         try
            frmSettlements.ShowModal();
         finally
            frmSettlements.Free;
         end;
    end;
end;

When I am in frmSettlements I dont want to allow F5 -> to call again frmSettlements because I get a acess violation.

thanx
MerijnBSr. Software Engineer
CERTIFIED EXPERT

Commented:
> When I am in frmSettlements I dont want to allow F5 -> to call again frmSettlements because I get a acess violation

I don't think that will happen since you show the form modal (ShowModal)

Author

Commented:
How can I prevent calling the form again.
Geert GOracle dba
CERTIFIED EXPERT
Top Expert 2009

Commented:
put a check and see if the form is of that class or not
procedure TfrmMain.FormKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
   if not (Self is TfrmAlphaSearch) and (Key = VK_F3) then
   begin
      Application.CreateForm(TfrmAlphaSearch, frmAlphaSearch);
      try
         frmAlphaSearch.ShowModal();
      finally
         frmAlphaSearch.Free;
      end;
   end;
 
 
   if not (self is TfrmSettlements) and (Key = VK_F5) then
   begin
         Application.CreateForm(TfrmSettlements, frmSettlements);
         try
            frmSettlements.ShowModal();
         finally
            frmSettlements.Free;
         end;
    end;
end;

Open in new window

- why don't you set FormKeyUp virtual in frmMain

procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState); virtual;


- and override that procedure in frmSettlements


procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState); override;
begin
    if Key = VK_F3 then Key := 0;
    inherited FormKeyUp(Sender, Key, Shift);
end;

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.