ListBox oddity

M

mick

Ive got a listbox with two items

int index = listBox1.SelectedIndex //This works ok and gives a value of 1
string strItem = listBox1.Text // This however doesnt and throws an
"IndexOutOfRangeException - Index was outside
// the bounds of the Array"

How so? How can the index be out of range when we know it's 1? Anyone with
more of an understanding of listboxes
have any inkling why this Ex is being thrown.

mick
 
R

Registered User

Ive got a listbox with two items

int index = listBox1.SelectedIndex //This works ok and gives a value of 1
string strItem = listBox1.Text // This however doesnt and throws an
"IndexOutOfRangeException - Index was outside
// the bounds of the Array"

How so? How can the index be out of range when we know it's 1? Anyone with
more of an understanding of listboxes
have any inkling why this Ex is being thrown.
From the description it is not clear where the exception orginates but
it is not in the two lines of code shown. What value is expected from
listBox1.Text? AFAIK there is no relationship between a ListBox's Text
property and its SelectedIndex property. Here is how the ListBox's
SelectedIndex property can be used.

int index = listBox1.SelectedIndex;
string strItem = listBox1.Items[index];

regards
A.G.
 
R

Registered User

Ive got a listbox with two items

int index = listBox1.SelectedIndex //This works ok and gives a value of 1
string strItem = listBox1.Text // This however doesnt and throws an
"IndexOutOfRangeException - Index was outside
// the bounds of the Array"

How so? How can the index be out of range when we know it's 1? Anyone with
more of an understanding of listboxes
have any inkling why this Ex is being thrown.
From the description it is not clear where the exception orginates but
it is not in the two lines of code shown. What value is expected from
listBox1.Text? AFAIK there is no relationship between a ListBox's Text
property and its SelectedIndex property. Here is how the ListBox's
SelectedIndex property can be used.

int index = listBox1.SelectedIndex;
string strItem = listBox1.Items[index];

regards
A.G.
 
R

Registered User

[...] AFAIK there is no relationship between a ListBox's Text
property and its SelectedIndex property. Here is how the ListBox's
SelectedIndex property can be used.

As the ListBox docs clearly state, the ListBox.Text property returns the
string corresponding to the currently selected item (or the first item
selected, if more than one item is selected). I would call that a
"relationship between a ListBox's Text property and its SelectedIndex
property".
int index = listBox1.SelectedIndex;
string strItem = listBox1.Items[index];

The "Items" property returns a ListBox.ObjectCollection instance, which as
the type "object" for the elements. You can't assign an element from that
collection to a "string" variable without some kind of conversion (e.g.
casting or calling ToString()).
I was unaware of the relationship, thanks for the enlightenment. I try
to avoid UI work because it is too subjective. The actual text
selected by the user is generally of much less concern then using the
selected index to access the proper object in the collection. The
unfortunate errors occurred because I was thinking object while the
code specified string, my bad. That is what I get for writing code off
the top of my head using the wrong tool.

I apologize for any confusion
A.G.
 
R

Registered User

[...] AFAIK there is no relationship between a ListBox's Text
property and its SelectedIndex property. Here is how the ListBox's
SelectedIndex property can be used.

As the ListBox docs clearly state, the ListBox.Text property returns the
string corresponding to the currently selected item (or the first item
selected, if more than one item is selected). I would call that a
"relationship between a ListBox's Text property and its SelectedIndex
property".
int index = listBox1.SelectedIndex;
string strItem = listBox1.Items[index];

The "Items" property returns a ListBox.ObjectCollection instance, which as
the type "object" for the elements. You can't assign an element from that
collection to a "string" variable without some kind of conversion (e.g.
casting or calling ToString()).
I was unaware of the relationship, thanks for the enlightenment. I try
to avoid UI work because it is too subjective. The actual text
selected by the user is generally of much less concern then using the
selected index to access the proper object in the collection. The
unfortunate errors occurred because I was thinking object while the
code specified string, my bad. That is what I get for writing code off
the top of my head using the wrong tool.

I apologize for any confusion
A.G.
 
C

Cor Ligthert[MVP]

Mick,

Are you using a datasource.

A listbox is like a combobox inherited from a listcontrol.

If it is with a datasource then it is the

selecteditem
and/or
selectedvalue

Cor
 
C

Cor Ligthert[MVP]

Mick,

Are you using a datasource.

A listbox is like a combobox inherited from a listcontrol.

If it is with a datasource then it is the

selecteditem
and/or
selectedvalue

Cor
 
M

mick

Cor Ligthert said:
Mick,

Are you using a datasource.

I am. Sorry should have mentioned.
A listbox is like a combobox inherited from a listcontrol.

If it is with a datasource then it is the

selecteditem
and/or
selectedvalue

I`ll give that a go. The thing is, this is an old bit of code that has only
just started kicking off. Unless I`ve changed it without realising, wouldn`t
be the first time:)

Thanks again,

mick
 
M

mick

Cor Ligthert said:
Mick,

Are you using a datasource.

I am. Sorry should have mentioned.
A listbox is like a combobox inherited from a listcontrol.

If it is with a datasource then it is the

selecteditem
and/or
selectedvalue

I`ll give that a go. The thing is, this is an old bit of code that has only
just started kicking off. Unless I`ve changed it without realising, wouldn`t
be the first time:)

Thanks again,

mick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top