Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading txt file to textbox

Status
Not open for further replies.

leprogrammer

Programmer
Mar 21, 2008
25
DK
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:
Code:
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
 
Have a look at System.IO.StreamReader, using this you can use the ReadLine method to read each line into an array, which you can also use to control your forward and previous buttons.

Have a look at the String Split function to break the elements of each line into an array. Set the delimiter to a space.


Hopw this helps.

[vampire][bat]
 
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:

Code:
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:
Code:
                if (pieces[0] == "end")
                {
                    curRec += 2;
                    displayLine(curRec);
                }
                else
                {

Here is all my code:
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 hold all lines of the file.
        ArrayList lines = new ArrayList(100);

        // Pointer variable to point to active record
        int curRec = 0;

        // Loading data and reading txt file
        public void Form1_Load(object sender, EventArgs e)
        {
            // Open the data file
            StreamReader f = new StreamReader(new FileStream(@"D:\monstersetbase.txt", FileMode.Open));

            String curLine;

            // Read in the data line by line and add it to our ArrayList
            while ((curLine = f.ReadLine()) != null)
            {
                lines.Add(curLine);
            }

            // Close
            f.Close();

            // Add one so it will not crash
            curRec += 1;

            // Display first line
            displayLine(curRec);
        }

        private void nxtButton_Click_1(object sender, EventArgs e)
        {
            if (curRec < lines.Count - 1)
            {
                curRec += 1;
                displayLine(curRec);
                prvButton.Enabled = true;
            }
        }

        private void prvButton_Click_1(object sender, EventArgs e)
        {
            if (curRec == 2)
            {
                prvButton.Enabled = false;
            }
            if (curRec > 0)
            {
                curRec -= 1;
                displayLine(curRec);
            }
        }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top