Home > Enterprise >  How do I make a textfile string convert to an integer and display in a numericUpDown in C#
How do I make a textfile string convert to an integer and display in a numericUpDown in C#

Time:12-03

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

CodePudding user response:

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

CodePudding user response:

is this what you're trying to do?

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