Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Selecting an item in a checked listview when form loads

Selecting an item in a checked listview when form loads

Scheduled Pinned Locked Moved C#
helpdatabasequestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lucy
    wrote on last edited by
    #1

    I have a checked listview on my windows form. When the form loads I have code where a specific item in the listview is selected: listView1.Items[1].Selected = true; But I also have a selected index changed event. In this event I go through all listview items and set their checked property to false and then check the item that has just been selected: foreach (ListViewItem lvi in listView1.Items) { lvi.Checked = false; } listView1.SelectedItems[0].Checked = true; These two pieces of code do not work together. If I comment one section of code, then no errors occur and vis versa but when both pieces of code are not commented out I get the following error at run time: An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll How do I solve my problem as both pieces of code are needed. Lucy

    J A 2 Replies Last reply
    0
    • L Lucy

      I have a checked listview on my windows form. When the form loads I have code where a specific item in the listview is selected: listView1.Items[1].Selected = true; But I also have a selected index changed event. In this event I go through all listview items and set their checked property to false and then check the item that has just been selected: foreach (ListViewItem lvi in listView1.Items) { lvi.Checked = false; } listView1.SelectedItems[0].Checked = true; These two pieces of code do not work together. If I comment one section of code, then no errors occur and vis versa but when both pieces of code are not commented out I get the following error at run time: An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll How do I solve my problem as both pieces of code are needed. Lucy

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      How about only assigning the SelectedIndexChanged event after making the initial item selected.

      L 1 Reply Last reply
      0
      • J J4amieC

        How about only assigning the SelectedIndexChanged event after making the initial item selected.

        L Offline
        L Offline
        Lucy
        wrote on last edited by
        #3

        So the SelectIndexChanged event would fire when the user selects an item? How would I do that? Lucy

        1 Reply Last reply
        0
        • L Lucy

          I have a checked listview on my windows form. When the form loads I have code where a specific item in the listview is selected: listView1.Items[1].Selected = true; But I also have a selected index changed event. In this event I go through all listview items and set their checked property to false and then check the item that has just been selected: foreach (ListViewItem lvi in listView1.Items) { lvi.Checked = false; } listView1.SelectedItems[0].Checked = true; These two pieces of code do not work together. If I comment one section of code, then no errors occur and vis versa but when both pieces of code are not commented out I get the following error at run time: An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll How do I solve my problem as both pieces of code are needed. Lucy

          A Offline
          A Offline
          Andrei Ungureanu
          wrote on last edited by
          #4

          Lucy_H85 wrote:

          checked listview

          Haven't heard of it. Is it a ListView with CheckBoxes or CheckedListBox?

          I will use Google before asking dumb questions

          L 1 Reply Last reply
          0
          • A Andrei Ungureanu

            Lucy_H85 wrote:

            checked listview

            Haven't heard of it. Is it a ListView with CheckBoxes or CheckedListBox?

            I will use Google before asking dumb questions

            L Offline
            L Offline
            Lucy
            wrote on last edited by
            #5

            It is a listview with checkboxes. Lucy

            A 1 Reply Last reply
            0
            • L Lucy

              It is a listview with checkboxes. Lucy

              A Offline
              A Offline
              Andrei Ungureanu
              wrote on last edited by
              #6

              Well....I will just give you an idea. The StackOverflow exception is because in the CheckChanged event you change the checked property of the listview items, thus this generating another checkchanged event, and so you enter in a infinite loop, which at a certain moment fills the system's stack and the program stops. To avoid this situation you should check if the checkedchanged event has been triggered by the user or by your code. I situation almost like these I used to use a boolean value which is set to true if I change the checked property of an item, or false if the user checks the item, and in the event handler method do whatever you need depending on the value. It's just an idea to work around this problem...maybe you'll find another one. Hope it helps.

              I will use Google before asking dumb questions

              L 1 Reply Last reply
              0
              • A Andrei Ungureanu

                Well....I will just give you an idea. The StackOverflow exception is because in the CheckChanged event you change the checked property of the listview items, thus this generating another checkchanged event, and so you enter in a infinite loop, which at a certain moment fills the system's stack and the program stops. To avoid this situation you should check if the checkedchanged event has been triggered by the user or by your code. I situation almost like these I used to use a boolean value which is set to true if I change the checked property of an item, or false if the user checks the item, and in the event handler method do whatever you need depending on the value. It's just an idea to work around this problem...maybe you'll find another one. Hope it helps.

                I will use Google before asking dumb questions

                L Offline
                L Offline
                Lucy
                wrote on last edited by
                #7

                Thank you for your help. Yes I noticed that both of these events were fired many times whilst debugging. I now understand a bit more about stacks. I will try and use boolean values to solve my problem. Lucy

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups