【发布时间】:2019-05-10 20:32:22
【问题描述】:
我有一个带有几个 numericUpDown 控件的 Winforms,我希望程序在用户单击表单上的退出按钮时发出警告,如果他们有未保存的更改,如果没有未保存的更改继续并退出。我有我的保存按钮单击事件,每次保存后将所有 NUD 控件重置为 0。
我尝试了其中一种工作方式,其中我将所有 numericUpDown 控件值汇总并显示在一个文本框中,然后我将文本框放在 if 语句中,如果文本框 texchanged 然后触发消息框然后告诉用户关于未保存的更改或在文本框为空时退出。
但是,在单击保存按钮甚至清除所有控件之后,文本框的默认值为 0,因为它是从 numericUpDown 控件中获取该值的。所以当我点击退出按钮时,它告诉我我有一个 0 值的未保存更改。那不是我想要的。所以我尝试了一些不同的东西。如果我在 if 语句中只包含一个 NUD 控件,它工作正常,但是我有几个控件要包含,当我添加其余的 NUD 控件时它不会工作。就像它显示在我的代码上一样。我对 c# 还是很陌生,所以我被困住了。这是我现在拥有的代码。
private void btnExit2_Click(object sender, EventArgs e)
{
if (numericUpDown1RB1Rep.Value <= 0 || numericUpDown1RB2Rep.Value <= 0 || numericUpDown1RB3Rep.Value <= 0 || numericUpDown1RB4Rep.Value <= 0)
{
DialogResult dialogResult = MessageBox.Show("Are you sure you want to exit this application?", "Exit",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dialogResult == DialogResult.Yes)
{
Application.Exit();
}
else if (dialogResult == DialogResult.No)
{
return;
}
}
if (numericUpDown1RB1Rep.Value > 0 || numericUpDown1RB2Rep.Value > 0 || numericUpDown1RB3Rep.Value > 0 || numericUpDown1RB4Rep.Value > 0)
{
DialogResult dialog = MessageBox.Show("You have unsaved changes. Please save before closing this application", "Information",
MessageBoxButtons.OK, MessageBoxIcon.Information);
if (dialog == DialogResult.OK)
{
return;
}
我想要一种方法或解决方案,我不必将所有 numericUpDown 控件添加到该 if 语句中,并且可能使它看起来有点优雅。或者,也许根本不必使用 if 语句。几天来,我一直在寻求帮助并尝试不同的事情。
【问题讨论】:
-
你应该做一个控件数组。