单击按钮后关闭新弹出窗体时变量不更新

时间:2014-01-02 18:35:19

标签: c# button popup

我正在尝试创建一个按钮,当单击时弹出一个带有yes和no按钮的新表单,然后根据按下的按钮创建一个if语句。这是我目前的代码:

        private YesNoMessageBoxResized newBoxResized;
        private string buttonClickResult;

        public void YesNoNewMessageBox(string title, string message,string buttonYes, string buttonNo)
        {
            YesNoMessageBoxResized msgResized = new YesNoMessageBoxResized(title, message, buttonYes, buttonNo);
            msgResized.StartPosition = FormStartPosition.CenterScreen;
            msgResized.TopMost = true;

            Button yesButtonResize = new Button();
            Button noButtonResize = new Button();

            //yes button
            yesButtonResize.Text = buttonYes;
            yesButtonResize.Size = new Size(150, 80);
            yesButtonResize.Font = new Font("Arial", 26);
            yesButtonResize.Location = new Point(100, 150);

            //no button
            noButtonResize.Text = buttonNo;
            noButtonResize.Size = new Size(150, 80);
            noButtonResize.Font = new Font("Arial", 26);
            noButtonResize.Location = new Point(300, 150);

            //make a copy of the current form
            newBoxResized = msgResized;

            //eventhandlers
            yesButtonResize.Click += YesButtonResizeClicked;
            noButtonResize.Click += noButtonResizeClicked;

            newBoxResized.Controls.Add(yesButtonResize);
            newBoxResized.Controls.Add(noButtonResize);
            msgResized.Show();
        }

        private void YesButtonResizeClicked(object o, EventArgs sEA)
        {
            this.buttonClickResult = "true";
            this.newBoxResized.Close();
        }

        private void noButtonResizeClicked(object o, EventArgs sEA)
        {
            this.buttonClickResult = "false";
            this.newBoxResized.Close();
        }

        private void buttonRestoreDefaults_Click(object sender, EventArgs e)
        {

            YesNoNewMessageBox("Restore Defaults?", "Restore Defaults?", "Yes", "No");

            if (this.buttonClickResult == "true")
                this.restoreDefaults();

        }

我的问题是,在点击是并关闭弹出的表单后,buttonClickResult不会被视为true,因此我调用的恢复默认函数不会被调用。只有在再次单击“RestoreDefaults”按钮时才会调用该函数。因此,似乎buttonRestoreDefaults_Click的onclick事件没有重新启动onclick,弹出窗口中的yes或no按钮,直到再次单击它为止。有没有办法绕过这个或某种实现来解决这个问题?谢谢。

此外,这是该类的代码。我正在考虑使用委托和事件处理程序,但我不确定我是否真的需要它,因为我的工作,但只是没有正确关闭时更新变量:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

    public class YesNoMessageBoxResized : Form
    {
        private Label labelMessage;

        //no default button specified
        public YesNoMessageBoxResized(string title, string message, string buttonYes, string buttonNo)
        {
            InitializeComponent();
            this.Text = title;
            this.labelMessage.Text = message;
            this.Deactivate += MyDeactivateHandler;
        }

        public YesNoMessageBoxResized()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.labelMessage = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // labelMessage
            // 
            this.labelMessage.AutoSize = true;
            this.labelMessage.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.labelMessage.Location = new System.Drawing.Point(12, 31);
            this.labelMessage.Name = "labelMessage";
            this.labelMessage.Size = new System.Drawing.Size(165, 29);
            this.labelMessage.TabIndex = 3;
            this.labelMessage.Text = "labelMessage";
            // 
            // YesNoMessageBoxResized
            // 
            this.ClientSize = new System.Drawing.Size(572, 268);
            this.Controls.Add(this.labelMessage);
            this.Name = "YesNoMessageBoxResized";
            this.ResumeLayout(false);
            this.PerformLayout();

        }



        protected void MyDeactivateHandler(object sender, EventArgs e)
        {
            this.Close();
        }

        public delegate void buttonYes_ClickResultEvent(object o, EventArgs sEA);
        public event buttonYes_ClickResultEvent choiceResult;

    }
 public class buttonYes_ClickResultEvent : EventArgs
 {
     public buttonYes_ClickResultEvent(bool choice)
     {
         this.buttonResult = choice;
     }

     public bool buttonResult;
 }

**我在codereview上发布了这个,但后来他们告诉我在这里发布它,因为它处理解决问题。

4 个答案:

答案 0 :(得分:0)

仅用于测试,尝试使用全局变量并在yesbuttonresizedclicked事件中将其设置为true。

类似

    public static bool yestest = false;
    private void YesButtonResizeClicked(object o, EventArgs sEA)
        {

    yestest = true;
    }

答案 1 :(得分:0)

我通过添加一个带有if语句和其他按钮事件的函数来修复它。我对其他解决方案持开放态度:

    private YesNoMessageBoxResized newBoxResized;
    private string buttonClickResult;

    public void YesNoNewMessageBox(string title, string message,string buttonYes, string buttonNo)
    {
        YesNoMessageBoxResized msgResized = new YesNoMessageBoxResized(title, message, buttonYes, buttonNo);
        msgResized.StartPosition = FormStartPosition.CenterScreen;
        msgResized.TopMost = true;

        Button yesButtonResize = new Button();
        Button noButtonResize = new Button();

        //yes button
        yesButtonResize.Text = buttonYes;
        yesButtonResize.Size = new Size(150, 80);
        yesButtonResize.Font = new Font("Arial", 26);
        yesButtonResize.Location = new Point(100, 150);

        //no button
        noButtonResize.Text = buttonNo;
        noButtonResize.Size = new Size(150, 80);
        noButtonResize.Font = new Font("Arial", 26);
        noButtonResize.Location = new Point(300, 150);

        //make a copy of the current form
        newBoxResized = msgResized;

        //eventhandlers
        yesButtonResize.Click += YesButtonResizeClicked;
        noButtonResize.Click += noButtonResizeClicked;


        newBoxResized.Controls.Add(yesButtonResize);
        newBoxResized.Controls.Add(noButtonResize);
        newBoxResized.Show();
    }

    private void YesButtonResizeClicked(object o, EventArgs sEA)
    {
        this.buttonClickResult = "true";
        this.DoIfStatement();
        this.newBoxResized.Close();
    }

    private void noButtonResizeClicked(object o, EventArgs sEA)
    {
        this.buttonClickResult = "false";
        this.DoIfStatement();
        this.newBoxResized.Close();
    }

    private void DoIfStatement()
    {
        if (buttonClickResult == "true")
            this.restoreDefaults();
    }

    private void buttonRestoreDefaults_Click(object sender, EventArgs e)
    {
        YesNoNewMessageBox("Restore Defaults?", "Restore Defaults?", "Yes", "No");
    }

我认为我需要它更加模块化。我并不总是希望那里有相同的if语句。因此,对于恢复默认值,我并不总是希望成为是和否。我可能希望它使用另一个函数调用执行其他操作并且使用yes而没有按钮执行其他操作。

答案 2 :(得分:0)

您可以使用MessageBox为您执行此操作:

if (MessageBox.Show("Yes or no?", "", MessageBoxButtons.YesNo)
    == DialogResult.Yes)

答案 3 :(得分:0)

您可以将表单更改为模式对话框表单。使AcceptCancel按钮属性等于相应的按钮。然后将每个按钮的对话框结果更改为适当的值。现在,当您使用ShowDialog时,表单将返回相应的DialogResult