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 4 1594
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 ca*****@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
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 ca*****@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
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? ca*****@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 ca*****@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
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: 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?
ca*****@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 ca*****@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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Brian |
last post by:
Hi, All,
I'm using MS.net 2003 and using a windows.forms.listbox control for my
window application but I don't know why the Horizontal Scrollbar could NOT be
shown even if I set...
|
by: Bill |
last post by:
I have have two list boxes. One is a listing of all possible variables.
We'll call this listbox A. The other is a listing of all the selected
variables. We'll call this listbox B. If a person...
|
by: Simon P |
last post by:
Hello group, I'm in desperate need of help. Here goes :
I have the following tables : CONTACTS (ContactID, FirstName,
LastName, Company, etc.), SHOWS (ShowID, ShowDescription) and
SHOWDETAILS...
|
by: Josema |
last post by:
Hi to all,
I have a class (persons) that derives from collection base:
and another class (person) with this properties:
-ID
-Name
When i have complete filled the object Persons with all...
|
by: MrNobody |
last post by:
Hi, I'm doing something where I add custom objects to a ListBox which have
aToString() method overriden so it displays what I want. When adding
instances of these custom objects to the ListBox I...
|
by: yamne |
last post by:
I have a problem.
When I click in edit datagrid button I show two listbox and two button.
I use two button to move data between two listbox.
My problem is that I can't call the listbox in the...
|
by: Ron |
last post by:
I've got a listbox that holds a list of groups. Users can
select a group, hit the remove button and the group
should be removed from the listbox. The only problem is
that no matter which group you...
|
by: nirdeshonline |
last post by:
Hi,
I have added a simple listbox in windows form under c# 2.0. It contains
a collection of approx 10 strings as list items. Now when i resize the
form whole listbox flickers.
Please tell me...
|
by: zdrakec |
last post by:
Hello all:
Clearly, I'm not getting it! Here is the scenario:
On a web page, I have two list boxen and a text box. The first listbox
is populated at page load time (if it is not a postback)....
|
by: Doogie |
last post by:
I have a .net app that a user currently enters a number in a text box,
hits a button and a data call is executed. She wants the ability to
enter in multiple numbers (up to 100).
So to make...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |