Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi弹出菜单检查_Delphi - Fatal编程技术网

Delphi弹出菜单检查

Delphi弹出菜单检查,delphi,Delphi,我使用的是Delphi中的弹出菜单。我想以“无线组”的方式使用它,如果用户选择一个项目,它将被选中,而其他项目则不会被选中。我尝试使用AutoCheck属性,但这允许检查多个项目。有没有办法设置弹出菜单,以便只选中一个项目?Zartog是正确的,但是如果您想保留复选框,请将此事件分配给弹出菜单中的每个项目 注意,这段代码看起来有点毛茸茸的,因为它不依赖于知道弹出菜单的名称(因此,使用“GetParentComponent”查找它) 回答下面的一条评论:如果您希望至少检查一项,请使用此代码块而不是

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

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

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

回答下面的一条评论:如果您希望至少检查一项,请使用此代码块而不是第一个代码块。您可能希望在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;
要将弹出菜单项(或任何其他)与无线电组项一样处理,请将无线电组中要包含的每个项的“RadioItem”属性设置为true


它不会显示复选标记,而是按所选项目显示一个项目符号,但它会按照您想要的方式工作,并且视觉提示实际上会与windows标准相匹配。

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

因此: 设置自动检查=真 设置为True
设置GroupIndex如果您需要多组无线电项目

他还应添加代码,检查是否至少选中了一个菜单项,以及是否添加了回签用户取消选中的条目。考虑到你的例子,这应该很简单。我不确定这是他的目标,但我在答案中添加了一个块来处理这个问题。它实际上简化了一些事情。这绝对不是我们要走的路:这个功能已经内置了。查看Zartog和Gerry的帖子,并将两者结合起来。
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;
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;