VCL CheckListBox传递所选项目并删除它

问题描述 投票:1回答:1

大家好,我是Delphi的初学者,我有一个简单的问题,因为在网上为Delphi寻找东西似乎很困难。

我有一个CheckListBox,当我单击某个项目的复选框时,我想将其传递给另一个表单,可以说创建了form2。那里有2个按钮:

  1. Delete-它应删除选定的Item,以便在其上生成表单。如何删除所选项目?

  2. Edit Entry-我需要将选中的Item值传递给另一种表单,在该表单中我可以编辑值并将其另存为新表单。如何将所选项目中的值传递给另一种形式?

Ty褪色。

delphi vlc
1个回答
5
投票

这是一种更好的方法:

  1. 创建新的VCL应用程序。

  2. 添加带有一些项目的TCheckListBox控件:

    Screenshot of form designer. A single TCheckListBox control is present on the form.

  3. TActionList组件拖放到窗体上。

  4. 创建两个动作:aDeleteaRename。将其字幕设置为Delete...Rename...,将其提示设置为Removes the selected item from the list.Renames the selected item in the list.

    Screenshot of the action list editor.

  5. 将以下代码添加到其OnExecute处理程序中:

    procedure TForm1.aDeleteExecute(Sender: TObject);
    begin
    
      if CheckListBox1.ItemIndex = -1 then
        Exit;
    
      if MessageBox(Handle, 'Do you want to delete the selected item?',
        'Delete', MB_ICONQUESTION or MB_YESNO) <> IDYES then
        Exit;
    
      CheckListBox1.DeleteSelected;
    
    end;
    
    procedure TForm1.aRenameExecute(Sender: TObject);
    var
      S: string;
    begin
    
      if CheckListBox1.ItemIndex = -1 then
        Exit;
    
      S := CheckListBox1.Items[CheckListBox1.ItemIndex];
      if InputQuery('Rename', 'Please enter the new name:', S) then
        CheckListBox1.Items[CheckListBox1.ItemIndex] := S;
    
    end;
    
  6. 将以下代码添加到其OnUpdate处理程序中:

    procedure TForm1.aDeleteUpdate(Sender: TObject);
    begin
      aDelete.Enabled := CheckListBox1.ItemIndex <> -1;
    end;
    
    procedure TForm1.aRenameUpdate(Sender: TObject);
    begin
      aRename.Enabled := CheckListBox1.ItemIndex <> -1;
    end;
    
  7. TPopupMenu拖放到表格上。将其命名为pmList。添加两个菜单项。将它们的Action属性分别设置为aDeleteaRename。这将自动为项目提供标题,提示和操作的热键:

    Screenshot of the popup menu editor.

  8. 现在将pmList分配给复选框控件的PopupMenu属性。

  9. 测试应用程序。请注意,只有在选择了一个菜单项后,上下文菜单项才会启用。否则,它们都将被禁用。 (这要感谢OnUpdate处理程序。跳过这些将非常草率。但是请注意,我们仍然会验证OnExceute处理程序中是否选择了一个项目。在高质量软件中,您始终同时使用Belt和大括号。)

    Screenshot of the application running, with the check list box's context menu visible.

  10. 当然,我们必须将DelF2键映射到删除和重命名操作。我们可以使用动作的ShortCut属性,但这将使这些键在此列表中删除并重命名,即使另一个GUI控件具有焦点,也很糟糕。相反,我们将OnKeyDown处理程序添加到复选框列表控件本身:

    procedure TForm1.CheckListBox1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    begin
      case Key of
        VK_DELETE:
          aDelete.Execute;
        VK_F2:
          aRename.Execute;
      end;
    end;
    
© www.soinside.com 2019 - 2024. All rights reserved.