【发布时间】:2017-10-24 13:10:34
【问题描述】:
我正在 Visual Studio 2017 上使用 c# 构建一个应用程序。我的界面由几个文本框和两个单选按钮组成。每次关闭应用程序时,我都会将文本框的值保存到两个单独的文本文件中。因此,我有两个文本文件(每个单选按钮)。每次选中单选按钮时,我都会从相应的 txt 文件中加载文本框值。这就是它现在的实现方式:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
TextBox[] boxes = { textBoxAscStart, textBoxAscStep };
loadConfigFile(boxes);
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
TextBox[] boxes = { textBoxAscStart, textBoxAscStep };
loadConfigFile(boxes);
}
private void loadConfigFile(TextBox[] boxes) {
try
{
string dir = Directory.GetCurrentDirectory();
string fullName;
if (radioButton1.Checked == true)
{
fullName = dir + "\\Config1.txt";
}
else
{
fullName = dir + "\\Config2.txt";
}
char[] delimiter = new char[] { '\t' };
using (System.IO.StreamReader file = new System.IO.StreamReader(@fullName))
{
for (int i = 0; i < boxes.Length; i++)
{
string[] line = file.ReadLine().Split(delimiter);
boxes[i].Text = line[1];
}
}
}
catch (IOException ex)
{
MessageBox.Show(ex.Message);
}
}
我知道我以虚拟的方式实现了这一点,很抱歉我的代码很糟糕。这种方法可以正常工作,直到您主要处理选中的单选按钮之一,然后退出应用程序以便将值写入文件。但是当用户在一个单选按钮上工作然后切换到另一个单选按钮(使用另一个值)时,我如何实现这种情况,在那里工作一点然后回来?就我而言,他仍然会从文本文件中看到那些旧值。我希望每次用户返回上一个单选按钮时,文本框中的值都是他输入的最新值。我希望我足够清楚。感谢您的帮助。
【问题讨论】:
-
在后台缓存它们。
标签: c# asp.net visual-studio textbox radio-button