Welcome to Dream.In.Code
Become a C# Expert!

Join 150,403 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 948 people online right now. Registration is fast and FREE... Join Now!




Reading txt file to textbox

 
Reply to this topicStart new topic

Reading txt file to textbox

Dumpen
21 Mar, 2008 - 08:00 PM
Post #1

New D.I.C Head
*

Joined: 21 Mar, 2008
Posts: 40

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
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Reading Txt File To Textbox
21 Mar, 2008 - 08:40 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 314 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well one way to do this is to load up all the lines into an array of some sort. I used an arraylist in the example below. Then create a display function which uses a pointer variable to point at each string, split it, then put the pieces into the textboxes. You would use the next and previous buttons to control the pointer variable and to refresh the boxes.

It is crude but effective and the only bug in it is that your create names that have a space in them are also split so the name "ice monster" would appear in the textbox as "ice

That will be your homework to work on. Once you get it, problem solved. wink2.gif

csharp

// Arraylist to hold all lines of the file.
ArrayList lines = new ArrayList(100);

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

private void Form1_Load(object sender, EventArgs e)
{
// Open the data file
StreamReader f = new StreamReader(new FileStream("c:\\data.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();

// Display first line
displayLine(curRec);
}

// Displays the line in the arraylist
private 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();

// Populate the text boxes with the appropriate pieces
txtID.Text = pieces[0];
txtName.Text = pieces[2];
}

}

// Moves the pointer variable back one and displays the value
private void btnPrevious_Click(object sender, EventArgs e)
{
if (curRec > 0) {
curRec -= 1;
displayLine(curRec);
}
}

// Moves the pointer variable up one and displays the value
private void btnNext_Click(object sender, EventArgs e)
{
if (curRec < lines.Count - 1) {
curRec += 1;
displayLine(curRec);
}
}


Read the in code comments to see how I am doing this. If you want to rip out the splitting of the line and do something with the line read in, feel free. It is up to you as to how you want to handle the line once you read it in.

This is only a quick example. Enjoy!

"At DIC we be splitting creature names since the beginning of the millennium... that and code ninjas!" decap.gif
User is offlineProfile CardPM
+Quote Post

Dumpen
RE: Reading Txt File To Textbox
22 Mar, 2008 - 06:38 AM
Post #3

New D.I.C Head
*

Joined: 21 Mar, 2008
Posts: 40

It might be a quick example, but it works like a charm

I only have one question now

I got the button back to be disabled when there are nothing to go back to with:
CODE
           if (curRec == 1)
            {
                prev_monster_button.Enabled = false;
            }


And then in the next button code I am enabling in it again:
CODE
            if (curRec < lines.Count - 1)
            {
                curRec += 1;
                displayLine(curRec);
                prev_monster_button.Enabled = true;
            }


But how do I do it so it will disable my next button like my previous?

If there is no content for the next button it should be disabled

Im kinda confused there blink.gif
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Reading Txt File To Textbox
22 Mar, 2008 - 06:51 AM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 314 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Just check if curRec is equal to the arraylist count. You see how in the next button you check if curRec is less than arraylist count? Well after you hit the last record and increment curRec, it will equal arraylist count and hence make that if statement false and won't let you go to the next record right? At that time you would disable the next button.

smile.gif
User is offlineProfile CardPM
+Quote Post

Dumpen
RE: Reading Txt File To Textbox
22 Mar, 2008 - 07:17 AM
Post #5

New D.I.C Head
*

Joined: 21 Mar, 2008
Posts: 40

QUOTE(Martyr2 @ 22 Mar, 2008 - 07:51 AM) *

Just check if curRec is equal to the arraylist count. You see how in the next button you check if curRec is less than arraylist count? Well after you hit the last record and increment curRec, it will equal arraylist count and hence make that if statement false and won't let you go to the next record right? At that time you would disable the next button.

smile.gif


You are my hero! biggrin.gif

CODE
            if (curRec == lines.Count -1)
            {
                next_monster_button.Enabled = false;
            }


Success tongue.gif

EDIT: Just read my rank

Would be fun if the site was named something with a K in the end wink2.gif

This post has been edited by Dumpen: 22 Mar, 2008 - 07:36 AM
User is offlineProfile CardPM
+Quote Post

Dumpen
RE: Reading Txt File To Textbox
22 Mar, 2008 - 07:54 AM
Post #6

New D.I.C Head
*

Joined: 21 Mar, 2008
Posts: 40

I have created a way so it takes ice monster

But is it a good way and how do I combine the string that I marked with a bold with a space between so it is Monster Name and not MonsterName?

CODE
                // My string to split
                [b]string str = pieces[2] + pieces[3];[/b]

                // Split seperator
                char[] seps = {' '};

                // Split the string into parts.
                string[] parts = str.Split(seps);
                for (int i = 0; i < parts.Length; i++)
                    monster_name_textbox.Text = parts[i].Replace("\"", "");;


Im guessing it should be something like:
string str = pieces[2] + " " + pieces[3]; but cant seem to get it to wkr

This post has been edited by Dumpen: 22 Mar, 2008 - 07:55 AM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Reading Txt File To Textbox
22 Mar, 2008 - 08:07 AM
Post #7

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 314 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
QUOTE
You are my hero!


Dream.In.Code... where the code heroes hang out. We are like the justice league of code. PsychoCoder is the Flash, NickDMax is Firestorm, Nykc is black lightning, no2pencil is Red tornado, letthecolorsrumble is the Green Latern, benigndesign is Black Canary, supersweety is wonder woman, supersloth is superman and I... I am batman!

Now skyhawk... while he might be on par with Professor xavier of sorts, he doesn't have the ability to kill men. He is a lady killer instead.

TOGETHER, WE FORM THE DIC JUSTICE LEAGUE!

We fight for justice, to protect newbies, and spread code of hope to all those who seek it.

biggrin.gif

QUOTE(Dumpen @ 22 Mar, 2008 - 08:54 AM) *

I have created a way so it takes ice monster

But is it a good way and how do I combine the string that I marked with a bold with a space between so it is Monster Name and not MonsterName?

CODE
                // My string to split
                [b]string str = pieces[2] + pieces[3];[/b]

                // Split seperator
                char[] seps = {' '};

                // Split the string into parts.
                string[] parts = str.Split(seps);
                for (int i = 0; i < parts.Length; i++)
                    monster_name_textbox.Text = parts[i].Replace("\"", "");;


Im guessing it should be something like:
string str = pieces[2] + " " + pieces[3]; but cant seem to get it to wkr


Well if you simply deleted the space in the name, it will be read in as a whole string there. You might want to consider replacing the space with a special character like an underscore and simply read in the name and replace the underscore with the space.

It is up to you and your design as to how you want to do that.


This post has been edited by Martyr2: 22 Mar, 2008 - 08:03 AM
User is offlineProfile CardPM
+Quote Post

Dumpen
RE: Reading Txt File To Textbox
22 Mar, 2008 - 04:20 PM
Post #8

New D.I.C Head
*

Joined: 21 Mar, 2008
Posts: 40

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

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 07:18PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month