关闭按钮也可以保存数值

问题描述 投票:0回答:1

我有一个对话框,用户可以通过 "确定 "按钮输入两个整数。输入的值被保存到变量中。然而,如果我通过关闭按钮关闭它,它也会保存最后输入的值,这意味着不仅是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];
        }
    }
}
c# forms popup parent-child
1个回答
2
投票

正如指出的 集美,你需要设置 "确定 "按钮 DialogResult 属性为 DialogResult.OK:

Button confirmation = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 170, DialogResult = DialogResult.OK };

并检查返回的数据 DialogResult 价值从 prompt.ShowDialog() 方法。

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.DialogResultprompt.FormClosed 事件。

prompt.FormClosed += (sender, e) => 
{
    if (prompt.DialogResult == DialogResult.OK)
    {
        // OK 
    }
    else
    {
        // Cancel
    }
};
© www.soinside.com 2019 - 2024. All rights reserved.