WidgetDataGrid onSelectionChanged

  • Jason

    Jason - 2011-02-23

    I've been using SmartWin for about two weeks and there is a lot that I don't understand and
    I've created an event handler for the WidgetDataGrid onSelectionChanged that was giving me problems.  I fixed the problem but I am curious about the onSelectionChanged event.

    My program is based on and is very similar to the AddressBook example in the SmartWin examples, I've added an event handler for the DataGrid's onSelectionChanged event, like so: 

    abbrvListView->onSelectionChanged( &SWLCentralWidget::selectedChanged );   // abbrvListView is the name of the WidgetDataGridPtr

    selectedChanged is defined as such:

    void SWLCentralWidget::selectedChanged( WidgetDataGridPtr This )
         // Get the index of the selected element to be shown.
         int index = This->getSelectedIndex();
         if (index != -1)
              // Get an iterator positioned at the student at the index
              std::list<Student*>::iterator selected = studentList.getStudentAtIndex( index ); // student list is an std::list<Student*>
              // Send the student to detailsAddView for display.
              detailsAddView->viewStudentDetails( *selected );

    The first time I make a selection, everything works like I expected, index get sets to the row number that I selected.
    The second time I make a selection, index gets set to the same index as the previous item selected even if I select a different row.  Then, (still on the second selection) the selectedChanged method runs again, this time setting the index to -1.  Finally, (still on the second selection) the selectedChanged method runs a third time, this time setting index to the index of the row that I actually selected.  Is this correct?  I expected the second selection to run just like the first selection, but every selection after the first one causes the selectedChanged method to be called three times consecutively as I have described.

    Adding the clause "if (index == -1)" keeps my program from crashing, but I am curious if the behavior described is what I am supposed to see or if I am doing something incorrectly.

  • andrew7

    andrew7 - 2011-02-23

    I'm not sure entirely, but here are some hints about how to figure it out.

    The behavior comes from  WM_NOTIFY, LVN_ITEMCHANGED

    describes the NMLISTVIEW Structure whihc is used:

    Looking at WidgetDataGrid.h
    /// Returns an unsigned integer which is the selected row of the grid.
    /** The return value defines the row in the grid that is selected. <br>
      * If the grid is in "multiple selection mode" you should rather use the
      * getSelectedRows function. <br>
      * If grid does NOT have a selected item the return value is - 1. <br>
      * Note! <br>
      * This returns the ROW of the selected item and NOT the lparam given when
      * inserted items meaning if you sort the grid or something this function will
      * return another number for the same item if its position has moved due to the
      * sort etc.
    template< class EventHandlerClass, class MessageMapPolicy >
    int WidgetDataGrid< EventHandlerClass, MessageMapPolicy >::getSelectedIndex() const
    unsigned tmpIdx = - 1;
    tmpIdx = ListView_GetNextItem( this->Widget::itsHandle, tmpIdx, LVNI_SELECTED );
    return tmpIdx;
    and then look at

    My guess would be that      detailsAddView->viewStudentDetails( *selected );
    causes some events to occur on the WidgetDataGrid.  But that s not entirely consistent with what you saw.

    A simple use of WidgetDataGrid in HelloWorld by adding:
    itsGrid->onSelectionChanged( & HelloWinClass::gridSelChanged );
    void gridSelChanged( WidgetDataGridPtr grid )
    setText( getText() + _T("+") );     // Add a '+' to the title each time we execute
    itsComboBox->setSelectedIndex( grid->getSelectedIndex() );

    showed only 1 + being added for each selection.

  • Jason

    Jason - 2011-02-25

    Thanks for responding!

    I'm busy now trying to get this project finished by the due date, but when I finish I'll be sure to check out the links you've provided. 

    Thanks again!


Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks