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
  1. Home
  2. General Programming
  3. C#
  4. Listboxes, Strings And Arrays

Listboxes, Strings And Arrays

Scheduled Pinned Locked Moved C#
helptutorialdata-structures
3 Posts 2 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.
  • B Offline
    B Offline
    Ben Magee
    wrote on last edited by
    #1

    Hi, I'm probably missing something really simple here, but i'm stuck. Basically I have a listbox, which a user puts items into via a textbox and button, this works fine, the items go in. Now, on exit the items save to a text file, this also works fine, ive checked all the items are contained in the text file. Now, when the user re-opens the application there is a button called load previous items, when i click this i have a streamreader reading the text document, also works fine, checked with a messagebox, and it saves it to the string Now, I want the string to be saved into a array which is then put back into the textbox, the problem being, I dont know how to count the items in the string (each on a diffrent line) to put them into a array, and then after that I need a example of adding the array into my listbox. Im guessing its something to do with the listbox1.Addrange(); command. Cheers, Ben.

    OriginalGriffO 1 Reply Last reply
    0
    • B Ben Magee

      Hi, I'm probably missing something really simple here, but i'm stuck. Basically I have a listbox, which a user puts items into via a textbox and button, this works fine, the items go in. Now, on exit the items save to a text file, this also works fine, ive checked all the items are contained in the text file. Now, when the user re-opens the application there is a button called load previous items, when i click this i have a streamreader reading the text document, also works fine, checked with a messagebox, and it saves it to the string Now, I want the string to be saved into a array which is then put back into the textbox, the problem being, I dont know how to count the items in the string (each on a diffrent line) to put them into a array, and then after that I need a example of adding the array into my listbox. Im guessing its something to do with the listbox1.Addrange(); command. Cheers, Ben.

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      If the file content is indeed strings each on a new line, then the easy way is:

              string\[\] lines = File.ReadAllLines(@"c:\\Temp.txt");
              ListBox1.Items.AddRange(lines);
      

      Which means you don't need to know how many lines there are. [edit]Where did my "[" and "]" characters go? Must remember to preview...[/edit]

      No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      B 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        If the file content is indeed strings each on a new line, then the easy way is:

                string\[\] lines = File.ReadAllLines(@"c:\\Temp.txt");
                ListBox1.Items.AddRange(lines);
        

        Which means you don't need to know how many lines there are. [edit]Where did my "[" and "]" characters go? Must remember to preview...[/edit]

        No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

        B Offline
        B Offline
        Ben Magee
        wrote on last edited by
        #3

        Legendary :D :D Thanks Alot :) Ben.

        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