从表单2开始关闭表单1上的事件

时间:2016-06-02 07:56:57

标签: c# winforms

我有一个带有关闭按钮的表单1设计,当按下关闭按钮时,当你在文本框中输入密码1234并按下时,在另一个表单(表单2)上获得一个文本框和一个确定按钮确定按钮我希望它关闭表单2,并关闭和e.cancel = false;形式1因为e.cancel = true;在Form1_FormClosing方法中。

表格1

   private void Form1_FormClosing(object sender, FormClosingEventArgs e)
   {                   
           e.Cancel = true;
   }

表格2

public string MyProperty { get; set; }
private void password_Click(object sender, EventArgs e)
{
    Form2 mc = new Form2();
    mc.MyProperty = textBox1.Text;

    if (textBox1.Text == "1234")
    {             
        Close();              
    }
    else
    {               

    }
}

底线,我想要像

这样的东西
 if (textBox1.Text == "1234")
 {      
    Close();
    e.Cancel=false;
    Form1.Close();
 }
如果它很容易做到,那就不要讨厌我,但我对C#编程并不陌生。

2 个答案:

答案 0 :(得分:4)

试试这个

on form1

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Form2 frm = new Form2();
            if (frm.ShowDialog() == DialogResult.Cancel)
            {
                e.Cancel = true;
            }
        }

on form2

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "12345")
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            }
        }

答案 1 :(得分:1)

form1上的

  public partial class Form2 : Form
    {
        Form1 frm;
        public Form2(Form1 frm)
        {
            InitializeComponent();
            this.frm = frm;
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "12345")
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
                frm.FormClosing += new System.Windows.Forms.FormClosingEventHandler(Form1_FormClosingFalse);
               frm.Close();
            }
            else
            {
                this.DialogResult = DialogResult.Cancel;
                this.Close();
                frm.FormClosing += new System.Windows.Forms.FormClosingEventHandler(Form1_FormClosingTrue);

            }
        }

        public void Form1_FormClosingTrue(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
        }
        public void Form1_FormClosingFalse(object sender, FormClosingEventArgs e)
        {
            e.Cancel = false ;
        }
    }

在form2上:

html { cursor:wait }