I'm using C Builder XE6 to write a VCL application that contains a TListView
with an associated TPopupMenu
context menu.
The TListView
has ViewStyle=vsReport
and ReadOnly=false
. The associated context menu contains a 'Delete' menu item with ShortCut=Del
.
The 'Delete' menu item has the following OnClick
event handler:
void __fastcall TForm1::Delete1Click (TObject *Sender)
{
ListView1->DeleteSelected();
}
The 'Delete' menu item is enabled/disabled in the OnPopup
event handler of the context menu:
void __fastcall TForm1::PopupMenu1Popup (TObject *Sender)
{
Delete1->Enabled = (ListView1->SelCount > 0);
}
When I select an item in the list view and press the Del key on the keyboard, the Delete1Click()
event handler is invoked and the item is deleted, as expected.
However, if the list view item is in edit mode and I press the Del key, the Delete1Click()
event handler is also invoked, deleting the item. The keypress is not sent to the edit box in the list view.
I tried to fix this by modifying the OnPopup
event handler of the context menu as follows:
void __fastcall TForm1::PopupMenu1Popup (TObject *Sender)
{
Delete1->Enabled = (ListView1->SelCount > 0 && !ListView1->IsEditing());
}
This prevents the deletion of the list view item, but the keypress is still not sent to the edit box in the list view.
When the list view item is in edit mode, the Del keypress should be handled by the edit box of the list view instead of invoking the Delete1Click()
event handler.
How do I achieve this?
I don't want to change the shortcut for the 'Delete' menu item, to something like Ctrl Del.
CodePudding user response:
To overcome your problem you need to clear the popup menu item's shortcut when editing a list item by adding an OnEditing and an OnEdited event handler.
void __fastcall TForm3::ListView1Editing(TObject *Sender, TListItem *Item, bool &AllowEdit)
{
Delete1->ShortCut = NULL;
}
Pressing the keyboard Del key will act as it should while you are editing, when finished editing you can reinstate the shortcut like this
void __fastcall TForm3::ListView1Edited(TObject *Sender, TListItem *Item, UnicodeString &S)
{
Delete1->ShortCut = TextToShortCut("Del") ;
}
Adding these two event handlers work fine in XE7 so you should have no problem with XE6.
You could also, while you are at it, store the menu items shortcut in a TShortCut then restore from that for each menu item with a shortcut that you are having issues with.
CodePudding user response:
Just a thought, you should be able to set a flag 'is_editing = true' when you select the ListView1 Item and use the flag to skip ListView1->DeleteSelected(); with something like
void __fastcall TForm1::Delete1Click (TObject *Sender)
{
if(!ListView1->Selected->Focused ) //revised
{
ListView1->DeleteSelected();
}
}