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. Reading txt file to textbox

Reading txt file to textbox

Scheduled Pinned Locked Moved C#
tutorialcsharphelpquestion
4 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.
  • C Offline
    C Offline
    Casper Hansen
    wrote on last edited by
    #1

    Hello. I am developing a program where I have some textboxes and forward and previous buttons. I also have a txt file. I want to read from my txt file and then write to my textbox. My txt file looks like this: 19 1 "Yeti" 30 900 0 105 110 37 0 150 37 2 17 4 6 400 2000 10 2 180 14 6 0 0 3 0 0 20 1 "Elite Yeti" 36 1200 0 120 125 50 0 180 43 3 0 1 6 400 1400 10 2 180 14 6 0 1 4 1 1 21 1 "Assassin" 26 800 0 95 100 33 0 130 33 2 0 1 7 400 2000 10 2 180 14 6 0 0 0 0 0 22 1 "Ice Monster" 22 650 0 80 85 27 0 110 27 2 7 1 5 400 2000 10 2 170 14 6 0 0 3 0 0 23 1 "Hommerd" 24 700 0 85 90 29 0 120 29 3 0 1 5 400 1600 10 2 170 14 6 0 0 3 0 0 24 1 "Worm" 20 600 0 75 80 25 0 100 25 3 0 1 4 400 1600 10 2 160 14 6 0 0 2 0 0 25 1 "Ice Queen" 52 4000 0 155 165 90 0 260 76 3 11 4 7 400 1400 50 2 180 14 3 0 4 5 4 4 I want it to read one line and then for example set Yeti and 19 into a textbox When you push forward it reads elite yeti and 20 But I don't know how to do I am assuming I need space as a separator and then use like var[3] where 3 is the third word. A little like in mirc where you can write $gettok(one two three, 32, 3) where it picks the 3 word with the separator space (32) But when it gets to the code and not the idea im kinda stuck. Any ideas? Im using Visual C# 2008 Express Edition

    C C 2 Replies Last reply
    0
    • C Casper Hansen

      Hello. I am developing a program where I have some textboxes and forward and previous buttons. I also have a txt file. I want to read from my txt file and then write to my textbox. My txt file looks like this: 19 1 "Yeti" 30 900 0 105 110 37 0 150 37 2 17 4 6 400 2000 10 2 180 14 6 0 0 3 0 0 20 1 "Elite Yeti" 36 1200 0 120 125 50 0 180 43 3 0 1 6 400 1400 10 2 180 14 6 0 1 4 1 1 21 1 "Assassin" 26 800 0 95 100 33 0 130 33 2 0 1 7 400 2000 10 2 180 14 6 0 0 0 0 0 22 1 "Ice Monster" 22 650 0 80 85 27 0 110 27 2 7 1 5 400 2000 10 2 170 14 6 0 0 3 0 0 23 1 "Hommerd" 24 700 0 85 90 29 0 120 29 3 0 1 5 400 1600 10 2 170 14 6 0 0 3 0 0 24 1 "Worm" 20 600 0 75 80 25 0 100 25 3 0 1 4 400 1600 10 2 160 14 6 0 0 2 0 0 25 1 "Ice Queen" 52 4000 0 155 165 90 0 260 76 3 11 4 7 400 1400 50 2 180 14 3 0 4 5 4 4 I want it to read one line and then for example set Yeti and 19 into a textbox When you push forward it reads elite yeti and 20 But I don't know how to do I am assuming I need space as a separator and then use like var[3] where 3 is the third word. A little like in mirc where you can write $gettok(one two three, 32, 3) where it picks the 3 word with the separator space (32) But when it gets to the code and not the idea im kinda stuck. Any ideas? Im using Visual C# 2008 Express Edition

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      the split method on the string class would usually do it, but because you have spaces in words, you need to either use split them rejoin based on quotes, or write your own parser.

      Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

      C 1 Reply Last reply
      0
      • C Christian Graus

        the split method on the string class would usually do it, but because you have spaces in words, you need to either use split them rejoin based on quotes, or write your own parser.

        Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

        C Offline
        C Offline
        Casper Hansen
        wrote on last edited by
        #3

        I have been ripping my hair out because I cant find a solution. I got it all working, but now I must format more text I got a new text that has sections like this: 1 0 33 10 85 162 95 168 -1 5 //Bull Fighter 1 29 30 40 113 45 116 -1 3 //Hound 2 41 5 126 160 125 161 -1 2 //Budge Dragon 3 38 5 106 161 111 160 -1 2 //Spider 4 38 5 106 161 111 160 -1 2 //Elite Bull Fighter 6 38 5 106 161 111 160 -1 2 //Lich 7 38 5 106 161 111 160 -1 2 //Giant 14 38 5 106 161 111 160 -1 2 //Skeleton Warrior end 0 275 33 10 85 162 95 168 -1 5 //Test 1 Section 2 275 29 30 40 113 45 116 -1 3 //Test 2 Section 2 275 41 5 126 160 125 161 -1 2 //Test 3 Section 2 275 38 5 106 161 111 160 -1 2 //Test 4 Section 2 end Now I changed my script so it reads the values from this script, and I changed the forward button so it jumps into new sections But I just cant seem to figure out how to jump back to a section This is the code that I use to go foward into a section: if (pieces[0] == "end") { curRec += 2; displayLine(curRec); } else { Here is all my code: // Displays the line in the arraylist public void displayLine(int linenumber) { // If the line number is in the range of total values if ((linenumber >= 0) && (linenumber < lines.Count)) { // Get the string out of the arraylist String line = (String)lines[linenumber]; // Split it into various pieces on the space String[] pieces = line.Split('\t'); if (pieces[0] == "end") { curRec += 2; displayLine(curRec); } else { txtID.Text = pieces[0]; txtMap.Text = pieces[1]; txtMoving.Text = pieces[2]; txtXStart.Text = pieces[3]; txtYStart.Text = pieces[4]; txtXEnd.Text = pieces[5]; txtYEnd.Text = pieces[6]; txtDirection.Text = pieces[7]; txtCount.Text = pieces[8]; txtComment.Text = pieces[9]; mobImage.ImageLocation = "D:/images/" + pieces[0] + ".jpg"; } } } // Arraylist to

        1 Reply Last reply
        0
        • C Casper Hansen

          Hello. I am developing a program where I have some textboxes and forward and previous buttons. I also have a txt file. I want to read from my txt file and then write to my textbox. My txt file looks like this: 19 1 "Yeti" 30 900 0 105 110 37 0 150 37 2 17 4 6 400 2000 10 2 180 14 6 0 0 3 0 0 20 1 "Elite Yeti" 36 1200 0 120 125 50 0 180 43 3 0 1 6 400 1400 10 2 180 14 6 0 1 4 1 1 21 1 "Assassin" 26 800 0 95 100 33 0 130 33 2 0 1 7 400 2000 10 2 180 14 6 0 0 0 0 0 22 1 "Ice Monster" 22 650 0 80 85 27 0 110 27 2 7 1 5 400 2000 10 2 170 14 6 0 0 3 0 0 23 1 "Hommerd" 24 700 0 85 90 29 0 120 29 3 0 1 5 400 1600 10 2 170 14 6 0 0 3 0 0 24 1 "Worm" 20 600 0 75 80 25 0 100 25 3 0 1 4 400 1600 10 2 160 14 6 0 0 2 0 0 25 1 "Ice Queen" 52 4000 0 155 165 90 0 260 76 3 11 4 7 400 1400 50 2 180 14 3 0 4 5 4 4 I want it to read one line and then for example set Yeti and 19 into a textbox When you push forward it reads elite yeti and 20 But I don't know how to do I am assuming I need space as a separator and then use like var[3] where 3 is the third word. A little like in mirc where you can write $gettok(one two three, 32, 3) where it picks the 3 word with the separator space (32) But when it gets to the code and not the idea im kinda stuck. Any ideas? Im using Visual C# 2008 Express Edition

          C Offline
          C Offline
          Casper Hansen
          wrote on last edited by
          #4

          It all works now :P

          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