我目前正在使用 VS Community 2017 用 C# 编写一个应用程序来计算和组织汽油(对于汽车,每 100 公里需要多少升等)。
我有两个框架,一个用于显示所有数据的概览,另一个用于输入新数据。当有人输入新数据时,我想在 frame1 处刷新。因此,我有一种方法可以使用保存了所有数据的文件中的内容更改标签的文本。
所以我的问题似乎与这个问题相似,但不知何故标签的文本没有改变。我也试过这个,但也在这里,标签文本不会改变。我没有收到任何错误,因此无法提供此信息,但我认为解决方案 1 不起作用,因为我不只是使用 Frame 我使用了另一个现有框架的实例。
以下是代码中最重要的部分:
Frame1:
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TankCheck_PC_Edition
{
public partial class f_StartTC : Form
{
string path = Directory.GetCurrentDirectory();
public f_StartTC()
{
InitializeComponent();
Reload();
}
public void Reload()
{
path += "DataTC.txt";
try { lbl_test.Text = File.ReadAllText(path); } catch (Exception ex) { }
}
....
private void cmd_add_Click(object sender, EventArgs e)
{
Input_TC.f_Input Frame2 = new Input_TC.f_Input();
Frame2.Closed += delegate
{
Reload();
};
Frame2.Show();
Frame2.FormClosed += new FormClosedEventHandler(Frame2_FormClosed);
}
void Frame2_FormClosed(object sender, FormClosedEventArgs e)
{
Reload();
}
}
}
帧 2:
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Input_TC
{
public partial class f_Input : Form
{
long Tacho = 0;
double km=0, price=0, tanked = 0;
string path = Directory.GetCurrentDirectory()+ "DataTC.txt";
public f_Input()
{
InitializeComponent();
}
private void cmd_Save_Click(object sender, EventArgs e)
{
...
if(!File.Exists(path))
File.WriteAllText(path, output);
else
File.AppendAllText(path, output);
TankCheck_PC_Edition.f_StartTC Test = new TankCheck_PC_Edition.f_StartTC();
Test.Reload();
Close();
}
}
}
其中“...”是不重要的代码被跳过。
感谢您的帮助!
您Reload
多次调用您的方法,但该path
变量仅在第一次调用该函数时有效。每次更改变量时,路径都无效并发生异常。
您可以像这样解决问题:
public void Reload()
{
var fullPath = path + "DataTC.txt";
try { lbl_test.Text = File.ReadAllText(fullPath); } catch (Exception ex) { }
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句