Sascha
Sascha

Reputation: 11

ListCtrl Hack: Set a ListItems Text in a ListCtrl

I am attempting to set an item's text contents in a ListCtrl. The problem is it doesn't work.

I use this function: _listItem.SetText( "Blah" ) but the text does not change? I have also tried the function SetItemText() but that doesn't work either.

Some important information (that maybe helpful):

My code:

class AddStockListCtrl( ListCtrl, listmix.TextEditMixin ):
    """ """

    # Class Variables:

    # self.parent
    # self.type_cell
    # self.type_cb


    # Class Functions:

    def __init__( self, _parent ):
        """ Constructor """

        ListCtrl.__init__( self, parent=_parent, id=wx.NewId(), style=wx.LC_EDIT_LABELS|wx.LC_REPORT )

        AddStockListCtrl.def_data = ('ABC', "Registered",
                                     '0.00', '500')  # wx.lib.masked.NumCtrl( self, value="0.00" ), wx.lib.masked.NumCtrl( self, value="0.00" ) )
        listmix.TextEditMixin.__init__(self)

        self.parent    = _parent
        self.type_cb   = wx.ComboBox( self, choices=('Registered', 'Tracking'))
        self.type_cell = None

        self.InsertColumn( 0, heading="Code", width=40 )
        self.InsertColumn( 1, heading="Type", width=50 )
        self.InsertColumn( 2, heading="Purchase Price", width=100 )
        self.InsertColumn( 3, heading="Purchase Quantity", width=110 )
        self.type_cb.SetSelection(0)
        self.type_cb.Hide()

        self.add_stock_row()

        self.Bind( wx.EVT_LIST_END_LABEL_EDIT,   self.on_end_edit )  # .on_validate_value )
        self.Bind( wx.EVT_LIST_BEGIN_LABEL_EDIT, self.on_begin_edit )


    def on_begin_edit( self, event ):
        """ Post: """

        if event.GetItem().GetColumn() == 1:

            self.type_cell = event.GetItem().GetId()
            item           = self.type_cell
            rect= self.GetItemRect( event.GetItem().GetId() )
            rect.SetLeft ( self.GetColumnWidth(0)+2 )
            rect.SetWidth( self.GetColumnWidth(1)-2 )

            @AfterEx( rect )
            def postedit( rect ):
                self.type_cb.SetRect( rect )
                self.type_cb.SetFocus()
                self.type_cb.Show()
                self.type_cb.Raise()
                #event.Veto()


    def on_end_edit( self, event ):
        """ Post: """

        if self.type_cell != -1:
            sel_type = str(self.type_cb.GetValue())
            print "Doing end edit & type_cell != None"
            print self.GetItemCount()
            print sel_type

            if sel_type == "Registered":

                for row in range(self.GetItemCount()):

                    #self.GetItem(row,2).SetEditable(True) # by the way is there a way to make a ListItem not editable?
                    #self.GetItem(row,3).SetEditable(True)
                    pass

            else: # sel_type == "Tracking"

                for row in range(self.GetItemCount()):

                    p_price = self.GetItem(row,2)
                    p_quant = self.GetItem(row,3)
                    p_price.SetText("0.00")
                    p_quant.SetText("500")
                    #p_price.SetEditable(False)
                    #p_quant.SetEditable(False)

            self.type_cb.Hide()
            #event.GetItem().SetText( sel_type )
            self.SetItemText( self.type_cell, sel_type ) # HERE the text should change but doesn't?!
            event.Veto()
        else:
            print "It == None"

Upvotes: 1

Views: 1569

Answers (1)

RobinDunn
RobinDunn

Reputation: 6206

When you use _listItem.SetText("Blah") you also need to put the item back into the listctrl with something like self.SetItem(_listItem). Or you can use the listctrl's SetItemText method and not have to deal with the wx.ListItem object at all.

Upvotes: 2

Related Questions