[wxPython] events

Hi,

My intension was to do a validation of particular cells (i.e. columns)
following a TAB or CR and if the data in a cell were incorrect, then to
position the cursor back on the previous cell (i.e. the cell with the
incorrect data). However, the order of the events and the availability
of the data "put the screws to my intensions"!

Pressing a key to enter the first character into a cell invokes the
OnEditShow event. The TAB or CR are captured in the OnKeyDown event
which comes next in time. However, at this time "GetCellValue()" returns
blank meaning the data is not yet available. Next comes OnSelectCell,
then OnEditHidden and then OnCellChange at which time the data in the
cell is at last available. Can anyone tell me how in this event handler
I can force the cursor back to the previous cell. Or is there a better
way to do it?

Larry

Pressing a key to enter the first character into a cell invokes the
OnEditShow event. The TAB or CR are captured in the OnKeyDown event
which comes next in time. However, at this time "GetCellValue()" returns
blank meaning the data is not yet available. Next comes OnSelectCell,
then OnEditHidden and then OnCellChange at which time the data in the
cell is at last available. Can anyone tell me how in this event handler
I can force the cursor back to the previous cell. Or is there a better
way to do it?

You'd think that you could just call SetGridCursor from within the
OnCellChanged, but it doesn't work because it is already nested inside a
SetGridCursor to when the event handler returns the original SetGridCursor
completes and moves the current cell to the new one. To get around this you
can change back to the cell in question in idle time, like this:

    def OnCellChange(self, evt):
        self.log.write("OnCellChange: (%d,%d) %s\n" %
                       (evt.GetRow(), evt.GetCol(), evt.GetPosition()))

        # Show how to stay in a cell that has bad data. We can't just
        # call SetGridCursor here since we are nested inside one so it
        # won't have any effect. Instead, set coordinants to move to in
        # idle time.
        value = self.GetCellValue(evt.GetRow(), evt.GetCol())
        if value == 'no good':
            self.moveTo = evt.GetRow(), evt.GetCol()

    def OnIdle(self, evt):
        if self.moveTo != None:
            self.SetGridCursor(self.moveTo[0], self.moveTo[1])
            self.moveTo = None

Another approach is to force the completion of the edit control (savinf the
data to the table) a little early in the OnSelectCell, and then check it and
if it is bad simply don't call evt.Skip(), like this:

    def OnSelectCell(self, evt):
        self.log.write("OnSelectCell: (%d,%d) %s\n" %
                       (evt.GetRow(), evt.GetCol(), evt.GetPosition()))

        # Another way to stay in a cell that has a bad value...
        row = self.GetGridCursorRow() # current coords
        col = self.GetGridCursorCol()
        if self.IsCellEditControlEnabled():
            self.HideCellEditControl()
            self.DisableCellEditControl()
        value = self.GetCellValue(row, col)
        if value == 'no good 2':
            return # cancels the cell selection
        else:
            evt.Skip()

GridSimple.py (6.53 KB)

···

--
Robin Dunn
Software Craftsman
robin@AllDunn.com
http://wxpython.org Java give you jitters?
http://wxpros.com Relax with wxPython!