Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > about SelectedIndexChanged event in listview
 

about SelectedIndexChanged event in listview

Hi i am very newbie in this vb.net world but i am developing a windows forms . I am using this code:

Private Sub lvwLocales_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvwLocales.SelectedIndexChanged
        ObtenerLocal(Trim(lvwLocales.SelectedItems(0).Text))
        MapearVariables()
        BloquearControles(TipoBloqueo.tbNormal, False, "Administrador")
 End Sub

when i change the index doing a click to another listitem the command lvwLocales.SelectedItems(0).Text show me an error

 "Additional information: Specified argument was out of the range of valid values.>"

any help?

thanks in advanced

César
MigrationUser 1  Monday, January 27, 2003 9:38 PM
One solution was to change the event.. so i put in the click event but is no efficient cause.. if i by mistake click the same item .. the click event is fired.. 

If you have any another answer let me know thanks

Thanks in advanced 

César
MigrationUser 1  Monday, January 27, 2003 10:37 PM
The second time the ListView raises the SeclectedIndexChanged event, it raises it twice.  

The first of each of these pairs of SelectedIndexChanged events is raised when the index is changed from the previously selected item to no selected items.  The second event is when the selected index is changed to the newly selected item.

So, the first event in the pair has zero selected items and hence your out of range exception.  The second one is the event you actually want, it has the selected item you care about.

To protectively code for this, put the following lines around the code in your event handler:

If ListView1.SelectedIndices.Count > 0 Then
    <Your Code>
End If

This behavior is a bug in the .NET Framework.  We'll address it for the next version.

thanks
 - mike
MigrationUser 1  Tuesday, January 28, 2003 12:17 AM
<non_sarcasm>wow, crazy...a bug?  I never noticed!</non_sarcasm>

I would suggest just always putting what mike suggested in the SelectedIndexChanged Event anyway, because if someone clicks off of the selected item (somewhere in the blank area), then no items will be selected and it will blow an error anyway.
MigrationUser 1  Tuesday, January 28, 2003 12:17 PM
thanks both for their answers!!! :)
MigrationUser 1  Thursday, January 30, 2003 9:47 PM
Mike,

Is this also a bug in the .net framework if the control is a combobox?

Thanks,
MigrationUser 1  Tuesday, February 04, 2003 2:00 PM
not really, no, because the ComboBox can only have one item selected at a time.  When there's nothing selected, the SelectedItem Property is Nothing (or null)
MigrationUser 1  Wednesday, February 05, 2003 7:02 PM

You can use google to search for other answers

Custom Search

More Threads

• allow nulls in databinding with DateTime datatype and typed dataset
• Datagridview - handling external table updates
• Datagridview Programmatic Sorting of Columns data, Need suggestion
• DataGridView column - currency format
• Excel export from GRID with large data
• should I use DataGrid? advice needed
• datagrid loop
• Could I modify the command created by aqlcommandbuilder?
• DatagridvewComboBox - databound lists per row
• Bound Datagrid, but want to add a drop down with elements from another table