Home All Groups Group Topic Archive Search About

Listbox Webcontrol SelectedIndexChanged Event not working

Author
28 Jun 2005 5:47 PM
Scott Lemen
Hi,

   My Listbox control is a webcontrol and its AutoPostBack property is set
to True.  When I left-click on an item the event code here executes:

    Private Sub ListBox2_SelectedIndexChanged(ByVal sender As Object, ByVal
e As System.EventArgs) Handles ListBox2.SelectedIndexChanged
        If sender.selectedIndex > -1 Then
            AddUserToDL(sender.selecteditem)
        End If
    End Sub

   The problem is the sender.selectedIndex property is never greater than -1
and the sender.SelectedItem is set to Nothing.  This happens whether the
control's SelectedMode is set to Single or Multiple and it does not matter
which of the 100 plus items, or multiple items, is selected.  Does anyone
know what could cause this?

  I am running Visual Studio 2003 Enterprise on a Windows 2000 Pro SP4
computer.  The web server is IIS, which is running on my own workstation.

Thank you for your assistance.

Scott Lemen

Author
28 Jun 2005 7:56 PM
lisa
Instead of sender.SelectedItem, try CType(sender,
ListBox).SelectedIndex.  SelectedIndex isn't a property of Object, and
sender is an Object unless you convert it.  If you're using VS.NET, the
fact that the S in SelectedIndex didn't become capitalized would have
been a tipoff.

Lisa


Scott Lemen wrote:
Show quoteHide quote
> Hi,
>
>    My Listbox control is a webcontrol and its AutoPostBack property is set
> to True.  When I left-click on an item the event code here executes:
>
>     Private Sub ListBox2_SelectedIndexChanged(ByVal sender As Object, ByVal
> e As System.EventArgs) Handles ListBox2.SelectedIndexChanged
>         If sender.selectedIndex > -1 Then
>             AddUserToDL(sender.selecteditem)
>         End If
>     End Sub
>
>    The problem is the sender.selectedIndex property is never greater than -1
> and the sender.SelectedItem is set to Nothing.  This happens whether the
> control's SelectedMode is set to Single or Multiple and it does not matter
> which of the 100 plus items, or multiple items, is selected.  Does anyone
> know what could cause this?
>
>   I am running Visual Studio 2003 Enterprise on a Windows 2000 Pro SP4
> computer.  The web server is IIS, which is running on my own workstation.
>
> Thank you for your assistance.
>
> Scott Lemen
Author
28 Jun 2005 8:22 PM
Scott Lemen
Lisa,

   Thank you for replying but your solution did not solve the problem.  Even
when the Event code is changed as shown just below, the
myListBox.SelectedIndex = -1 and .SelectedItem is Nothing.  Also,
mySelectedListItemIndex = -1.

Regards,

Scott

    Private Sub ListBox2_SelectedIndexChanged(ByVal sender As Object, ByVal
e As System.EventArgs) Handles ListBox2.SelectedIndexChanged
        Dim mySelectedListItemIndex As Integer
        Dim myListBox As ListBox
        myListBox = CType(sender, ListBox)
        mySelectedListItemIndex = CType(sender, ListBox).SelectedIndex

        If mySelectedListItemIndex > -1 Then
            AddUserToDL(myListBox.Items(mySelectedListItemIndex))
        End If
    End Sub


Show quoteHide quote
"l***@starways.net" wrote:

> Instead of sender.SelectedItem, try CType(sender,
> ListBox).SelectedIndex.  SelectedIndex isn't a property of Object, and
> sender is an Object unless you convert it.  If you're using VS.NET, the
> fact that the S in SelectedIndex didn't become capitalized would have
> been a tipoff.
>
> Lisa
>
>
> Scott Lemen wrote:
> > Hi,
> >
> >    My Listbox control is a webcontrol and its AutoPostBack property is set
> > to True.  When I left-click on an item the event code here executes:
> >
> >     Private Sub ListBox2_SelectedIndexChanged(ByVal sender As Object, ByVal
> > e As System.EventArgs) Handles ListBox2.SelectedIndexChanged
> >         If sender.selectedIndex > -1 Then
> >             AddUserToDL(sender.selecteditem)
> >         End If
> >     End Sub
> >
> >    The problem is the sender.selectedIndex property is never greater than -1
> > and the sender.SelectedItem is set to Nothing.  This happens whether the
> > control's SelectedMode is set to Single or Multiple and it does not matter
> > which of the 100 plus items, or multiple items, is selected.  Does anyone
> > know what could cause this?
> >
> >   I am running Visual Studio 2003 Enterprise on a Windows 2000 Pro SP4
> > computer.  The web server is IIS, which is running on my own workstation.
> >
> > Thank you for your assistance.
> >
> > Scott Lemen
>
>
Author
28 Jun 2005 9:21 PM
lisa
Hmm... well, I think (I'm not sure, but it's easy enough to check) that
CType(e, Integer) will give you the SelectedIndex.  Other than that,
I'm stumped.

Lisa


Scott Lemen wrote:
Show quoteHide quote
> Lisa,
>
>    Thank you for replying but your solution did not solve the problem.  Even
> when the Event code is changed as shown just below, the
> myListBox.SelectedIndex = -1 and .SelectedItem is Nothing.  Also,
> mySelectedListItemIndex = -1.
>
> Regards,
>
> Scott
>
>     Private Sub ListBox2_SelectedIndexChanged(ByVal sender As Object, ByVal
> e As System.EventArgs) Handles ListBox2.SelectedIndexChanged
>         Dim myListBox As ListBox = CType(sender, ListBox)
>         Dim mySelectedListItemIndex As Integer = myListBox.SelectedIndex
>
>         If mySelectedListItemIndex > -1 Then
>             AddUserToDL(myListBox.Items(mySelectedListItemIndex))
>         End If
>     End Sub
>
>
> "l***@starways.net" wrote:
>
> > Instead of sender.SelectedItem, try CType(sender,
> > ListBox).SelectedIndex.  SelectedIndex isn't a property of Object, and
> > sender is an Object unless you convert it.  If you're using VS.NET, the
> > fact that the S in SelectedIndex didn't become capitalized would have
> > been a tipoff.
> >
> > Lisa
> >
> >
> > Scott Lemen wrote:
> > > Hi,
> > >
> > >    My Listbox control is a webcontrol and its AutoPostBack property is set
> > > to True.  When I left-click on an item the event code here executes:
> > >
> > >     Private Sub ListBox2_SelectedIndexChanged(ByVal sender As Object, ByVal
> > > e As System.EventArgs) Handles ListBox2.SelectedIndexChanged
> > >         If sender.selectedIndex > -1 Then
> > >             AddUserToDL(sender.selecteditem)
> > >         End If
> > >     End Sub
> > >
> > >    The problem is the sender.selectedIndex property is never greater than -1
> > > and the sender.SelectedItem is set to Nothing.  This happens whether the
> > > control's SelectedMode is set to Single or Multiple and it does not matter
> > > which of the 100 plus items, or multiple items, is selected.  Does anyone
> > > know what could cause this?
> > >
> > >   I am running Visual Studio 2003 Enterprise on a Windows 2000 Pro SP4
> > > computer.  The web server is IIS, which is running on my own workstation.
> > >
> > > Thank you for your assistance.
> > >
> > > Scott Lemen
> >
> >