Hi there
I noticed that ListCtrl.DeleteItem
triggers a focus event with the wrong item.
This issue is similar to: ListCtrl.GetSelectedItemCount() giving wrong result after DeleteItem()?.
But the current problem occurs with wx 4.2.1 on Windows 10.
Here is a code example that demonstrates the problem.
import wx
class CheckList(wx.ListCtrl):
def __init__(self, *args, **kwargs):
wx.ListCtrl.__init__(self, *args,
style=wx.LC_REPORT|wx.LC_HRULES, **kwargs)
self.EnableCheckBoxes()
self.AppendColumn("A")
for i in range(6):
self.InsertItem(i, "Item %d" % i)
self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
self.Bind(wx.EVT_LIST_DELETE_ITEM, self.OnDeleteItem)
self.Bind(wx.EVT_LIST_ITEM_FOCUSED, self.OnItemFocused)
self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected)
self.Bind(wx.EVT_LIST_ITEM_DESELECTED, self.OnItemDeselected)
def OnKeyDown(self, evt):
key = evt.GetKeyCode()
j = self.FocusedItem
if j != -1 and key == wx.WXK_DELETE:
print("\nDeleting index", j)
## self.Select(j, False) # no effect
self.DeleteItem(j)
print(f"{self.FocusedItem = } is correct.")
evt.Skip()
def OnDeleteItem(self, evt):
print("Delete index", evt.Index)
evt.Skip()
def OnItemFocused(self, evt):
print("Focused index", evt.Index)
evt.Skip()
def OnItemSelected(self, evt):
print("Selected index", evt.Index,
"selection", self.SelectedItemCount,
"of", self.ItemCount)
evt.Skip()
def OnItemDeselected(self, evt):
print("Deselected index", evt.Index)
evt.Skip()
if __name__ == "__main__":
app = wx.App()
frm = wx.Frame(None)
frm.lc = CheckList(frm)
frm.Show()
app.MainLoop()
Do the following steps:
- Click on Item 0
- Press [delete]
Then, it produces the following output:
Focused index 0
Selected index 0 selection 1 of 6
Deleting index 0
Deselected index 0
Focused index 1
Delete index 0
self.FocusedItem = 0 is correct.
I think the focus event should be triggered after deletion, not during deletion.
I will report these to the issue tracker, but I want to hear your opinions before that.
Thoughts?