アクション バンドには TAction コンポーネントがあります。
そのコンポーネントは、という名前のプロパティを保持しています
GroupIndex: Integer;
しかし、フィールド
RadioItem: Boolean;
そこにいない。
- 何故ですか?
- TAction をチェックボックスにするにはどうすればよいですか?
アクションの向きは、ActionMainMenuBar と ActionManager です。
アクション バンドには TAction コンポーネントがあります。
そのコンポーネントは、という名前のプロパティを保持しています
GroupIndex: Integer;
しかし、フィールド
RadioItem: Boolean;
そこにいない。
アクションの向きは、ActionMainMenuBar と ActionManager です。
TAction は TComponent から派生していますが、アクションは GUI 要素という意味でのコンポーネントではありません。これは、たとえばラジオ ボタン、チェックボックス、またはあなたの場合は TActionMainMenuBar の TActionClientItem などの GUI 要素にリンクすることを意図しています。
あなたの質問について:
アクションのGroupIndex
プロパティは、アクションがラジオ アイテムのように動作するかどうかを示します。ヘルプには次のように書かれています。
GroupIndex は、ラジオ ボタンのように機能するアクションのグループを定義するために使用されます。GroupIndex が 0 より大きい場合、アクションが属するグループを識別します。そのグループ内のいずれかのアクションの Checked プロパティが true に設定されている場合、グループ内の他のすべてのアクションの Checked プロパティは false に設定されます。つまり、一度にチェックできるのはグループ内の 1 つのアクションだけです。注: グループ内のすべてのアクションは、同じアクション リストまたはアクション マネージャーによってリストされている必要があります。
チェックボックス付きの ActionMainMenuBar にメニュー項目 (TActionClientItem) を表示するには:
Checked
真に設定し、Category
プロパティを設定し、Checked
アクションの OnExecute イベント ハンドラーでプロパティを切り替えます。ActionManager のアクションにリンクされている通常のチェックボックスを表示するには、ActionMainMenuBar を使用しないでください。ただし、デフォルトのチェックボックス コンポーネントをドロップできる ActionToolBar を使用します。
チェックボックスとラジオ ボタンのどちらが必要かはわかりませんが、どちらも簡単です: たとえば、次の Form1.dfm を使用した単純な VCL アプリ:
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 251
ClientWidth = 588
Color = clBtnFace
ParentFont = True
Menu = MainMenu1
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object CheckBox1: TCheckBox
Left = 216
Top = 32
Width = 97
Height = 17
Action = Action1
TabOrder = 0
end
object RadioGroup1: TRadioGroup
Left = 336
Top = 32
Width = 185
Height = 121
Caption = 'RadioGroup1'
Items.Strings = (
'1'
'2'
'3')
TabOrder = 1
end
object ActionList1: TActionList
Left = 184
Top = 120
object Action1: TAction
Caption = 'Action1'
OnExecute = Action1Execute
end
object Action2: TAction
Caption = 'Action2'
GroupIndex = 1
OnExecute = Action1Execute
end
object Action3: TAction
Caption = 'Action3'
GroupIndex = 1
OnExecute = Action1Execute
end
object Action4: TAction
Caption = 'Action4'
GroupIndex = 1
OnExecute = Action1Execute
end
end
object MainMenu1: TMainMenu
Left = 224
Top = 120
object miTest: TMenuItem
Caption = 'Test'
object miAction1: TMenuItem
Action = Action1
AutoCheck = True
end
object miSep: TMenuItem
Caption = '-'
end
object miAction2: TMenuItem
Action = Action2
AutoCheck = True
GroupIndex = 1
RadioItem = True
end
object miAction3: TMenuItem
Action = Action3
AutoCheck = True
GroupIndex = 1
RadioItem = True
end
object miAction4: TMenuItem
Action = Action4
AutoCheck = True
GroupIndex = 1
RadioItem = True
end
end
end
end
これらのイベント ハンドラーで:
procedure TForm1.Action1Execute(Sender: TObject);
var
actn: TAction absolute Sender;
begin
Assert(Sender is TAction);
actn.Checked := not actn.Checked;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
begin
// Hardcoded association for test purposes:
for i := 0 to Pred(RadioGroup1.ControlCount) do
RadioGroup1.Controls[i].Action := ActionList1.Actions[i + 1];
end;
私に期待されるように動作します。
アクションをメニューのラジオ項目のように見せるには、アクションではなくメニュー項目に RadioItem を設定する必要があります。GroupIndex が <> 0 の場合、これがデフォルトではない理由がわかりません。
更新: ActionManager のものは、古き良き ActionLists よりもトリッキーです。このDFM
object Form1: TForm1
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 271
ClientWidth = 588
Color = clBtnFace
ParentFont = True
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object CheckBox1: TCheckBox
Left = 200
Top = 48
Width = 97
Height = 17
Action = Action1
TabOrder = 0
end
object RadioGroup1: TRadioGroup
Left = 336
Top = 32
Width = 185
Height = 121
Caption = 'RadioGroup1'
ItemIndex = 1
Items.Strings = (
'1'
'2'
'3')
TabOrder = 1
end
object ActionMainMenuBar1: TActionMainMenuBar
Left = 0
Top = 0
Width = 588
Height = 24
UseSystemFont = False
ActionManager = ActionManager1
Caption = 'ActionMainMenuBar1'
ColorMap.HighlightColor = clWhite
ColorMap.BtnSelectedColor = clBtnFace
ColorMap.UnusedColor = clWhite
ParentFont = True
PersistentHotKeys = True
Spacing = 0
end
object ActionManager1: TActionManager
ActionBars = <
item
Items = <
item
Items = <
item
Action = Action1
Caption = '&Action1'
end
item
Caption = '-'
end
item
Action = Action2
Caption = 'A&ction2'
end
item
Action = Action3
Caption = 'Ac&tion3'
end
item
Action = Action4
Caption = 'Act&ion4'
end>
Caption = 'T&est'
end>
ActionBar = ActionMainMenuBar1
end>
Left = 184
Top = 160
StyleName = 'XP Style'
object Action1: TAction
Category = 'Test'
AutoCheck = True
Caption = 'Action1'
end
object Action2: TAction
Category = 'Test'
AutoCheck = True
Caption = 'Action2'
GroupIndex = 1
end
object Action3: TAction
Category = 'Test'
AutoCheck = True
Caption = 'Action3'
Checked = True
GroupIndex = 1
end
object Action4: TAction
Category = 'Test'
AutoCheck = True
Caption = 'Action4'
GroupIndex = 1
end
end
end
このハンドラーで
procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
begin
for i := 0 to Pred(ActionManager1.ActionCount) do
TAction(ActionManager1.Actions[i]).DisableIfNoHandler := False;
for i := 0 to Pred(RadioGroup1.ControlCount) do
RadioGroup1.Controls[i].Action := ActionManager1.Actions[i + 1];
end;
動作します。ただし、AutoCheck を使用しないとラジオ アイテムを動作させることができません。