4

いくつかの項目を含むポップアップ メニューがあります。

オプション1
 - サブ1
 - サブ2
オプション 2
 - サブ1
 - サブ2
オプション 3
 - サブ1
 - サブ2

サブサブメニューをオプション 3 sub2 に追加できるようにしたいので、次のようになります。

オプション1
 - サブ1
 - サブ2
オプション 2
 - サブ1
 - サブ2
オプション 3
 - サブ1
 - サブ2
   - ダイナミックアイテム1
   - 動的項目 2
   - ダイナミックアイテム3

私はそれを理解するために次のコードを操作しようとしましたが、「IF」メニュー オプションがまだ存在しない場合 (オプション 3) は実行できますが、オプション 3 が既に存在する場合は実行できません (重複を作成します)。メニューオプション)

どうすればいいですか?

ありがとうございました


unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, StdCtrls;

type
  TForm2 = class(TForm)
    popMenu: TPopupMenu;
    gbDynamic: TGroupBox;
    gbMain: TGroupBox;
    popDynamic: TPopupMenu;
    Option11: TMenuItem;
    Option21: TMenuItem;
    Option31: TMenuItem;
    Sub11: TMenuItem;
    Sub21: TMenuItem;
    Item11: TMenuItem;
    Item21: TMenuItem;
    Item31: TMenuItem;
    Item41: TMenuItem;
    Sub12: TMenuItem;
    Sub22: TMenuItem;
    Sub13: TMenuItem;
    Sub23: TMenuItem;
    Option12: TMenuItem;
    Sub24: TMenuItem;
    Sub14: TMenuItem;
    Option22: TMenuItem;
    Sub25: TMenuItem;
    Sub15: TMenuItem;
    Option32: TMenuItem;
    Sub26: TMenuItem;
    Sub16: TMenuItem;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    Procedure PopupItemClick ( Sender : TObject );
  end;

var
  Form2: TForm2;
  stlist: TStrings;

implementation

{$R *.dfm}

procedure TForm2.FormCreate(Sender: TObject);
Var
     menuItem,SubItem : TMenuItem;
     I, N : Integer;
     s : String;

begin
//This will be a downloaded list from a server.
   stlist := TStringList.Create;
     stlist.Add ( 'Item 1' );
     stlist.Add ( 'Item 2' );
     stlist.Add ( 'Item 3' );

     SubItem := TMenuItem.Create(popDynamic);
     SubItem.Caption := 'Option 3';
     popDynamic.Items.Add(SubItem);

     MenuItem := TMenuItem.Create(popDynamic);
     MenuItem.Caption := 'Sub 1';
     SubItem.Add(MenuItem);
     //This would work if Option 3 menu item was not aleady a menu item.

       For I := 0 To stlist.Count - 1 Do
          Begin
          SubItem := TMenuItem.Create ( popDynamic );
          SubItem.Caption := stlist [i];
          SubItem.OnClick := PopupItemClick;
          //assign it a custom integer value..
          SubItem.Tag := i;
          MenuItem.Add(SubItem);
          End;
end;
Procedure TForm2.PopupItemClick ( Sender : TObject );
Var
     menuItem : TMenuItem;
Begin
     menuItem := TMenuItem ( sender );
     label1.caption := stlist.Strings [menuItem.Tag];
End;
end.

形:

