【问题标题】:Delphi Popup Menu ChecksDelphi 弹出菜单检查
【发布时间】:2008-09-25 16:47:27
【问题描述】:

我在 Delphi 中使用弹出菜单。我想以“无线电组”方式使用它,如果用户选择一个项目,它会被选中,而其他项目不会被选中。我尝试使用 AutoCheck 属性,但这允许检查多个项目。有没有办法将弹出菜单设置为只能检查一项?

【问题讨论】:

    标签: delphi


    【解决方案1】:

    要将弹出(或任何其他)菜单项视为单选组项,请将您希望在单选组中包含的每个项目的“RadioItem”属性设置为 true。

    它不会显示复选标记,而是显示所选项目的项目符号,但它会按照您想要的方式工作,并且视觉提示实际上将符合 Windows 标准。

    【讨论】:

      【解决方案2】:

      Zartog 是对的,但如果您想保留复选框,请将此事件分配给弹出菜单中的每个项目。

      请注意,这段代码看起来有点毛茸茸,因为它不依赖于知道弹出菜单的名称(因此,使用“GetParentComponent”查找它)。

      procedure TForm2.OnPopupItemClick(Sender: TObject);
      var
        i : integer;
      begin
        with (Sender as TMenuItem) do begin
          //if they just checked something...
          if Checked then begin
            //go through the list and *un* check everything *else*
            for i := 0 to (GetParentComponent as TPopupMenu).Items.Count - 1 do begin
              if i <> MenuIndex then begin  //don't uncheck the one they just clicked!
                (GetParentComponent as TPopupMenu).Items[i].Checked := False;
              end;  //if not the one they just clicked
            end;  //for each item in the popup
          end;  //if we checked something
        end;  //with
      end;
      

      您可以在运行时将事件分配给表单上的每个弹出框,如下所示(如果您想这样做):

      procedure TForm2.FormCreate(Sender: TObject);
      var
        i,j: integer;
      begin
        inherited;
      
        //look for any popup menus, and assign our custom checkbox handler to them
        if Sender is TForm then begin
          with (Sender as TForm) do begin
            for i := 0 to ComponentCount - 1 do begin
              if (Components[i] is TPopupMenu) then begin
                for j := 0 to (Components[i] as TPopupMenu).Items.Count - 1 do begin
                  (Components[i] as TPopupMenu).Items[j].OnClick := OnPopupItemClick;
                end;  //for every item in the popup list we found
              end;  //if we found a popup list
            end;  //for every component on the form
          end;  //with the form
        end;  //if we are looking at a form
      end;
      

      响应此答案下方的评论:如果您想要求至少检查一个项目,请使用它而不是第一个代码块。您可能想在 oncreate 事件中设置一个默认选中项。

      procedure TForm2.OnPopupItemClick(Sender: TObject);
      var
        i : integer;
      begin
        with (Sender as TMenuItem) do begin
          //go through the list and make sure *only* the clicked item is checked
          for i := 0 to (GetParentComponent as TPopupMenu).Items.Count - 1 do begin
            (GetParentComponent as TPopupMenu).Items[i].Checked := (i = MenuIndex);
          end;  //for each item in the popup
        end;  //with
      end;
      

      【讨论】:

      • 他还应该添加代码来检查至少一个菜单项是否被选中,并添加 check back 用户取消选中已选中的条目。鉴于您的示例,这应该很简单。
      • 我不确定这是他的目标,但我在答案中添加了一个块来处理它。它实际上简化了一些事情。
      • 这绝对不是要走的路:这个功能已经内置了。查看 Zartog 和 Gerry 的帖子,并将两者结合起来。
      【解决方案3】:

      放大 Zartog 的帖子:Delphi 中的弹出菜单(至少从 D6 开始)有一个 GroupIndex 属性,它允许您在一个菜单中拥有多组单选项目。将第一组的 GroupIndex 设置为 1,第二组设置为 2,以此类推。

      所以: 设置自动检查 = True 设置 RadioItem = True 如果您需要多组单选项目,请设置 GroupIndex

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-02-28
        • 2011-03-08
        • 1970-01-01
        • 2011-06-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多