大家好,我是Delphi的初学者,我有一个简单的问题,因为在网上为Delphi寻找东西似乎很困难。
我有一个CheckListBox,当我单击某个项目的复选框时,我想将其传递给另一个表单,可以说创建了form2。那里有2个按钮:
Delete-它应删除选定的Item,以便在其上生成表单。如何删除所选项目?
Edit Entry-我需要将选中的Item值传递给另一种表单,在该表单中我可以编辑值并将其另存为新表单。如何将所选项目中的值传递给另一种形式?
Ty褪色。
这是一种更好的方法:
创建新的VCL应用程序。
添加带有一些项目的TCheckListBox
控件:
将TActionList
组件拖放到窗体上。
创建两个动作:aDelete
和aRename
。将其字幕设置为Delete...
和Rename...
,将其提示设置为Removes the selected item from the list.
和Renames the selected item in the list.
。
将以下代码添加到其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;
将以下代码添加到其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;
将TPopupMenu
拖放到表格上。将其命名为pmList
。添加两个菜单项。将它们的Action
属性分别设置为aDelete
和aRename
。这将自动为项目提供标题,提示和操作的热键:
现在将pmList
分配给复选框控件的PopupMenu
属性。
测试应用程序。请注意,只有在选择了一个菜单项后,上下文菜单项才会启用。否则,它们都将被禁用。 (这要感谢OnUpdate
处理程序。跳过这些将非常草率。但是请注意,我们仍然会验证OnExceute
处理程序中是否选择了一个项目。在高质量软件中,您始终同时使用Belt和大括号。)
当然,我们必须将Del和F2键映射到删除和重命名操作。我们可以使用动作的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;