TAction радио елемент

В екшън лентите има компонент TAction.

Този компонент съдържа свойство на име

GroupIndex: Integer;

обаче полето

RadioItem: Boolean;

не е там.

  1. Защо така?
  2. Как мога да направя TAction да бъде поле за отметка?

Ориентацията на действието е ActionMainMenuBar и ActionManager.


person none    schedule 06.10.2011    source източник


Отговори (2)


Въпреки че TAction произлиза от TComponent, действието не е компонент в смисъла на GUI елемент. Той е предназначен да бъде свързан с GUI елемент, който например може да бъде радио бутон, квадратче за отметка или във вашия случай TActionClientItem на TActionMainMenuBar.

Колкото до вашите въпроси:

  1. #P3#
    #P4#
  2. За да покажете елемент от менюто (TActionClientItem) в ActionMainMenuBar с квадратче за отметка:

    • Create an action,
    • Задайте Checked на True,
    • Задайте свойството Category,
    • Плъзнете категорията до ActionMainMenuBar,
    • Et voila.
    • Превключете свойството Checked в манипулатора на събитие OnExecute на действието.

    За да покажете нормално квадратче за отметка, което е свързано с действие в ActionManager: не използвайте ActionMainMenuBar, а ActionToolBar, на който можете да пуснете компонента на полето за отметка по подразбиране.

person NGLN    schedule 06.10.2011

Не съм сигурен дали искате квадратчета за отметка или радио бутони, но и двете са лесни: Например просто VCL приложение със следния 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

с тези манипулатори на събития:

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.

person Uli Gerhardt    schedule 06.10.2011
comment
въпросът е за TActionMainMenuBar, а не за TMainMenu. идеята за поставяне на TCheckBox в TActionMainMenuBar. това обаче е хубав отговор за MainMenu. - person none; 06.10.2011