Home All Groups Group Topic Archive Search About
Author
20 Jun 2006 6:20 PM
camainc
I am adding some objects to a standard list box. When I add multiple
objects, I experience no problems. However, when I add a single object,
I cannot set the SelectedIndex property to 0 - it stays at -1. Does
anyone know why this is happening, and how to work around it?

Sample code:

        With lstUsers
            .BeginUpdate()
            .Items.Clear()
            For Each entity As IBusinessEntity In Coll
                .Items.Add(entity)
            Next
            .EndUpdate()
        End With

To select the first item in the list:

        If lstUsers.Count > 0 Then
            lstUsers.SelectedIndex = 0
        End If

The above code works fine for multiple objects, but with only one
object the SelectedIndex stays at -1. Setting the SelectedIndex to 0
doesn't throw an exception, but it never changes.

I need some help with this.

Thanks

Author
20 Jun 2006 6:55 PM
Ahmed
Hi,

I tried your code on both .NET 1.1 and .NET 2.0 except instead of using
objects I used strings and it works fine. First item is selected wether
there is one value or multiple values. Did you try entity.toString? Are
you sure your code compiles? listbox.count is not a valid property. I
am assuming you meant lstUsers.items.count >0


cama***@gmail.com wrote:
Show quoteHide quote
> I am adding some objects to a standard list box. When I add multiple
> objects, I experience no problems. However, when I add a single object,
> I cannot set the SelectedIndex property to 0 - it stays at -1. Does
> anyone know why this is happening, and how to work around it?
>
> Sample code:
>
>         With lstUsers
>             .BeginUpdate()
>             .Items.Clear()
>             For Each entity As IBusinessEntity In Coll
>                 .Items.Add(entity)
>             Next
>             .EndUpdate()
>         End With
>
> To select the first item in the list:
>
>         If lstUsers.Count > 0 Then
>             lstUsers.SelectedIndex = 0
>         End If
>
> The above code works fine for multiple objects, but with only one
> object the SelectedIndex stays at -1. Setting the SelectedIndex to 0
> doesn't throw an exception, but it never changes.
>
> I need some help with this.
>
> Thanks
Author
20 Jun 2006 7:06 PM
camainc
Thanks for the reply.

I can't use the .ToString method because the objects have properties
and data that I am accessing. It is a really weird problem, but I think
I've found a workaround. When there is only one object in my
collection, I do not call .BeginUpdate() and .EndUpdate().

For some reason, when I do that I am able to programmatically select
the single object in the listbox.


Ahmed wrote:
Show quoteHide quote
> Hi,
>
> I tried your code on both .NET 1.1 and .NET 2.0 except instead of using
> objects I used strings and it works fine. First item is selected wether
> there is one value or multiple values. Did you try entity.toString? Are
> you sure your code compiles? listbox.count is not a valid property. I
> am assuming you meant lstUsers.items.count >0
>
>
> cama***@gmail.com wrote:
> > I am adding some objects to a standard list box. When I add multiple
> > objects, I experience no problems. However, when I add a single object,
> > I cannot set the SelectedIndex property to 0 - it stays at -1. Does
> > anyone know why this is happening, and how to work around it?
> >
> > Sample code:
> >
> >         With lstUsers
> >             .BeginUpdate()
> >             .Items.Clear()
> >             For Each entity As IBusinessEntity In Coll
> >                 .Items.Add(entity)
> >             Next
> >             .EndUpdate()
> >         End With
> >
> > To select the first item in the list:
> >
> >         If lstUsers.Count > 0 Then
> >             lstUsers.SelectedIndex = 0
> >         End If
> >
> > The above code works fine for multiple objects, but with only one
> > object the SelectedIndex stays at -1. Setting the SelectedIndex to 0
> > doesn't throw an exception, but it never changes.
> >
> > I need some help with this.
> >
> > Thanks
Author
20 Jun 2006 7:20 PM
Ahmed
I am glad you found a work around for the problem. I just tried your
code with object this time and it also worked fine. Are you sure that:

            lstUsers.SelectedIndex = 0
is being executed?

