关闭 Form2 时 Form1 执行方法不起作用
Form1 execute Method when Form2 is closed doesn't work
我目前正在使用 VS Community 2017 在 C# 中编写一个应用程序来计算和组织汽油(对于汽车,每 100 公里多少升等)。
我有两个框架,一个显示所有数据的概览,一个用于输入新数据。当有人输入新数据时,我想在 frame1 刷新。因此,我有一种方法可以使用保存了所有数据的文件中的内容更改标签的文本。
所以我的问题似乎类似于 this problem, but somehow the text of the label doesn't change. I also tried this 但在这里,标签文本不会改变。我没有收到任何错误,所以我不能提供这个,但我认为解决方案 1 不起作用,因为我不使用简单的框架我使用另一个已经存在的框架的实例。
以下是代码中最重要的部分:
第 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();
}
}
}
第 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) { }
}
我目前正在使用 VS Community 2017 在 C# 中编写一个应用程序来计算和组织汽油(对于汽车,每 100 公里多少升等)。
我有两个框架,一个显示所有数据的概览,一个用于输入新数据。当有人输入新数据时,我想在 frame1 刷新。因此,我有一种方法可以使用保存了所有数据的文件中的内容更改标签的文本。
所以我的问题似乎类似于 this problem, but somehow the text of the label doesn't change. I also tried this 但在这里,标签文本不会改变。我没有收到任何错误,所以我不能提供这个,但我认为解决方案 1 不起作用,因为我不使用简单的框架我使用另一个已经存在的框架的实例。
以下是代码中最重要的部分:
第 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();
}
}
}
第 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) { }
}