ListCtrl problem

Hi all,

I have to manually recreate the ListCtrl after selection, but the following code doesn't work (no selection is made).

What do I do wrong?

Thanks,
Igor

import wx

class WinList(wx.Frame):
     def __init__(self,parent,id,title):
         wx.Frame.__init__(self,parent,id,title)
         self.lista = wx.ListCtrl(self,-1,style=wx.LC_REPORT|wx.LC_SINGLE_SEL)
         self.lista.InsertColumn(0,"ID")
         self.lista.InsertColumn(1,"Summary")
         self.populateList()
         self.lista.Bind(wx.EVT_LIST_ITEM_SELECTED,self.onItemSelected)
         self.Show(True)

     def populateList(self):
         self.lista.InsertStringItem(0,"1")
         self.lista.SetStringItem(0,1,"First item")
         self.lista.InsertStringItem(1,"2")
         self.lista.SetStringItem(1,1,"Second item")

     def onItemSelected(self,e):
         if self.lista.GetSelectedItemCount() == 1:
             self.refreshGui(self.lista.GetFirstSelected())

     def refreshGui(self,id):
         self.lista.DeleteAllItems()
         self.populateList()
         self.lista.SetEvtHandlerEnabled(False) #avoid infinite loop
         self.lista.Select(id)
         self.lista.SetEvtHandlerEnabled(True)

if __name__ == "__main__":
     app = wx.App()
     frame = WinList(None,-1,"ListCtrl problem")
     app.MainLoop()

···

--
Igor Jese, igor@jeseonline.com
http://mockupscreens.com
igor@mockupscreens.com

Igor Jese wrote:

Hi all,

I have to manually recreate the ListCtrl after selection, but the following code doesn't work (no selection is made).

What do I do wrong?

IIRC, there is some internal cleanup that the control does after deleting all items. That is probably also clearing the selection. So if you delay your reselection for a short time then it should work fine for you, something like this:

     def refreshGui(self, id):
         self.lista.DeleteAllItems()
         self.populateList()
         wx.CallLater(100, self.afterRefresh, id)

     def afterRefresh(self, id):
         self.lista.SetEvtHandlerEnabled(False) #avoid infinite loop
         self.lista.Select(id)
         self.lista.SetEvtHandlerEnabled(True)

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Thanks Robin. This works, but only when set for 150ms or more.

Is there a way to make sure I'm on the safe side? I can't just increase the delay in ms. I tried to do the same with CallAfter, but it won't work either.

···

On Fri, 19 Jan 2007 19:53:09 +0100, Robin Dunn <robin@alldunn.com> wrote:

IIRC, there is some internal cleanup that the control does after deleting all items. That is probably also clearing the selection. So if you delay your reselection for a short time then it should work fine for you, something like this:
     def refreshGui(self, id):
        self.lista.DeleteAllItems()
        self.populateList()
        wx.CallLater(100, self.afterRefresh, id)

Thanks again,
Igor
--
Igor Jese, igor@jeseonline.com

igor@mockupscreens.com