cama***@gmail.com wrote:
Show quoteHide quote
> Thanks for the reply.
>
> I can't use the .ToString method because the objects have properties
> and data that I am accessing. It is a really weird problem, but I think
> I've found a workaround. When there is only one object in my
> collection, I do not call .BeginUpdate() and .EndUpdate().
>
> For some reason, when I do that I am able to programmatically select
> the single object in the listbox.
>
>
> Ahmed wrote:
> > Hi,
> >
> > I tried your code on both .NET 1.1 and .NET 2.0 except instead of using
> > objects I used strings and it works fine. First item is selected wether
> > there is one value or multiple values. Did you try entity.toString? Are
> > you sure your code compiles? listbox.count is not a valid property. I
> > am assuming you meant lstUsers.items.count >0
> >
> >
> > cama***@gmail.com wrote:
> > > I am adding some objects to a standard list box. When I add multiple
> > > objects, I experience no problems. However, when I add a single object,
> > > I cannot set the SelectedIndex property to 0 - it stays at -1. Does
> > > anyone know why this is happening, and how to work around it?
> > >
> > > Sample code:
> > >
> > >         With lstUsers
> > >             .BeginUpdate()
> > >             .Items.Clear()
> > >             For Each entity As IBusinessEntity In Coll
> > >                 .Items.Add(entity)
> > >             Next
> > >             .EndUpdate()
> > >         End With
> > >
> > > To select the first item in the list:
> > >
> > >         If lstUsers.Count > 0 Then
> > >             lstUsers.SelectedIndex = 0
> > >         End If
> > >
> > > The above code works fine for multiple objects, but with only one
> > > object the SelectedIndex stays at -1. Setting the SelectedIndex to 0
> > > doesn't throw an exception, but it never changes.
> > >
> > > I need some help with this.
> > >
> > > Thanks
Author
20 Jun 2006 8:04 PM
camainc
Yep. I worked with it for quite awhile, trying this and that, until I
came upon the solution above. I think there must be something going on
with object references behind the scenes that I'm just not seeing.

Thanks for your interest.

Ahmed wrote:
Show quoteHide quote
> I am glad you found a work around for the problem. I just tried your
> code with object this time and it also worked fine. Are you sure that:
>
>             lstUsers.SelectedIndex = 0
> is being executed?
>
> cama***@gmail.com wrote:
> > Thanks for the reply.
> >
> > I can't use the .ToString method because the objects have properties
> > and data that I am accessing. It is a really weird problem, but I think
> > I've found a workaround. When there is only one object in my
> > collection, I do not call .BeginUpdate() and .EndUpdate().
> >
> > For some reason, when I do that I am able to programmatically select
> > the single object in the listbox.
> >
> >
> > Ahmed wrote:
> > > Hi,
> > >
> > > I tried your code on both .NET 1.1 and .NET 2.0 except instead of using
> > > objects I used strings and it works fine. First item is selected wether
> > > there is one value or multiple values. Did you try entity.toString? Are
> > > you sure your code compiles? listbox.count is not a valid property. I
> > > am assuming you meant lstUsers.items.count >0
> > >
> > >
> > > cama***@gmail.com wrote:
> > > > I am adding some objects to a standard list box. When I add multiple
> > > > objects, I experience no problems. However, when I add a single object,
> > > > I cannot set the SelectedIndex property to 0 - it stays at -1. Does
> > > > anyone know why this is happening, and how to work around it?
> > > >
> > > > Sample code:
> > > >
> > > >         With lstUsers
> > > >             .BeginUpdate()
> > > >             .Items.Clear()
> > > >             For Each entity As IBusinessEntity In Coll
> > > >                 .Items.Add(entity)
> > > >             Next
> > > >             .EndUpdate()
> > > >         End With
> > > >
> > > > To select the first item in the list:
> > > >
> > > >         If lstUsers.Count > 0 Then
> > > >             lstUsers.SelectedIndex = 0
> > > >         End If
> > > >
> > > > The above code works fine for multiple objects, but with only one
> > > > object the SelectedIndex stays at -1. Setting the SelectedIndex to 0
> > > > doesn't throw an exception, but it never changes.
> > > >
> > > > I need some help with this.
> > > >
> > > > Thanks