none
C#: How to disable a form when another form is open? RRS feed

  • Question

  • Hi everyone,

    What I mean with my question is I have two forms, LoginForm and ChangePassForm. What I want is when I click 'Change Password' button on the LoginForm which it means it will open ChangePassForm, then I want the LoginForm is disabled, it keeps showed but it is disabled. I mean I want to "force" user to fill the ChangePassForm so they cann't click or do anything with the LoginForm when ChangePassForm is open. So, how can I do that?


    • Edited by The_C Tuesday, December 1, 2015 11:41 AM
    Tuesday, December 1, 2015 11:37 AM

Answers

  • does the ChangePassForm.ShowDialog() not work?


    DON'T TRY SO HARD,THE BEST THINGS COME WHEN YOU LEAST EXPECT THEM TO.

    • Edited by Matthew LEAN . D Tuesday, December 1, 2015 12:17 PM
    • Marked as answer by The_C Tuesday, December 1, 2015 12:21 PM
    Tuesday, December 1, 2015 12:14 PM
  • The Form has Enabled property that you can use to disable the Form and all its child controls.

    Let's say I have Form1 and Form2, so from Form1 button click I will show Form2 and disable current Form:

     private void btnCancel_Click(object sender, EventArgs e)
            {
                Form2 frm = new Form2(this);
                this.Enabled = false;
                frm.Show();
            }

    But you will need to enable this Form again when the user closes Form2 for example. Since the Form is a class instance we need that same instance to enable it again.

    So in Form2 we create a constructor that accepts Form1 as input arg as below and we use it to initialize a class level var:

     private Form _frm;
            public Form1(Form1 frm)
            {
                _frm = frm;
                InitializeComponent();
            }

    and from Form Closed event or another event or function to enable Form1 again:

    private void Form2_FormClosed(object sender, FormClosedEventArgs e)
            {
                _frm.Enabled = true;
            }


    Fouad Roumieh

    • Marked as answer by The_C Tuesday, December 1, 2015 1:18 PM
    Tuesday, December 1, 2015 12:32 PM

All replies

  • does the ChangePassForm.ShowDialog() not work?


    DON'T TRY SO HARD,THE BEST THINGS COME WHEN YOU LEAST EXPECT THEM TO.

    • Edited by Matthew LEAN . D Tuesday, December 1, 2015 12:17 PM
    • Marked as answer by The_C Tuesday, December 1, 2015 12:21 PM
    Tuesday, December 1, 2015 12:14 PM
  • yes, it works sir... thanks for the answer...
    Tuesday, December 1, 2015 12:21 PM
  • The Form has Enabled property that you can use to disable the Form and all its child controls.

    Let's say I have Form1 and Form2, so from Form1 button click I will show Form2 and disable current Form:

     private void btnCancel_Click(object sender, EventArgs e)
            {
                Form2 frm = new Form2(this);
                this.Enabled = false;
                frm.Show();
            }

    But you will need to enable this Form again when the user closes Form2 for example. Since the Form is a class instance we need that same instance to enable it again.

    So in Form2 we create a constructor that accepts Form1 as input arg as below and we use it to initialize a class level var:

     private Form _frm;
            public Form1(Form1 frm)
            {
                _frm = frm;
                InitializeComponent();
            }

    and from Form Closed event or another event or function to enable Form1 again:

    private void Form2_FormClosed(object sender, FormClosedEventArgs e)
            {
                _frm.Enabled = true;
            }


    Fouad Roumieh

    • Marked as answer by The_C Tuesday, December 1, 2015 1:18 PM
    Tuesday, December 1, 2015 12:32 PM
  • this is the answer i need... thanks sir...
    Tuesday, December 1, 2015 1:19 PM