我必须多次单击MessageBox按钮才能从消息框中退出

时间:2012-11-18 09:52:51

标签: c# winforms

退出按钮单击“事件”。

`void buttn2_Click(object sender, EventArgs e) //QUIT BUTTON CLICK EVENT. 
    {
       if (MessageBox.Show("LEAVE CURRENT GAME?", "QUIT CONFIRMATION", MessageBoxButtons.YesNo) == DialogResult.Yes)
        {
            this.Controls.Remove(buttn); .. PLAY AGAIN BUTTON.
            this.Controls.Remove(buttn2);
            for (int i = 0; i <= gencount; i++)
            {
               this.Controls.Remove(panel[i]);
               this.Controls.Remove(label200[i]);
               this.Controls.Remove(label100[i]);
               this.Controls.Remove(Tbox[i]);
            }
               this.Controls.Remove(AttemptsRem);
               this.Controls.Remove(AttemptNum);
               this.Controls.Remove(TimeRem);
               this.Controls.Remove(Min);
               this.Controls.Remove(Sec);
               this.Controls.Remove(misc);
               this.ReftoForm2.Show(); To go back to the starting form 
        }
        else
            buttn.Focus();
    }

Form1激活事件。

 private void Form1_Activated(object sender, EventArgs e)
    {
        if (ui_formCowsAndBulls.rdbSinglePlayer.Checked == true)//Static variable
        {

            //GetAllTheWords(); .. Am still working on getting a the 4 letter words
            //GetDistinctElements(); .. randomly out of a list.
            textBox1.PasswordChar = '*';
            textBox1.Focus();
            foreach (string val in distinctWords)
            {
                if (val == "ABLE") .. For single player,the guess word is ABLE.
                    textBox1.Text = val;
            }
        }
        else
        {
            textBox1.Text = string.Empty;
            textBox1.Enabled = true;
            textBox1.Focus();
        }
        //textBox1.Focus();
    }

再玩点击事件

 private void buttn_Click(object sender, EventArgs e) //PLAY AGAIN CLICK EVENT.
    {
        for (int i = 0; i <= gencount; i++)
          {
            this.Controls.Remove(panel[i]);
            this.Controls.Remove(label200[i]);
            this.Controls.Remove(label100[i]);
            this.Controls.Remove(Tbox[i]);
          }

            this.Controls.Remove(AttemptsRem);
            this.Controls.Remove(AttemptNum);
            textBox1.Text = string.Empty;
            textBox1.Enabled = true;
            textBox1.Focus();
            incrpanel = 0; gencount = 0; count = 10;
            this.Controls.Remove(TimeRem);
            this.Controls.Remove(Min);
            this.Controls.Remove(Sec);
            this.Controls.Remove(misc);
            this.textBox1.PasswordChar = '*';
            this.Controls.Remove(buttn);
            this.Controls.Remove(buttn2);
    }

我的问题是,当我点击消息框按钮NO时,我不会从消息框中走出来。我第一次玩游戏时出现在消息框中,但是如果我第二次玩游戏,那就需要我从消息框中点击两次。如果我第三次玩游戏,我会在消息框中点击“是”或“否”按钮3次。我希望大家可以帮助我。我发布了之前的相同问题,但没有代码。希望代码有所帮助。

1 个答案:

答案 0 :(得分:1)

我想您可能正在订阅Click - 事件:

button1.Click += new EventHandler(buttn2_Click);

在代码中不止一次被调用的位置,因此当单击该按钮时,将显示MessageBox,并且当buttn2_Click事件处理程序(您发布的代码)完成时 - 它将再次运行,显示另一个MessageBox,与订阅(上面的“... + = ...”)完成的次数相同。