2017-08-17 108 views
1

我有一个很大的应用程序,当用户单击菜单栏时正在创建几个窗体。我想通过在编辑组件中键入其名称来给他们调用子窗体的功能。 ChildForm通常通过以下过程创建:德尔福MDI创建子窗体

procedure CreateChildForm(ChildClass: TComponentClass; var Reference); 
begin 
    Screen.Cursor := crHourGlass; 
    try 
    if (FindChildForm(ChildClass, TObject(Reference))) then 
begin 
    (TObject(Reference) as TForm).WindowState := wsNormal; 
    (TObject(Reference) as TForm).Show; 
end 
else 
    Application.CreateForm(ChildClass,Reference); 
    except 
    on e: exception do 
    ErrorMsg('Exception -> CreateChildForm ' + e.message); 
    end; 
    Screen.Cursor := crDefault; 
end; 

这样子。

procedure TMain.acPN010Execute(Sender: TObject); 
begin 
    CreateChildForm(TfrmPN010,frmPN010); 
end; 

如何将字符串作为参数传递给该过程?

+0

你不能。一个字符串就是一个字符串。您需要一种方法来查找给定字符串的引用。但这听起来不像是一个非常好的设计。 –

+0

我会使用组合框,从菜单栏中填充条目。通过这种方式,用户不能错误输入,每个项目条目的对象中都显示了名称确切的Info。 – nil

+0

也许[GetClass](http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/Classes_GetClass.html)函数可以帮助你,它允许你从一个类开始班级名称。 – ExDev

回答

1

为了扩充我的评论,这里有一个使用TCombobox来创建子表单的例子。这可能潜在地用于基本上重新使用您的菜单代码并自动调整以更改您的菜单 - 例如当新的孩子形式被添加到您的项目。

你没有提及你的菜单类的类型或结构,但这应该是可以调整的,具体取决于实现细节。

有了下面的代码,你可以填充一个组合框的ItemsTMenuItem的所有子项:

procedure FillCombobox(AMenuItem: TMenuItem; ACombobox: TCombobox); 
var 
    I: Integer; 
begin 
    for I := 0 to AMenuItem.Count - 1 do 
    ACombobox.Items.AddObject(AMenuItem.Items[I].Caption, AMenuItem.Items[I]); 
end; 

使用OnSelect事件的组合框,你可以做什么在给定的菜单项,点击做了:

procedure TForm1.ComboBox1Select(Sender: TObject); 
begin 
    TMenuItem(ComboBox1.Items.Objects[ComboBox1.ItemIndex]).Click; 
end; 

使用这个和创建子代码构成您提供一个小的测试项目 - 什么,除了FindChildFormErrorMsg,细节不知道。

.PAS:

interface 

uses 
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, 
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, Vcl.ExtCtrls, Vcl.StdCtrls; 

type 
    TForm1 = class(TForm) 
    MainMenu1: TMainMenu; 
    N1: TMenuItem; 
    FormNo11: TMenuItem; 
    FormNo21: TMenuItem; 
    ComboBox1: TComboBox; 
    Panel1: TPanel; 
    procedure FormNo21Click(Sender: TObject); 
    procedure FormNo11Click(Sender: TObject); 
    procedure FormCreate(Sender: TObject); 
    procedure ComboBox1Select(Sender: TObject); 
    private 
    { Private declarations } 
    FChild1: TForm; 
    FChild2: TForm; 
    public 
    { Public declarations } 
    end; 

    TChild1 = class(TForm) 
    constructor Create(AOwner: TComponent); override; 
    end; 

    TChild2 = class(TForm) 
    public 
    constructor Create(AOwner: TComponent); override; 
    end; 

    procedure FillCombobox(AMenuItem: TMenuItem; ACombobox: TCombobox); 

var 
    Form1: TForm1; 

implementation 

{$R *.dfm} 

procedure CreateChildForm(ChildClass: TComponentClass; var Reference); 
Begin 
    Screen.Cursor := crHourGlass; 
    try 
// if (FindChildForm(ChildClass, TObject(Reference))) then 
// begin 
//  (TObject(Reference) as TForm).WindowState := wsNormal; 
//  (TObject(Reference) as TForm).Show; 
// end 
// else 
    Application.CreateForm(ChildClass,Reference); 
    except 
    on e: exception do 
    raise; 
// ErrorMsg('Except/on -> CreateChildForm ' + e.message); 
    end; 
    Screen.Cursor := crDefault; 
end; 

procedure TForm1.ComboBox1Select(Sender: TObject); 
begin 
    TMenuItem(ComboBox1.Items.Objects[ComboBox1.ItemIndex]).Click; 
end; 

procedure FillCombobox(AMenuItem: TMenuItem; ACombobox: TCombobox); 
var 
    I: Integer; 
begin 
    for I := 0 to AMenuItem.Count - 1 do 
    ACombobox.Items.AddObject(AMenuItem.Items[I].Caption, AMenuItem.Items[I]); 
end; 

procedure TForm1.FormCreate(Sender: TObject); 
begin 
    FillCombobox(N1, ComboBox1); 
end; 

procedure TForm1.FormNo11Click(Sender: TObject); 
begin 
    CreateChildForm(TChild1, FChild1); 
end; 

procedure TForm1.FormNo21Click(Sender: TObject); 
begin 
    CreateChildForm(TChild2, FChild2); 
end; 

{ TChild1 } 

constructor TChild1.Create(AOwner: TComponent); 
begin 
    inherited CreateNew(AOwner); 
    FormStyle := fsMDIChild; 
    Caption := 'No. I'; 
end; 

{ TChild2 } 

constructor TChild2.Create(AOwner: TComponent); 
begin 
    inherited CreateNew(AOwner); 
    FormStyle := fsMDIChild; 
    Caption := 'No. II'; 
end; 

end. 

.DFM:

object Form1: TForm1 
    Left = 0 
    Top = 0 
    Caption = 'Form1' 
    ClientHeight = 344 
    ClientWidth = 649 
    Color = clBtnFace 
    Font.Charset = DEFAULT_CHARSET 
    Font.Color = clWindowText 
    Font.Height = -11 
    Font.Name = 'Tahoma' 
    Font.Style = [] 
    FormStyle = fsMDIForm 
    Menu = MainMenu1 
    OldCreateOrder = False 
    OnCreate = FormCreate 
    PixelsPerInch = 96 
    TextHeight = 13 
    object Panel1: TPanel 
    Left = 0 
    Top = 303 
    Width = 649 
    Height = 41 
    Align = alBottom 
    Caption = 'Panel1' 
    TabOrder = 0 
    ExplicitTop = 309 
    object ComboBox1: TComboBox 
     Left = 8 
     Top = 12 
     Width = 145 
     Height = 21 
     AutoCompleteDelay = 2500 
     AutoDropDown = True 
     TabOrder = 0 
     OnSelect = ComboBox1Select 
    end 
    end 
    object MainMenu1: TMainMenu 
    Left = 24 
    Top = 8 
    object N1: TMenuItem 
     Caption = 'Forms' 
     object FormNo11: TMenuItem 
     Caption = 'Form No. 1' 
     OnClick = FormNo11Click 
     end 
     object FormNo21: TMenuItem 
     Caption = 'Form No. 2' 
     OnClick = FormNo21Click 
     end 
    end 
    end 
end 
+0

我通过使用应用程序ActionList组件来匹配Action并用'bvsActionList.Actions [i] .Execute;'Thank you Nil执行来解决问题。 –