Question

I need to select the component on currently focused/active form, but can't figure the way of doing this completely. So, I have a delphi unit with all common procedures on it, so I call them from other forms. Now, the thing is, that with one of the procedures I call the component show action, but, since the component is placed on each form (couldn't find a way of placing one component, which can be used by all forms, but I think this somehow can't be done. Am I wrong? ), I need to call a show event for the component on currently active form, otherwise the specified form, from which I call the component, gets active and focused.

So far I've tried by getting current form name, via Screen.ActiveForm and Screen.ActiveForm.Name, but none of this works, since the compiler doesn't compile because component parent is not defined (getting error " 'TForm' does not contain a member named 'KeyBoard1' "...)

Here's the procedure code:

  procedure KeyDownEvents(var Key: Word; Shift: TShiftState);
    begin
    CurrentForm:=Screen.ActiveForm.Name;
    if Key = VK_F9 then CurrentForm.KeyBoard1.Show;
    end;

Using global variable

    var CurrentForm: TForm;

Where and what am I missing, since I've tried like 10 different combinations...?

Thanks,

Marc.

Ps: as asked above, is there a way of placing or calling component, to be active on any form, or should it be placed in each form, so no focus is changed...? I know in MDI I could use a component in main form (as far as I know...), but I'm working in SDI, because of multiple monitors needed...


Complete code for David:

unit CommonProcedures;

interface

uses Classes, Dialogs, StdCtrls, Math, Winapi.Windows, Winapi.Messages,
Vcl.Controls, Vcl.Forms, AdvTouchKeyboard;

procedure KeyDownEvents(var Key: Word; Shift: TShiftState);

var
CurrentForm: TComponentName;
KeyBoard1Visible: Boolean;

implementation


uses Startup, Main, Controller, Settings, Patch, Output, StageVisual, FixturesEditor,
FixturesEditorGlobal;

procedure KeyDownEvents(var Key: Word; Shift: TShiftState);
 begin
  CurrentForm:=Screen.ActiveForm.Name;
   if ((ssAlt in Shift) and (Key = VK_F4)) then Key:=0;
   if Key = VK_F1 then Main1.Show;
   if Key = VK_F2 then Controller1.Show;
   if Key = VK_F3 then Settings1.Show;
   if Key = VK_F4 then Patch1.Show;
   if Key = VK_F5 then Output1.Show;
   if Key = VK_F6 then StageVisual1.Show;
   if Key = VK_F7 then FixturesEditor1.Show;
   if Key = VK_F8 then FixturesEditor2.Show;
   if Key = VK_F9 then
     begin
       if KeyBoard1Visible=False
       then
        begin
          KeyBoard1Visible:=True;
          CurrentForm.KeyBoard1.Show;
        end
       else
        begin
         KeyBoard1Visible:=False;
         CurrentForm.KeyBoard1.Hide;
       end;
     end;
  end;

end.

There you can see, that I only left out all other key events, since they're completely unimportant, but I did as well left out process of checking whether the keyboard is shown or hidden, so the VK_F9 behaves as toggle key.

That is so far the only procedure in this unit, as I only started to create the program a few days ago, so it's still in first base, so said... And yes, as you can see and guess, it's sort of light-show controller program, the project for my senior year.

Ps: don't see the reason to downvote the question when I edited it to be even clearer...

Was it helpful?

Solution

It can be done. It does kind of depend on exactly how the components are setup on the forms, as in whether they are the same name and type. For example, here are two methods:

procedure TfrmSiteMapMain.dummy();
Var
  comp : TComponent;
begin
  // Find component by Name
  comp := screen.ActiveForm.FindComponent('btnMyButtonName');
  if comp <> nil then TButton(comp).Click;
end;

or

procedure TfrmSiteMapMain.dummy();
Var
  comp : TControl;
  i : integer;
  frm : TForm;
begin
  frm := screen.ActiveForm;
  for i := 0 to frm.ControlCount -1 do begin
    comp := frm.Controls[i];
    // If you had multiple components, here's where you could check its name, tag, etc
    if comp.ClassNameIs('TButton') then begin
      Break;
    end;
  end;
  if comp <> nil then TButton(comp).Click;
end;

Be aware that the forms have a Controls collection and a Component collection.

Edited to add: Considering the code you posted above, you could to this: (I don't have the Keyboard component, but I'm guessing it's a TKeyboard)

if Key = VK_F9 then ToggleKeyboard;

procedure ToggleKeyboard;
Var
  frm : TForm;
  comp : TComponent;
begin
  frm := Screen.ActiveForm;
  if frm <> nil then begin
    comp := frm.FindComponent('Keyboard1');
    if comp <> nil then begin
      TKeyboard(comp).Visible := not TKeyboard(comp).Visible; // toggle it
    end;
  end;
end;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top