Delphi form overriding form inheritance

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.
henryreynoldsAsked:
Who is Participating?
 
reynaldioCommented:
- 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;
0
 
Geert GOracle dbaCommented:
in frmMain you are using KeyPreview ?
in frmCapture set it off

or provide a little more code so we can see what you ment ...
0
 
MerijnBSr. Software EngineerCommented:
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

0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
henryreynoldsAuthor 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
0
 
MerijnBSr. Software EngineerCommented:
> 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)
0
 
henryreynoldsAuthor Commented:
How can I prevent calling the form again.
0
 
Geert GOracle dbaCommented:
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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.