0

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.
Community
  • 1
  • 1
AmilaDG
  • 186
  • 1
  • 3
  • 18
  • from what I see in your screen shot, I don't think **the `Add Category` form is a child of `Add Product` form** – King King Sep 25 '13 at 13:36
  • @KingKing There is a button called 'Add New Category' in 'Add Product'. – AmilaDG Sep 25 '13 at 13:40
  • Is `AddCategory` shown form modal? – Sriram Sakthivel Sep 25 '13 at 13:44
  • Yes, that doesn't mean `Add Category` is a child of `Add Product`. The `Add Product` shows the `Add Category`. Simply you should pass the `Add Product` as the owner into `Show()` method and then you can access to the `Add Product` form in the `Add Category` form via the property `Owner` and set the `Enabled` property to `false` like this `Owner.Enabled = false;` – King King Sep 25 '13 at 13:44
  • @KingKing Already tried that. Once I do that 'Add Product' stays behind 'Add Category' without disabling. And it is closable while 'Add Category' is there. – AmilaDG Sep 25 '13 at 13:50
  • For MDI, there is no built-in notion of parents between children or modal dialogs as they are all simply children of the main MDI container. If you want both to be proper MdiChildren, but have Add Category be "modal", then you have to SIMULATE it by disabling all other MdiChildren (and possibly elements of the main MDI container like menus...depends on your design) when that form is opened. Subscribe to the FormClosed() event so you can re-enable everything when it is closed. – Idle_Mind Sep 25 '13 at 14:13
  • Try using `obj_AddCategory.ShowDialog();` – Diana Prodan Feb 04 '17 at 17:49

2 Answers2

1

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
     }
 }
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • `ShowDialog` works only for non-toplevel form. The `AddCategory` form has the `main form` as its `Mdi Parent` so this doesn't work. I also tried it. – King King Sep 25 '13 at 13:57
  • @KingKing You should note that I removed that `MdiParent` that is redundant here. :) – Sriram Sakthivel Sep 25 '13 at 14:00
  • However that may be not what the OP wants. The idea of *putting all the child forms* into the main *Mdi parent* is different from using any dialog. – King King Sep 25 '13 at 14:02
  • @KingKing You should note that OP is asking to disable `Parent` when a child is shown. *If disabled is there any way to open another child from disabled parent ?* So, Isn't this solution relevant for expected output? – Sriram Sakthivel Sep 25 '13 at 14:05
  • In fact the solution is very simple. The idea is we have to get access to the parent from `Add Category` and simply use the `Enabled` property. I've tried that and it works. Using `Owner` to pass `Parent` to the `Category` won't work but we **have tons of ways to pass object from this class to another class**. – King King Sep 25 '13 at 14:11
1

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.

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • When i write it, it gives me an error, the error be like { System.NullReferenceException: 'Object reference not set to an instance of an object.' System.Windows.Forms.Form.MdiParent.get returned null. } – Feezan Khattak Jun 12 '21 at 05:13
  • @FeezanKhattak The comment at the top of my code clearly says, `... running from within an MdiChild ...`. If you're not in an MdiChild, then why would you expect "MdiParent" to give you anything back? – Idle_Mind Jun 12 '21 at 22:19