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. Transferring items from a listbox to a string variable [modified]

Transferring items from a listbox to a string variable [modified]

Scheduled Pinned Locked Moved C#
helpquestiondatabasedata-structures
7 Posts 4 Posters 1 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.
  • G Offline
    G Offline
    gamer1127
    wrote on last edited by
    #1

    Hello. I'm having a little problem transferring all the items in the listbox to a string variable. All the items in the listbox are also string type so i don't have any problems about data types. I tried to use array of string type variable but when i transfer them using a for..loop statement it gets a run-time error that the index was out of bounds. How will I resolve that? How can i store all the items into a variable without selecting them? I used the listbox as a control for outputs. this is the code:

    for (int a = 0; a < lstBoxRequests.Items.Count; a++)
    {
    string[] passedRequests = new string[a];

                    passedRequests\[a\] = Convert.ToString(lstBoxRequests.Items\[a\]);
    
                    MessageBox.Show(passedRequests\[a\]);
                }
    

    modified on Friday, November 6, 2009 6:41 AM

    C L 2 Replies Last reply
    0
    • G gamer1127

      Hello. I'm having a little problem transferring all the items in the listbox to a string variable. All the items in the listbox are also string type so i don't have any problems about data types. I tried to use array of string type variable but when i transfer them using a for..loop statement it gets a run-time error that the index was out of bounds. How will I resolve that? How can i store all the items into a variable without selecting them? I used the listbox as a control for outputs. this is the code:

      for (int a = 0; a < lstBoxRequests.Items.Count; a++)
      {
      string[] passedRequests = new string[a];

                      passedRequests\[a\] = Convert.ToString(lstBoxRequests.Items\[a\]);
      
                      MessageBox.Show(passedRequests\[a\]);
                  }
      

      modified on Friday, November 6, 2009 6:41 AM

      C Offline
      C Offline
      Covean
      wrote on last edited by
      #2

      Try this:

      string[] passedRequests = new string[lstBoxRequests.Items.Count];
      for (int a = 0; a < lstBoxRequests.Items.Count; a++)
      {

                      passedRequests\[a\] = Convert.ToString(lstBoxRequests.Items\[a\]);
      
                      MessageBox.Show(passedRequests\[a\]);
                  }
      

      Greetings Covean

      1 Reply Last reply
      0
      • G gamer1127

        Hello. I'm having a little problem transferring all the items in the listbox to a string variable. All the items in the listbox are also string type so i don't have any problems about data types. I tried to use array of string type variable but when i transfer them using a for..loop statement it gets a run-time error that the index was out of bounds. How will I resolve that? How can i store all the items into a variable without selecting them? I used the listbox as a control for outputs. this is the code:

        for (int a = 0; a < lstBoxRequests.Items.Count; a++)
        {
        string[] passedRequests = new string[a];

                        passedRequests\[a\] = Convert.ToString(lstBoxRequests.Items\[a\]);
        
                        MessageBox.Show(passedRequests\[a\]);
                    }
        

        modified on Friday, November 6, 2009 6:41 AM

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        How manu elements are there in your array? And how many elements would the loop go through? If your array holds 10 items, then a for-next loop will go from either 1 to 10 or from 0 to 9. Your loop goes 11 times, starting at passedRequest[0] and moving on to passedRequst[10], generating an "index out of bounds". That means that the number between the [] is smaller or larger then the number of items in the array :)

        for (int a = 0; a < lstBoxRequests.Items.Count -1; a++)

        Next item is that you're reinitializing the array on each loop. That means that the entire Array is created anew, with a new maximum-upperbound of "a". You'd prolly want a maximum number in the array that corresponds to the number of items in the combobox. That would come to something like this;

        int itemCount = lstBoxRequests.Items.Count -1;
        string[] passedRequests = new string[itemCount];
        for (int a = 0; a < itemCount; a++)
        {
        passedRequests[a] = Convert.ToString(lstBoxRequests.Items[a]);

        There are easier ways to enumerate through the items, but I'll leave that to the next poster :)

        I are Troll :suss:

        V C 2 Replies Last reply
        0
        • L Lost User

          How manu elements are there in your array? And how many elements would the loop go through? If your array holds 10 items, then a for-next loop will go from either 1 to 10 or from 0 to 9. Your loop goes 11 times, starting at passedRequest[0] and moving on to passedRequst[10], generating an "index out of bounds". That means that the number between the [] is smaller or larger then the number of items in the array :)

          for (int a = 0; a < lstBoxRequests.Items.Count -1; a++)

          Next item is that you're reinitializing the array on each loop. That means that the entire Array is created anew, with a new maximum-upperbound of "a". You'd prolly want a maximum number in the array that corresponds to the number of items in the combobox. That would come to something like this;

          int itemCount = lstBoxRequests.Items.Count -1;
          string[] passedRequests = new string[itemCount];
          for (int a = 0; a < itemCount; a++)
          {
          passedRequests[a] = Convert.ToString(lstBoxRequests.Items[a]);

          There are easier ways to enumerate through the items, but I'll leave that to the next poster :)

          I are Troll :suss:

          V Offline
          V Offline
          V 0
          wrote on last edited by
          #4

          I think you'll miss one item with this code.

          Eddy Vluggen wrote:

          for (int a = 0; a < lstBoxRequests.Items.Count -1; a++)

          or do this:

          for (int a = 0; a <= lstBoxRequests.Items.Count -1; a++)

          or this:

          for (int a = 0; a < lstBoxRequests.Items.Count; a++)

          V.
          Stop smoking so you can: Enjoy longer the money you save. Moviereview Archive

          1 Reply Last reply
          0
          • L Lost User

            How manu elements are there in your array? And how many elements would the loop go through? If your array holds 10 items, then a for-next loop will go from either 1 to 10 or from 0 to 9. Your loop goes 11 times, starting at passedRequest[0] and moving on to passedRequst[10], generating an "index out of bounds". That means that the number between the [] is smaller or larger then the number of items in the array :)

            for (int a = 0; a < lstBoxRequests.Items.Count -1; a++)

            Next item is that you're reinitializing the array on each loop. That means that the entire Array is created anew, with a new maximum-upperbound of "a". You'd prolly want a maximum number in the array that corresponds to the number of items in the combobox. That would come to something like this;

            int itemCount = lstBoxRequests.Items.Count -1;
            string[] passedRequests = new string[itemCount];
            for (int a = 0; a < itemCount; a++)
            {
            passedRequests[a] = Convert.ToString(lstBoxRequests.Items[a]);

            There are easier ways to enumerate through the items, but I'll leave that to the next poster :)

            I are Troll :suss:

            C Offline
            C Offline
            Covean
            wrote on last edited by
            #5

            for (int a = 0; a < lstBoxRequests.Items.Count; a++)

            Is right, your loop:

            for (int a = 0; a < lstBoxRequests.Items.Count - 1; a++)

            is wrong it leaves one item alone! General: for(int loop=0; loop < count; loop++) or for(int loop=0; loop <= count -1; loop++)! His problem was the following:

            for (int a = 0; a < lstBoxRequests.Items.Count; a++)
            {
            string[] passedRequests = new string[a];

                            passedRequests\[a\] = Convert.ToString(lstBoxRequests.Items\[a\]);   <---- this causes the out of array, because passedRequests.length = a but passedRequests\[a\] is wrong !!!
            
                            MessageBox.Show(passedRequests\[a\]);
                        }
            

            Edit: Second problem was the creation of the string array in the loop.

            Greetings Covean

            L 1 Reply Last reply
            0
            • C Covean

              for (int a = 0; a < lstBoxRequests.Items.Count; a++)

              Is right, your loop:

              for (int a = 0; a < lstBoxRequests.Items.Count - 1; a++)

              is wrong it leaves one item alone! General: for(int loop=0; loop < count; loop++) or for(int loop=0; loop <= count -1; loop++)! His problem was the following:

              for (int a = 0; a < lstBoxRequests.Items.Count; a++)
              {
              string[] passedRequests = new string[a];

                              passedRequests\[a\] = Convert.ToString(lstBoxRequests.Items\[a\]);   <---- this causes the out of array, because passedRequests.length = a but passedRequests\[a\] is wrong !!!
              
                              MessageBox.Show(passedRequests\[a\]);
                          }
              

              Edit: Second problem was the creation of the string array in the loop.

              Greetings Covean

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Covean wrote:

              or (int a = 0; a < lstBoxRequests.Items.Count - 1; a++) is wrong it leaves one item alone!

              You're right, I was wrong. I've been looking at these too much;

              for(int x = 0; x <= bla.Count - 1; x++)

              Notice that the check has an equals-sign. The current project that I'm working has both styles mixed throughout the code. In VB6 :)

              I are Troll :suss:

              V 1 Reply Last reply
              0
              • L Lost User

                Covean wrote:

                or (int a = 0; a < lstBoxRequests.Items.Count - 1; a++) is wrong it leaves one item alone!

                You're right, I was wrong. I've been looking at these too much;

                for(int x = 0; x <= bla.Count - 1; x++)

                Notice that the check has an equals-sign. The current project that I'm working has both styles mixed throughout the code. In VB6 :)

                I are Troll :suss:

                V Offline
                V Offline
                V 0
                wrote on last edited by
                #7

                Eddy Vluggen wrote:

                In VB6

                that explains it :-D

                V.
                Stop smoking so you can: Enjoy longer the money you save. Moviereview Archive

                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