C# 将值从.txt文件加载到 NumericUpDown

本文关键字:加载 NumericUpDown 文件 txt | 更新日期: 2023-09-27 18:34:00

我希望将.txt文件中包含的值加载到数字上下。我一直在使用以下方法将文本从 .txts 加载到组合框中:

//Load Movelist
        if (comboBox_PlayerChar.SelectedIndex == 27 && gameno == 3)
        {
            charno = 27;
            this.comboBox_Movelist.Items.Clear();
            StreamReader movelist = new StreamReader(@"filepath'document.txt");
            string line = movelist.ReadLine();
            while (line != null)
            {
                comboBox_Movelist.Items.Add(line);
                line = movelist.ReadLine();
            }
         }

我想这将是数字下降的类似方法,但老实说,我不知道该怎么做。我已经在互联网上做了一些窥探,似乎没有其他人想这样做。

tl;dr,我需要某种方法来获取文本文件中的单个数字,将其写入变量并将数字UpDown设置为该变量。

重要的部分是将值放入变量中。 设置实际的数字UpDown很容易。

希望你明白我的意思。

C# 将值从.txt文件加载到 NumericUpDown

如果是文本文件中的单个数字值

using (StreamReader sr = new StreamReader(@"filepath'document.txt"))
{    
    // read the first line
    string line = sr.ReadLine();
    // parse the line for an integer
    int value;
    int.TryParse(line, out value);
    // if the line in the file was indeed an integer, the variable value will be equal to it now
    // sr will be disposed at end of using block
}

使用此代码

while(!movelist.EndOfStream)
{
    comboBox_Movelist.Items.Add(line);
    line = movelist.ReadLine();
}