John Pietrar
John Pietrar

Reputation: 543

Start closing event on form 1 from form 2

I have a form 1 design with a Close button on it,when the close button is pressed u get a textbox and an ok button on another form (form 2),when u enter the password 1234 in the textbox and press the ok button I want it to close the form 2 and , close and e.cancel = false; the form 1 because e.cancel = true; in the Form1_FormClosing method.

Form 1

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

Form 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
    {               

    }
}

Bottom line, I want something like

 if (textBox1.Text == "1234")
 {      
    Close();
    e.Cancel=false;
    Form1.Close();
 }

Don't hate me if it's easy to do but I'm really new to C# programming.

Upvotes: 1

Views: 955

Answers (2)

Beldi Anouar
Beldi Anouar

Reputation: 2180

on form1 :

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2(this);
            f2.ShowDialog();
        }        
    }
}

On form2 :

  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 ;
        }
    }

Upvotes: 1

Pranav Patel
Pranav Patel

Reputation: 1559

try like this

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();
            }
        }

Upvotes: 3

Related Questions