0

I am making a program that is a golf scorecard. There is a text file for the selected course they choose and when they click a button I want the program to display the previous round to numericUpDowns I have created(there are 9 of them named num(1-9).

I want the program to display the previous round, then when the user saves their current round it overwrites the textile and removes the old round, and saves the new. I hope this makes sense with what I am trying to do. I need to convert the textile string to integers that way I can display the scores and save them.

I tried this but it does not work I just wanted to see if one line is displayed. Right now the text file has the numbers 0 going down vertically 9 times for each numericUpDown but nothing is displayed in the numUpDowns.

           try
            {
                //open file
                StreamReader InputFile;
                InputFile = File.OpenText("Course1.txt");

                //read lines and display previous scores
                num1.Value.ToString(Convert.ToInt32(InputFile.ReadLine()));
            }
            catch (IOException)
            {
                MessageBox.Show("There was an error loading Course1.txt");
            }
        

2 Answers 2

2

This is how I'd do it, using a generic WinForms app to replicate things. The setup is that I dropped a textbox, a numeric up/down, a label (for error messages) and a button on a Form. Then, in the button handler I did this:

private void button1_Click(object sender, EventArgs e)
{
    if (int.TryParse(textBox1.Text, out var result) && result >=  numericUpDown1.Minimum && result <= numericUpDown1.Maximum)
    {
        numericUpDown1.Value = result;
    }
    else
    {
        label1.Text = $"You must enter a numeric value between {numericUpDown1.Minimum} and {numericUpDown1.Maximum}";
    }
}

Using TryParse rather than Convert.ToInt32 allows me to check the validity of the data without the cost of an exception.

Your code num1.Value.ToString(Convert.ToInt32(InputFile.ReadLine())); makes no sense. This part: num1.Value.ToString takes the current value of num1.Value and converts it to a string (which you don't assign to anything). Does that code even compile; there's no overload of int.ToString that takes an integer as a parameter. What you want to assign an integer to num1.Value (like my code does).

The key code in this is:

int.TryParse(textBox1.Text, out var result)

which tries to convert the string to an integer (result). It returns true if it succeeds and returns false if it can't do the conversion, and...

numericUpDown1.Value = result

which assigns that (integer) result to the value of the up/down control.

As to the issue of reading a text file and populating a set of controls, that gets a little difficult. Let's say your text file looks like:

0
1
2
...
9

That is, 10 lines, with the numbers 0 to 9, one per line. And, consider you have ten up/down controls (num1, num2, ... num10). One trick is making sure that you don't exceed the boundaries of any arrays. I'd do it like this (I'm just writing code in the SO text box here):

var lines = File.ReadLines("Course1.txt");
var controls = new NumericUpDown [] {
    num1,
    num2,
    //...
    num10,
};

var count = Math.Min(lines.Length, controls.Length);
for (var i = 0; i < count; ++i) {
    if (int.TryParse(lines[i], out var result) {
        controls[i].Value = result;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

It would be lovely if you could rename the controls after dropping them on the form..
@CaiusJard: Name them to what? The OP has no hints as to the semantics of this application. He has one control in his post (carefully named num1). The advantage of not naming them is that it makes a minimal reproducible example easier to reproduce. I can say "drop these three controls on a form" and not have to bother saying "and rename the first to xyz, the second to foo and the third to bar"
0

is this what you're trying to do?

num1.Value = Convert.ToInt32(InputFile.ReadLine());

2 Comments

That is what I was trying to do yes. How would I make the program then clear the text file line and save the score line by line?
string[] scores = new string[9]; scores[0] = num1.Value.ToString(); //... scores[8] = num9.Value.ToString(); File.WriteAllLines(@"C:\golfscores.txt", scores);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.