ListView problem

J

Joza

Hi!

I'm getting error when selecting items in ListView control, it
says ArguementOutOfRangeExceptions. That happens in SelectIndexChange
event. I have tryed with try...catch but there's no effect.
The code inside that event is:

private void LVKalk_SelectedIndexChanged(object sender, System.EventArgs e)
{
try
{
txtBox1.Text = LVKalk.SelectedItems[0].Text;
txtBox2.Text = LVKalk.SelectedItems[0].SubItems[1].Text;
}
catch (ArgumentOutOfRangeException err)
{
err = null;
}
}

What I'm doing wrong?

Thnx.
 
J

Joza

Peter said:
I'm getting error when selecting items in ListView control, it
says ArguementOutOfRangeExceptions. That happens in SelectIndexChange
event. I have tryed with try...catch but there's no effect.
[...]
What I'm doing wrong?

What is it that you want to have happen? What is in your ListView in
the first place?

The exception is telling you that either the SelectedItems collection
has fewer than 1 element (i.e. no elements), or the SubItems collection
has fewer than 2 elements.

The only obvious thing in your code example that's wrong is the line
where you set the "err" variable to null. Since doing so has no effect
outside that block of code, and you don't have any other code inside
that block, whatever it is you expect setting it to null to do, I'm
confident it doesn't do that.

Something wrong that's perhaps less obvious is the assumption in the
code that the SelectedItems collection has at least one element, and
that the SubItems collection has at least two elements. Whether that's
a fair assumption for you to make is hard to say without a complete code
example. But obviously the assumption is wrong.

It seems to me that if you checked the count of elements in each
collection before trying to use them, you would at least have a better
idea of which assumption has failed. It's possible that with that
additional information, it would also be more obvious why your
assumption was incorrect and what you need to do to correct that (either
don't make the assumption, or fix the code that was supposed to ensure
the assumption was correct).

Pete

hm... I'm little bit confused beacuse my listview is populated and it has
several columns and it contains more than one elemnt, but i need to get
only data from first and second column and when i select with mouse sometimes
throws me a message with error that i described early in my first post.
I get this error only when i use SelectItemChange event,
but if i use Click or DoubleClick events there is no such error, it works fine.

I have changed this part of code
catch (ArgumentOutOfRangeException err)
{
err = null;
}

with this one

catch (Exception ex) {}

And now works fine, but sometimes my program get blocked for second or two
when i selecting items in listview control and after that all works fine
and there is no any type of error messages.
 
S

Saimvp

Hello Joza. Good Day.

Try this code.


txtBox1.Text = LVKalk.Items[LVKalk.FocusedItem.Index].SubItems[0].text;
txtBox2.Text = LVKalk.Items[LVKalk.FocusedItem.Index].SubItems[1].text;

I hope it can help you.
 
M

Martin

Hello Joza. Good Day.

Try this code.

txtBox1.Text = LVKalk.Items[LVKalk.FocusedItem.Index].SubItems[0].text;
txtBox2.Text = LVKalk.Items[LVKalk.FocusedItem.Index].SubItems[1].text;

I hope it can help you.
--
To be Happy is To be Yourself



Joza said:
I'm getting error when selecting items in ListView control, it
says ArguementOutOfRangeExceptions. That happens in SelectIndexChange
event. I have tryed with try...catch but there's no effect.
The code inside that event is:
private void LVKalk_SelectedIndexChanged(object sender, System.EventArgse)
{
try
{
txtBox1.Text = LVKalk.SelectedItems[0].Text;
txtBox2.Text = LVKalk.SelectedItems[0].SubItems[1].Text;
}
catch (ArgumentOutOfRangeException err)
{
err = null;
}
}
What I'm doing wrong?
Thnx.- Hide quoted text -

- Show quoted text -

The event will be triggered when the selection changes from one, or
several, to none, just as Peter Duniho said. So what you need to do is
to ensure that at least one item is selected. I would use the
following code to make sure of that. It's also good practice to try to
avoid throwing exceptions.

if (LVKarl.SelectedItems.Count > 0)
{
...
}
 

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