关闭按钮也保存值
Close button also save values
我有一个对话框,用户可以使用确定按钮输入两个整数。输入的值被保存到变量中。但是,如果我从关闭按钮关闭它,它还会保存最后输入的值,这意味着不仅 OK 按钮会保存它们。我在这里做错了什么?
对话框:
namespace WindowsFormsApplication1
{
public static class inputBoxDialog
{
public static void ShowDialog(string text1, string text2, string caption, int[] lastValue)
{
Form prompt = new Form();
prompt.Width = 500;
prompt.Height = 250;
prompt.Text = caption;
prompt.StartPosition = FormStartPosition.CenterScreen;
//input 1
Label textLabel = new Label() { Left = 50, Top = 20, Text = text1 };
NumericUpDown inputBox1 = new NumericUpDown() { Left = 50, Top = 50, Width = 400 };
inputBox1.Value = lastValue[0];
//input 2
Label textLabe2 = new Label() { Left = 50, Top = 100, Text = text2 };
NumericUpDown inputBox2 = new NumericUpDown() { Left = 50, Top = 130, Width = 400 };
inputBox2.Value = lastValue[1];
Button confirmation = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 170 };
confirmation.Click += (sender, e) => { prompt.Close();};
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.Controls.Add(inputBox1);
prompt.Controls.Add(textLabe2);
prompt.Controls.Add(inputBox2);
prompt.ShowDialog();
lastValue[0] = (int)inputBox1.Value;
lastValue[1] = (int)inputBox2.Value;
}
}
}
我在这里调用对话框:
namespace WindowsFormsApplication1
{
public partial class MainWindow : Form
{
public MainWindow()
{
InitializeComponent();
}
int[] loopParams = new int[2] { 0, 0 };
private void button1_Click(object sender, EventArgs e)
{
inputBoxDialog.ShowDialog("For Loop Begin: ", "For Loop End: ", "Popup", loopParams);
//display the result
label1.Text = "1:- " + loopParams[0]+"\r\n2:- " + loopParams[1];
}
}
}
正如Jimi所指出的,您需要将确定按钮DialogResult
属性设置为DialogResult.OK
:
Button confirmation = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 170, DialogResult = DialogResult.OK };
并检查 prompt.ShowDialog()
方法返回的 DialogResult
值:
if (prompt.ShowDialog() == DialogResult.OK)
{
lastValue[0] = (int)inputBox1.Value;
lastValue[1] = (int)inputBox2.Value;
}
这是添加了上述内容的代码:
public static void ShowDialog(string text1, string text2, string caption, int[] lastValue)
{
Form prompt = new Form();
prompt.Width = 500;
prompt.Height = 250;
prompt.Text = caption;
prompt.StartPosition = FormStartPosition.CenterScreen;
//input 1
Label textLabel = new Label() { Left = 50, Top = 20, Text = text1 };
NumericUpDown inputBox1 = new NumericUpDown() { Left = 50, Top = 50, Width = 400 };
inputBox1.Value = lastValue[0];
//input 2
Label textLabe2 = new Label() { Left = 50, Top = 100, Text = text2 };
NumericUpDown inputBox2 = new NumericUpDown() { Left = 50, Top = 130, Width = 400 };
inputBox2.Value = lastValue[1];
Button confirmation = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 170, DialogResult = DialogResult.OK };
confirmation.Click += (sender, e) => { prompt.Close(); };
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.Controls.Add(inputBox1);
prompt.Controls.Add(textLabe2);
prompt.Controls.Add(inputBox2);
if (prompt.ShowDialog() == DialogResult.OK)
{
// OK
lastValue[0] = (int)inputBox1.Value;
lastValue[1] = (int)inputBox2.Value;
}
else
{
// Cancel
}
}
检查prompt.FormClosed
事件中的prompt.DialogResult
:
prompt.FormClosed += (sender, e) =>
{
if (prompt.DialogResult == DialogResult.OK)
{
// OK
}
else
{
// Cancel
}
};
我有一个对话框,用户可以使用确定按钮输入两个整数。输入的值被保存到变量中。但是,如果我从关闭按钮关闭它,它还会保存最后输入的值,这意味着不仅 OK 按钮会保存它们。我在这里做错了什么?
对话框:
namespace WindowsFormsApplication1
{
public static class inputBoxDialog
{
public static void ShowDialog(string text1, string text2, string caption, int[] lastValue)
{
Form prompt = new Form();
prompt.Width = 500;
prompt.Height = 250;
prompt.Text = caption;
prompt.StartPosition = FormStartPosition.CenterScreen;
//input 1
Label textLabel = new Label() { Left = 50, Top = 20, Text = text1 };
NumericUpDown inputBox1 = new NumericUpDown() { Left = 50, Top = 50, Width = 400 };
inputBox1.Value = lastValue[0];
//input 2
Label textLabe2 = new Label() { Left = 50, Top = 100, Text = text2 };
NumericUpDown inputBox2 = new NumericUpDown() { Left = 50, Top = 130, Width = 400 };
inputBox2.Value = lastValue[1];
Button confirmation = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 170 };
confirmation.Click += (sender, e) => { prompt.Close();};
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.Controls.Add(inputBox1);
prompt.Controls.Add(textLabe2);
prompt.Controls.Add(inputBox2);
prompt.ShowDialog();
lastValue[0] = (int)inputBox1.Value;
lastValue[1] = (int)inputBox2.Value;
}
}
}
我在这里调用对话框:
namespace WindowsFormsApplication1
{
public partial class MainWindow : Form
{
public MainWindow()
{
InitializeComponent();
}
int[] loopParams = new int[2] { 0, 0 };
private void button1_Click(object sender, EventArgs e)
{
inputBoxDialog.ShowDialog("For Loop Begin: ", "For Loop End: ", "Popup", loopParams);
//display the result
label1.Text = "1:- " + loopParams[0]+"\r\n2:- " + loopParams[1];
}
}
}
正如Jimi所指出的,您需要将确定按钮DialogResult
属性设置为DialogResult.OK
:
Button confirmation = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 170, DialogResult = DialogResult.OK };
并检查 prompt.ShowDialog()
方法返回的 DialogResult
值:
if (prompt.ShowDialog() == DialogResult.OK)
{
lastValue[0] = (int)inputBox1.Value;
lastValue[1] = (int)inputBox2.Value;
}
这是添加了上述内容的代码:
public static void ShowDialog(string text1, string text2, string caption, int[] lastValue)
{
Form prompt = new Form();
prompt.Width = 500;
prompt.Height = 250;
prompt.Text = caption;
prompt.StartPosition = FormStartPosition.CenterScreen;
//input 1
Label textLabel = new Label() { Left = 50, Top = 20, Text = text1 };
NumericUpDown inputBox1 = new NumericUpDown() { Left = 50, Top = 50, Width = 400 };
inputBox1.Value = lastValue[0];
//input 2
Label textLabe2 = new Label() { Left = 50, Top = 100, Text = text2 };
NumericUpDown inputBox2 = new NumericUpDown() { Left = 50, Top = 130, Width = 400 };
inputBox2.Value = lastValue[1];
Button confirmation = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 170, DialogResult = DialogResult.OK };
confirmation.Click += (sender, e) => { prompt.Close(); };
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.Controls.Add(inputBox1);
prompt.Controls.Add(textLabe2);
prompt.Controls.Add(inputBox2);
if (prompt.ShowDialog() == DialogResult.OK)
{
// OK
lastValue[0] = (int)inputBox1.Value;
lastValue[1] = (int)inputBox2.Value;
}
else
{
// Cancel
}
}
检查prompt.FormClosed
事件中的prompt.DialogResult
:
prompt.FormClosed += (sender, e) =>
{
if (prompt.DialogResult == DialogResult.OK)
{
// OK
}
else
{
// Cancel
}
};