Aaron Kim Aaron Kim - 6 days ago 5
C# Question

Load data from a text file into multiple objects on a Windows Form

I need some help loading text from within a text file into multiple Textboxes and Labels on one of my forms. My form has 5 textboxes and labels. They are labelled 'txtPlayer1', 'lblPlayer1Points' etc.

I have 5 lines of code in a text file that has been saved via another form, and the data in the text file looks like this:


First Place: AK - Aaron, Points: 44

Second Place: GJ - George, Points: 24

Third Place: SB - Sarah, Points: 40

Fourth Place: JB - Jason, Points: 29

Fifth Place: JS - Jacob, Points: 22

Total Points: 159


Is there anyway I can put the player name on the first line (AK - Aaron) into the 'txtPlayer1' textbox and then her points (44) into 'lblPlayer1Points'?

Ideally I'd want this done for each line so I end up with 'txtPlayer2' with
'GJ - George' and 'lblPlayer2Points' with '24' all the way until the fifth line is done.

EDIT:
This is the code I use to open the text file on my form. A button is pressed and this code is executed:

OpenFileDialog FileDialog = new OpenFileDialog();

FileDialog.Title = "Select Text File!";
FileDialog.InitialDirectory = @"c:\\";
FileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";

if (FileDialog.ShowDialog() == DialogResult.OK)
{ string fileName;
fileName = FileDialog.FileName;
Properties.Settings.Default.fullPath = Path.Combine(Directory.GetCurrentDirectory(), fileName);


Now I had this section of code that works for the first line but not the others:

string filePath = Properties.Settings.Default.fullPath;

string[] lines = null;
lines = File.ReadAllLines(filePath);



//READING TOP LINE

string textLine = lines[0];
string[] stringSplit = textLine.Split(' ');

string firstName = stringSplit[1];
string firstPoints = stringSplit[3];
txtFirstName.Text = FirstName;
lblFirstPoints.Text = firstPoints;


If I try and apply this to other lines it simply just cause my form to crash.

Answer

Try something like this:

        string ctlName;
        Control[] matches;
        Control player, score;
        string[] lines = File.ReadAllLines(filePath);
        for(int i = 0; i < 5; i++)
        {
            player = null;
            ctlName = "txtPlayer" + (i + 1).ToString();
            matches = this.Controls.Find(ctlName, true);
            if (matches.Length > 0)
            {
                player = matches[0];
                player.Text = "";
            }

            score = null;
            ctlName = "lblPlayer" + (i + 1).ToString() + "Points";
            matches = this.Controls.Find(ctlName, true);
            if (matches.Length > 0)
            {
                score = matches[0];
                score.Text = "";
            }

            if (i < lines.GetUpperBound(0))
            {
                string[] parts = lines[i].Split(",".ToCharArray());
                if (parts.Length == 2)
                {
                    string[] nameParts = parts[0].Split(":".ToCharArray());
                    if (nameParts.Length == 2)
                    {
                        if (player != null)
                        {
                            player.Text = nameParts[1].Trim();
                        }
                    }
                    string[] scoreParts = parts[1].Split(":".ToCharArray());
                    if (scoreParts.Length == 2)
                    {
                        if (score != null)
                        {
                            score.Text = scoreParts[1].Trim();
                        }
                    }
                }
            }
        }
Comments