Programmatic selection in wxListCtrl

I am writing a wxListCtrl subclass with helper methods for unit testing GUI
that use list controls.

Right now, I need methods to programmatically:

1. set the selected item in the list

2. query the list for its selected item

I have gotten 2. to work, but 1. doesn't. In the code below, when I invoke
DoSelectItem(), both SelectedItem() and DoSelectItem() print -1. Yet, when I
manually click on an item in the list, SelectedItem() prints the correct
item index.

What am I doing wrong?

Thx.

···

----------

class wxListCtrlWithHelpers(wxListCtrl):

    def SelectedItem(self):
       selected = self.GetNextItem(-1, wxLIST_NEXT_ALL,
wxLIST_STATE_SELECTED)
       print '-- SelectedItem: returning %s' % selected
       return selected
       
    def DoSelectItem(self, row, column):
       self.SetItemState(row, column, wxLIST_STATE_SELECTED)
       print '-- DoSelectedItem: after selecting row=%s, column=%s, selected
item is: %s' % \
             (row, column, self.SelectedItem())

Alain Désilets, MASc
Agent de recherches/Research Officer
Institut de technologie de l'information du CNRC /
NRC Institute for Information Technology

alain.desilets@nrc-cnrc.gc.ca
Tél/Tel (613) 990-2813
Facsimile/télécopieur: (613) 952-7151

Conseil national de recherches Canada, M50, 1200 chemin Montréal,
Ottawa (Ontario) K1A 0R6
National Research Council Canada, M50, 1200 Montreal Rd., Ottawa, ON
K1A 0R6

Gouvernement du Canada | Government of Canada

Desilets, Alain wrote:

I am writing a wxListCtrl subclass with helper methods for unit testing GUI
that use list controls.

Right now, I need methods to programmatically:

1. set the selected item in the list

2. query the list for its selected item

wx.ListCtrl already has some helper methods for doing things like this. I've copied them below.

I have gotten 2. to work, but 1. doesn't. In the code below, when I invoke
DoSelectItem(), both SelectedItem() and DoSelectItem() print -1. Yet, when I
manually click on an item in the list, SelectedItem() prints the correct
item index.

What am I doing wrong?

You aren't using the second parameter of SetItemState correctly.

     def Select(self, idx, on=1):
         '''[de]select an item'''
         if on: state = wx.LIST_STATE_SELECTED
         else: state = 0
         self.SetItemState(idx, state, wx.LIST_STATE_SELECTED)

     def Focus(self, idx):
         '''Focus and show the given item'''
         self.SetItemState(idx, wx.LIST_STATE_FOCUSED, wx.LIST_STATE_FOCUSED)
         self.EnsureVisible(idx)

     def GetFocusedItem(self):
         '''get the currently focused item or -1 if none'''
         return self.GetNextItem(-1, wx.LIST_NEXT_ALL, wx.LIST_STATE_FOCUSED)

     def GetFirstSelected(self, *args):
         '''return first selected item, or -1 when none'''
         return self.GetNextSelected(-1)

     def GetNextSelected(self, item):
         '''return subsequent selected items, or -1 when no more'''
         return self.GetNextItem(item, wx.LIST_NEXT_ALL, wx.LIST_STATE_SELECTED)

     def IsSelected(self, idx):
         '''return True if the item is selected'''
         return self.GetItemState(idx, wx.LIST_STATE_SELECTED) != 0

     def SetColumnImage(self, col, image):
         item = self.GetColumn(col)
         # preserve all other attributes too
         item.SetMask( wx.LIST_MASK_STATE |
                       wx.LIST_MASK_TEXT |
                       wx.LIST_MASK_IMAGE |
                       wx.LIST_MASK_DATA |
                       wx.LIST_SET_ITEM |
                       wx.LIST_MASK_WIDTH |
                       wx.LIST_MASK_FORMAT )
         item.SetImage(image)
         self.SetColumn(col, item)

     def ClearColumnImage(self, col):
         self.SetColumnImage(col, -1)

     def Append(self, entry):
         '''Append an item to the list control. The entry parameter should be a
            sequence with an item for each column'''
         if len(entry):
             if wx.USE_UNICODE:
                 cvtfunc = unicode
             else:
                 cvtfunc = str
             pos = self.GetItemCount()
             self.InsertStringItem(pos, cvtfunc(entry[0]))
             for i in range(1, len(entry)):
                 self.SetStringItem(pos, i, cvtfunc(entry[i]))
             return pos

···

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