TAaction 单选项目

发布于 2024-12-08 15:21:44 字数 273 浏览 0 评论 0原文

在动作带中有一个 TAction 组件。

该组件拥有一个名为

GroupIndex: Integer;

但该字段

RadioItem: Boolean;

不存在的属性。

  1. 这是为什么?
  2. 如何使 TAction 成为复选框?

操作的方向是 ActionMainMenuBar 和 ActionManager。

In action bands there is a TAction component.

That component holds a property named

GroupIndex: Integer;

however the field

RadioItem: Boolean;

is not there.

  1. Why is that?
  2. How can I make a TAction to be a checkbox?

The orientation of the action is ActionMainMenuBar and ActionManager.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七月上 2024-12-15 15:21:44

尽管 TAction 源自 TComponent,但操作并不是 GUI 元素意义上的组件。它旨在链接到 GUI 元素,例如可以是单选按钮、复选框,或者在您的情况下是 TActionMainMenuBar 上的 TActionClientItem。

至于您的问题:

  1. 操作的 GroupIndex 属性指示该操作的行为是否类似于单选项目。帮助说:

    <块引用>

    GroupIndex 用于定义类似于单选按钮的操作组。当GroupIndex大于0时,它标识该操作所属的组。当该组中任何操作的 Checked 属性设置为 true 时,该组中所有其他操作的 Checked 属性都设置为 false。也就是说,一次只能检查组中的一项操作。注意:组中的所有操作必须由同一操作列表或操作管理器列出。

  2. 要在 ActionMainMenuBar 中显示带有复选框的菜单项(TActionClientItem):

    • 创建一个操作,
    • 已检查设置为True,
    • 设置Category 属性,
    • 将类别拖至 ActionMainMenuBar,
    • 瞧。
    • 切换操作的 OnExecute 事件处理程序中的 Checked 属性。

    要显示链接到 ActionManager 中操作的普通复选框:不要使用 ActionMainMenuBar,而是使用可以在其上放置默认复选框组件的 ActionToolBar。

Though TAction descends from TComponent, an action is not a component in the sense of a GUI element. It is meant to be linked to a GUI element, which for instance can be a radio button, a checkbox, or in your case a TActionClientItem on a TActionMainMenuBar.

As for your questions:

  1. The GroupIndex property of an action indicates whether the action behaves like a radio item. The help says:

    GroupIndex is used to define groups of actions that act like radio buttons. When GroupIndex is greater than 0, it identifies the group to which the action belongs. When the Checked property of any action in that group is set to true, the Checked property of all other actions in the group is set to false. That is, only one action in the group can be checked at a time. Note: All actions in a group must be listed by the same action list or action manager.

  2. To show a menu item (a TActionClientItem) in an ActionMainMenuBar with a checkbox:

    • Create an action,
    • Set Checked to True,
    • Set the Category property,
    • Drag the category to the ActionMainMenuBar,
    • Ét voila.
    • Toggle the Checked property in the action's OnExecute event handler.

    To show a normal checkbox which is linked to an action in the ActionManager: do not use an ActionMainMenuBar, but an ActionToolBar on which you can drop the default checkbox component.

毁虫ゝ 2024-12-15 15:21:44

我不确定您是否需要复选框或单选按钮,但两者都很简单:例如,一个带有以下 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,而不是在操作上设置 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,我就无法让无线电项目正常工作。

I'm not sure if you want checkboxes or radio buttons, but both are easy: For example a simple VCL app with the following Form1.dfm:

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

with these event handlers:

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;

works like one would expect for me.

To make the actions look like radio items on the menu, one has to set RadioItem on the menu items, not on the action. I don't know why this is not the default if GroupIndex is <> 0.

Update: The ActionManager stuff is trickier than good old ActionLists. This 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

with this handler

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;

works. However I can't get the radio items to work without using AutoCheck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文