ListView problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joza

    ListView problem

    Hi!

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

    private void LVKalk_Selected IndexChanged(ob ject sender, System.EventArg s e)
    {
    try
    {
    txtBox1.Text = LVKalk.Selected Items[0].Text;
    txtBox2.Text = LVKalk.Selected Items[0].SubItems[1].Text;
    }
    catch (ArgumentOutOfR angeException err)
    {
    err = null;
    }
    }

    What I'm doing wrong?

    Thnx.
  • Peter Duniho

    #2
    Re: ListView problem

    On Fri, 11 Apr 2008 14:03:14 -0700, Joza
    <josip.pejakovi c@MAKNIOVOetfos .hrwrote:
    I'm getting error when selecting items in ListView control, it
    says ArguementOutOfR angeExceptions. That happens in SelectIndexChan ge
    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

    Comment

    • Joza

      #3
      Re: ListView problem

      Peter Duniho wrote:
      On Fri, 11 Apr 2008 14:03:14 -0700, Joza
      <josip.pejakovi c@MAKNIOVOetfos .hrwrote:
      >
      >I'm getting error when selecting items in ListView control, it
      >says ArguementOutOfR angeExceptions. That happens in SelectIndexChan ge
      >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 SelectItemChang e 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 (ArgumentOutOfR angeException 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.

      Comment

      • Peter Duniho

        #4
        Re: ListView problem

        On Fri, 11 Apr 2008 15:31:00 -0700, Joza
        <josip.pejakovi c@MAKNIOVOetfos .hrwrote:
        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..
        The best way to get unconfused is to find out what's happening. You can
        either change the code in the way I suggested, or even better you can just
        look at the current state of the data in the debugger when the exception
        happens.

        Without finding out what's actually happening, you'll remain confused, I'm
        afraid. That's just how life is.
        I get this error only when i use SelectItemChang e event,
        but if i use Click or DoubleClick events there is no such error, it
        works fine.
        If I had to guess (and so far, looks like I do), I'd say that you're
        getting the exception when you click outside a valid item, thus causing no
        items to be selected, thus the SelectedItems collection winds up empty,
        thus the exception.

        For some collection controls, you only get a Click event when an actual
        item in the collection was selected. I haven't bothered to check, but
        ListView might be one of those controls. In which case, obviously you
        won't get notified except when an actual item in the collection is
        selected. That would explain why your code fails when you're handling the
        SelectedItemCha nged event but not the Click event.

        But guessing is no way to write code. It's much better to just look and
        see what's going on.

        I would do so myself, except you haven't provided a concise-but-complete
        code sample with which to do so.
        I have changed this part of code
        catch (ArgumentOutOfR angeException err)
        {
        err = null;
        }
        >
        with this one
        >
        catch (Exception ex) {}
        Neither version is an appropriate solution, really. Assuming the
        exception being thrown is really the ArgumentOutOfRa ngeException, then
        either is equivalent. And neither helps you understand what's wrong or
        address it in a more correct way.
        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.
        Well, there are no error messages because you're eating them all. I can't
        explain the pause in your program's execution. Exceptions are expensive,
        but they aren't _that_ expensive. Unless for some reason you get a huge
        number of them all at once.

        Still, it's possible that if you wrote the code in a way that didn't
        generate exceptions, your problems might go away.

        Pete

        Comment

        • =?Utf-8?B?U2FpbXZw?=

          #5
          RE: ListView problem

          Hello Joza. Good Day.

          Try this code.


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

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


          "Joza" wrote:
          Hi!
          >
          I'm getting error when selecting items in ListView control, it
          says ArguementOutOfR angeExceptions. That happens in SelectIndexChan ge
          event. I have tryed with try...catch but there's no effect.
          The code inside that event is:
          >
          private void LVKalk_Selected IndexChanged(ob ject sender, System.EventArg s e)
          {
          try
          {
          txtBox1.Text = LVKalk.Selected Items[0].Text;
          txtBox2.Text = LVKalk.Selected Items[0].SubItems[1].Text;
          }
          catch (ArgumentOutOfR angeException err)
          {
          err = null;
          }
          }
          >
          What I'm doing wrong?
          >
          Thnx.
          >

          Comment

          • Martin

            #6
            Re: ListView problem

            On Apr 12, 3:21 am, Saimvp <Sai...@discuss ions.microsoft. comwrote:
            Hello Joza. Good Day.
            >
            Try this code.
            >
            txtBox1.Text = LVKalk.Items[LVKalk.FocusedI tem.Index].SubItems[0].text;
            txtBox2.Text = LVKalk.Items[LVKalk.FocusedI tem.Index].SubItems[1].text;
            >
            I hope it can help you.
            --
            To be Happy is To be Yourself
            >
            >
            >
            "Joza" wrote:
            Hi!
            >
            I'm getting error when selecting items in ListView control, it
            says ArguementOutOfR angeExceptions. That happens in SelectIndexChan ge
            event. I have tryed with try...catch but there's no effect.
            The code inside that event is:
            >
            private void LVKalk_Selected IndexChanged(ob ject sender, System.EventArg se)
            {
            try
            {
            txtBox1.Text = LVKalk.Selected Items[0].Text;
            txtBox2.Text = LVKalk.Selected Items[0].SubItems[1].Text;
            }
            catch (ArgumentOutOfR angeException 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.Selecte dItems.Count 0)
            {
            ...
            }

            Comment

            Working...