object Form2: TForm2
  Left = 0
  Top = 0
  Caption = 'Form2'
  ClientHeight = 247
  ClientWidth = 480
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object gbDynamic: TGroupBox
    Left = 0
    Top = 0
    Width = 217
    Height = 247
    Align = alLeft
    Caption = 'Dynamic'
    PopupMenu = popDynamic
    TabOrder = 0
    object Label1: TLabel
      Left = 48
      Top = 32
      Width = 31
      Height = 13
      Caption = 'Label1'
    end
  end
  object gbMain: TGroupBox
    Left = 217
    Top = 0
    Width = 263
    Height = 247
    Align = alClient
    Caption = 'What I Want Dynamically Created'
    PopupMenu = popMenu
    TabOrder = 1
    ExplicitLeft = 336
    ExplicitTop = 72
    ExplicitWidth = 185
    ExplicitHeight = 105
  end
  object popDynamic: TPopupMenu
    Left = 136
    Top = 96
    object Option12: TMenuItem
      Caption = 'Option 1'
      object Sub14: TMenuItem
        Caption = 'Sub 1'
      end
      object Sub24: TMenuItem
        Caption = 'Sub 2'
      end
    end
    object Option22: TMenuItem
      Caption = 'Option 2'
      object Sub15: TMenuItem
        Caption = 'Sub 1'
      end
      object Sub25: TMenuItem
        Caption = 'Sub 2'
      end
    end
    object Option32: TMenuItem
      Caption = 'Option 3'
      object Sub16: TMenuItem
        Caption = 'Sub 1'
      end
      object Sub26: TMenuItem
        Caption = 'Sub 2'
      end
    end
  end
  object popMenu: TPopupMenu
    Left = 256
    Top = 112
    object Option11: TMenuItem
      Caption = 'Option 1'
      object Sub13: TMenuItem
        Caption = 'Sub 1'
      end
      object Sub23: TMenuItem
        Caption = 'Sub 2'
      end
    end
    object Option21: TMenuItem
      Caption = 'Option 2'
      object Sub12: TMenuItem
        Caption = 'Sub 1'
      end
      object Sub22: TMenuItem
        Caption = 'Sub 2'
      end
    end
    object Option31: TMenuItem
      Caption = 'Option 3'
      object Sub11: TMenuItem
        Caption = 'Sub 1'
        object Item11: TMenuItem
          Caption = 'Item 1'
        end
        object Item21: TMenuItem
          Caption = 'Item 2'
        end
        object Item31: TMenuItem
          Caption = 'Item 3'
        end
        object Item41: TMenuItem
          Caption = 'Item 4'
        end
      end
      object Sub21: TMenuItem
        Caption = 'Sub 2'
      end
    end
  end
end

4

2 に答える 2

3

これを行う最も簡単な方法は、Option3 に名前を付けて、ループで確認することです。

 SubItem := nil;
 for i := 0 to popdynamic.Items.Count-1 do
   if SameText(popdynamic.Items[i].Name, 'mnuOption3') then
     SubItem := popdynamic.Items[i];

 if SubItem = nil then
   begin
     SubItem := TMenuItem.Create(popDynamic);
     SubItem.Caption := 'Option 3';
     Subitem.Name := 'mnuOption3';
     popDynamic.Items.Add(SubItem);
   end;

このようにして、すでに存在する場合、新しいサブアイテムを作成しません。

于 2010-02-26T23:02:31.153 に答える
1

新しいサブメニューを作成するために必要なことは、既存のメニュー項目への参照を取得することです。毎回同じである場合は、オブジェクトを直接参照するだけです。それ以外の場合は、何らかの方法でそれを見つけてから、必要な TMenuItem を使用して Add を呼び出します。たす。私のアプリの 1 つで似たようなものを使用して、XML ファイルからメニューを再帰的に作成しています。

これは、私がやっていることの非常に凝縮されたバージョンとして役立つはずです。

procedure TMainForm.AddMenuItems(XMLNode: IXMLDOMNode; Parent: TMenuItem);
var
  node: IXMLNode;
  item: TMenuItem;
begin
  for i := 0 to XMLNode.childNodes.length - 1 do begin
    node := XMLNode.childNodes.item[i];
    item := NewItem(node.attributes.getNamedItem('name').nodeValue, 0, false, true, nil, 0, '');
    if node.nodeName = 'group' then begin
      Parent.Add(item);
      AddMenuItems(node, item);
    end else begin
      //You probably want to add a Tag to your items so you can tell them apart.
      item.OnClick := DynamicItemClick;
      Parent.Add(item);
    end;
  end;
end;

procedure TMainForm.LoadData;
var
  XMLNode: IXMLNode;
  //...
begin
  //Clear old dynamic items
  while Set1.Count > 0 do
    Set1.Items[0].Free;
  //Open XML file, find right XMLNode...
  AddMenuItems(XMLNode.firstChild, Set1, '');
end;

...ここで、Set1 はメニュー項目を作成するメニュー項目です。あなたの場合、それはSub26(およびポップアップメニューのSub23)になると思います。この基本的なアプローチを使用して、XML をファイルの読み取りに必要なものに置き換えます。

于 2010-02-27T02:00:54.620 に答える