Windows Develop Bookmark and Share   
 index > Windows Forms General > Reading txt file to textbox
 

Reading txt file to textbox

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 Snippet

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
Dumpen  Saturday, March 22, 2008 3:58 AM

Try

if (pieces.Length == 1)
{
curRec -= 2;
displayLine(curRec);
}
else
{
....
}

Zhi-Xin Ye  Wednesday, March 26, 2008 8:26 AM
Hi there,

Have a look at the string.split method. it returns a string array, and splits the string based on the separator you give it.

Hope this helps.


PS If you have any say over the look of the file, consider using serialization. That way you can simply serialize your form (or an object containing the data) which will make it so much easier to read a file and set the textboxes.

Rick van den Bosch  Saturday, March 22, 2008 9:29 AM
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 Snippet

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 Snippet

if (pieces[0] == "end")
{
curRec += 2;
displayLine(curRec);
}
else
{



Here is all my code:
Code Snippet

// 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);
}
}

Dumpen  Sunday, March 23, 2008 12:21 AM

Try

if (pieces.Length == 1)
{
curRec -= 2;
displayLine(curRec);
}
else
{
....
}

Zhi-Xin Ye  Wednesday, March 26, 2008 8:26 AM
Thanks for the answear

It all works now Stick out tongue
Dumpen  Saturday, April 05, 2008 11:45 AM

You can use google to search for other answers

Custom Search

More Threads

• Using SplitContainer in MDI parent
• Synchronization primitives, exitContext and AsyncOperation
• Moving a control around on a form?
• Clling Control.Update() from a class which does not inherit from Control
• some WebClient questions
• pattern / architecture suggestion needed for Winforms Application.
• Simple non-technical explanation as to how "skins" interpret music.
• C# context menu
• "Cannot access a disposed object" error from Application.Run() after closing form
• Weird problem with a Panel and PictureBoxes