【发布时间】:2017-06-30 11:39:41
【问题描述】:
我目前正在使用 VS Community 2017 用 C# 编写一个应用程序来计算和组织汽油(对于汽车,每 100 公里有多少升等)。
我有两个框架,一个显示所有数据的概述,一个用于输入新数据。当有人输入新数据时,我想在 frame1 刷新。因此,我有一种方法可以使用文件中的内容更改标签的文本,该文件中保存了所有数据。
所以我的问题似乎类似于this 问题,但不知何故标签的文本没有改变。我也试过this 但也在这里,标签文本不会改变。我没有收到任何错误,因此无法提供此信息,但我认为解决方案 1 不起作用,因为我不使用简单的框架,而是使用另一个现有框架的实例。
以下是代码中最重要的部分:
第一帧:
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();
}
}
}
第二帧:
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 += "DataTC.txt";?在 reload 中添加一个断点,看看它是否在 form2 关闭时命中。
标签: c# winforms visual-studio frame