Question

I have an UltimateListCtrl with three columns. The first simply shows the index, the second has a Choice widget to choose an action, and the third has some StaticText widgets (parameters), their number and identity depending on the choice in column 2.

When the Choice is changed, I get a CommandEvent about it, but I can't figure out in which cell I am. I need this to change the widgets in column three.

Attached is the relevant code:

def addAction(self, action):
    # set the Choice in the cell
    index = self.list.InsertStringItem(sys.maxint, '')
    self.list.SetStringItem(index, self.columns['#'], str(index))
    self.list.SetStringItem(index, self.columns['Action'], '')
    self.list.SetStringItem(index, self.columns['Parameters'], '')

    item = self.list.GetItem(index, self.columns['Action'])
    choice = wx.Choice(self.list, -1, name=action.name,
             choices=[availableAction.name for availableAction in self.availableActions])
    choice.Bind(wx.EVT_CHOICE, self.onActionChange)
    item.SetWindow(choice, expand=True)
    self.list.SetItem(item)

    # set the third column's widgets
    self.setItemParameters(index, action)


def onActionChange(self, event):
    action = copy.deepcopy(self.availableActionsDict[event.GetString()])
    # This doesn't work because this event doesn't have a GetIndex() function
    self.setItemParameters(event.GetIndex(), action)

As you can see in the code, I'd like to find the index of the changed Choice widget. Does anybody know how to do that? I tried getting the item index by looking at the current selected/focused item in the list but it doesn't corelate to the Choice being changed.

Was it helpful?

Solution

Got it! I keep it as it is, and simply use the SetClientData() to give each Choice widget its place in the list:

def addAction(self, action):
    # set the Choice in the cell
    index = self.list.InsertStringItem(sys.maxint, '')
    self.list.SetStringItem(index, self.columns['#'], str(index))
    self.list.SetStringItem(index, self.columns['Action'], '')
    self.list.SetStringItem(index, self.columns['Parameters'], '')

    item = self.list.GetItem(index, self.columns['Action'])
    choice = wx.Choice(self.list, -1, name=action.name,
             choices=[availableAction.name for availableAction in self.availableActions])
    choice.SetClientData(0, index)
    choice.Bind(wx.EVT_CHOICE, self.onActionChange)
    item.SetWindow(choice, expand=True)
    self.list.SetItem(item)

    # set the third column's widgets
    self.setItemParameters(index, action)


def onActionChange(self, event):
    action = copy.deepcopy(self.availableActionsDict[event.GetString()])
    self.setItemParameters(event.GetEventObject().GetClientData(0), action)

I do need to update this every time the index is changed (like when an item is deleted from the middle of the list), but I can live with that.

Any other solutions will be appreciated

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top