AmilaDG
AmilaDG

Reputation: 186

How to Disable the Parent Form When a Child Form is Active Within MDI Parent Form?

How to Disable the Parent Form When a Child Form is Active and Within MDI Parent Form?

'Add Product' is a child of MDI Parent Form, and 'Add Category' is child of 'Add Product' form. I already bind 'Add Category' to MDI Parent Form by using following code

        frm_Add_Category obj_AddCategory = new frm_Add_Category();
        obj_AddCategory.MdiParent = this.MdiParent;
        obj_AddCategory.Show();

Now it is not going out of the border of MDI Parent Form. Next what I have to do is make 'Add Product' disable when 'Add Category' form pop up. I look through all over the web but when I fix this 'Add Category' is going out to the MDI Parent Form. I already tried all methods which explained here.

As a summery what I want to do is

  1. 'Add Category' Cannot go out from the MDI Parent Form.
  2. 'Add Product' should disable/un-clickable when 'Add Category' pops.
  3. 'Add Product' should enable/clickable when 'Add Category' closed.

Upvotes: 0

Views: 4445

Answers (2)

Idle_Mind
Idle_Mind

Reputation: 39142

For an MdiChild to be "modal" you have to simulate it by disabling everything else manually, then re-enabling them when that form is closed.

Quick example:

    // ... running from within an MdiChild ...        

    private void button1_Click(object sender, EventArgs e)
    {
        foreach (Form child in this.MdiParent.MdiChildren)
        {
            child.Enabled = false;
        }

        Form3 f3 = new Form3();
        f3.MdiParent = this.MdiParent;
        f3.FormClosed += new FormClosedEventHandler(f3_FormClosed);
        f3.Show();
    }

    void f3_FormClosed(object sender, FormClosedEventArgs e)
    {
        foreach (Form child in this.MdiParent.MdiChildren)
        {
            child.Enabled = true;
        }
    }

This will be different than a normal modal dialog, though, because it won't blink when you attempt to click on other forms in the application.

Upvotes: 1

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73502

It is not a good good idea to disable a free floating form(Add Product), because when user clicks on it it won't respond. May introduce a wierd feeling to user that program struck or something like that.

So if you want to prevent user from accessing(Add Product) when Add Category is showing then you could do this by showing Add Category as modal.

Try this

 using(frm_Add_Category obj_AddCategory = new frm_Add_Category())
 {
     if(obj_AddCategory.ShowDialog(this) == DialogResult.Ok)
     {
         //Save success
     }
     else
     {
         //Save cancelled
     }
 }

On a side note don't name variables and Classes like this. looks ugly. If I were doing it, I would have named like this. This also may be not be the best, but better than former version. Let's see any suggestions on this.

 using(AddCategoryForm addCategory = new AddCategoryForm())
 {
     if(addCategory.ShowDialog(this) == DialogResult.Ok)
     {
         //Save success
     }
     else
     {
         //Save cancelled
     }
 }

Upvotes: 1

Related Questions