【问题标题】:C# How to increase value in every second and the increment value is based on amount.text fileC#如何每秒增加值,增量值基于amount.text文件
【发布时间】:2020-03-24 15:58:33
【问题描述】:

IDE 是 Visual Studio 2010。

我有两个名为 total-cost.txt 和 amount.txt 的文本文件 里面的文件如下所示:

total-cost.txt
4500000

amount.txt
600

第一个文本文件(total-cost.txt)表示将在文本框显示的总成本(文本框名称为totalcost)。

第二个文件(amount.txt)代表每秒的增量值。

我正在尝试显示来自 total-cost.txt 的总成本并自动增加在 amount.txt 中设置的每一秒的值

例如:

1 秒后 4500000 变为 2 秒后 4500600 4501200 等等。

如果我将 amount.txt 的值从 600 更改为 700,它将变为

1 秒后 4500000 变为 2 秒后 4500700 4501400 等等。

该值将保持刷新并仅显示最新的总费用。

问题是我已经在文本框中显示了总成本值,但我不知道如何增加由 amount.txt 设置的值

我做的代码在下面

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Globalization;

namespace new_countdown
{
    public partial class Form1 : Form
    {         
        private string TotalCost;
        private int TotalFont;

        public Form1()
        {
            InitializeComponent();
        }

        private void ReadTotalCostFile()
        {
            try
            {
                StreamReader sr = File.OpenText("total-cost.txt");
                TotalCost = sr.ReadToEnd();
                sr.Close();
            }
            catch { }
        }

        private void UpdateDisplay() 
        {
            if (totalcost.Text != TotalCost)
            {
               totalcost.Text = TotalCost;
            }

            if (totalcost.Font.Size != TotalFont && TotalFont != 0)
            { 
                this.totalcost.Font = new System.Drawing.Font("Microsoft Sans Serif",(float)TotalFont,System.Drawing.FontStyle.Bold,
                System.Drawing.GraphicsUnit.Point,((byte)(0)));
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            UpdateDisplay();
            ReadTotalCostFile();
        }
   }
}

不知何故,我刚刚在文本框中完成了显示总成本。

我对自动增量没有任何想法。

有没有人分享这个想法或解决方案。我非常感谢它。

【问题讨论】:

  • 您好,为了可读性,请尽量只发布相关代码sn-ps,而不是整个类。
  • @Stefan 好的,我只想尽可能说清楚。下次我会注意到的。谢谢你。
  • 不使用C语言的C语言标签有什么用?
  • @Gerhardh 我已被修改。谢谢你。

标签: c# windows visual-studio-2010 textview text-files


【解决方案1】:
using System;
using System.IO;

private void IncrementInt32ValueInFile(string filePath)
{
    var currentFileText = File.ReadAllText(filePath);
    if (int.TryParse(currentFileText, out int integerValue))
    {
        File.WriteAllText(filePath, Convert.ToString(++integerValue));
    }

    throw new Exception($"Incorrect file content. Path: {filePath}"); // If value in file can't be parsed as integer
}

【讨论】:

  • 感谢您的回复。我看不懂代码,你能解释一下吗? filePath 是什么意思,是我需要替换的所有 filePath 吗?如果是,我应该替换哪个文件以及在我的完整代码中在哪里添加此代码。谢谢。
  • @Kim filePath 是您的 amount.txt 文件路径的参数。只需将此方法插入您的课程并在您想要的地方使用它
  • 好的。例如: private void IncrementInt32ValueInFile(string filePath) { var currentFileText = File.ReadAllText("amount.txt"); if (int.TryParse(currentFileText, out int integerValue)) { File.WriteAllText("total-cost.txt", Convert.ToString(++integerValue)); } throw new Exception("文件内容不正确。路径:{filePath}"); // 如果文件中的值不能被解析为整数 } 如果我做错了,请修改它。谢谢。
  • @Kim 是的,但是为什么你不想使用filePath 参数并直接在File.ReadAllText 方法中替换它?使用示例:IncrementInt32ValueInFile("amount.txt");
  • 亲爱的 picolino,我的程序仍然无法正常工作...您愿意帮助我通过电子邮件查看我的编码吗?如果带来任何不便,您可以忽略它。谢谢。
猜你喜欢
  • 1970-01-01
  • 2021-08-13
  • 2022-11-30
  • 2021-10-25
  • 1970-01-01
  • 2018-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多