0

Adding a control that has it's Dock property set to DockStyle.Fill outside of the form's InitializeComponent() function doesn't consider the adjacent controls in the form.

In this case my DataGridView control is being underlapped by my MenuStrip control. Data Grid View Overlapping with Tool strip menu

I assume it has something to do with SuspendLayout() ,ResumeLayout(false), and PerformLayout() and I've tried mimicking the function calls done in InitializeComponent() as so:

            mainForm = new Form1();
            //add post-init control
            mainForm.menuStrip1.SuspendLayout();
            mainForm.SuspendLayout();
            mainForm.TableMainGridView = mainForm.GetGridView();
            ((System.ComponentModel.ISupportInitialize)(mainForm.TableMainGridView)).BeginInit();
            mainForm.Controls.Add(mainForm.TableMainGridView);
            ((System.ComponentModel.ISupportInitialize)(mainForm.TableMainGridView)).EndInit();
            mainForm.menuStrip1.ResumeLayout(false);
            mainForm.menuStrip1.PerformLayout();
            mainForm.ResumeLayout(false);
            mainForm.PerformLayout();

But it doesn't change anything.

After some more digging it seems that SuspendLayout() and ResumeLayout() are only used for the performance of forms with many controls.
PerformLayout() is supposed to "force the control to apply layout logic to all its child controls." but as to what this "layout logic" is remains a mystery to me.

DED
  • 391
  • 2
  • 12
  • It's not really clear what this code: `mainForm.TableMainGridView = mainForm.GetGridView();` is doing or what it's supposed to do. Do you have a DataGridView in this Form already? Are you adding it to the Form.Controls collection again? Or was it `null`? What is `GetGridView();` doing? In any case, why to the Form.Controls collection directly? You should use a docked Panel container or TableLayoutPanel to host new child controls with this kind of layout (multiple docked objects, apparently) otherwise use `BringToFront()`/`SnedToBack()` to set the z-order (which is part of the *layout logic*). – Jimi Apr 20 '20 at 06:18
  • @Jimi it just returns a new dgv object with all of it's formatting/settings (I moved it out of the form class to be a static function instead). The point is that i'm adding this one dgv control after `InitializeComponent()` which i assume happens when a new form object is created. If there's no fix for this I'll just have to have the same code twice, once in `GetGridView()` and once in `InitializeComponent()`. If i call `GetGridView()` from `InitializeComponent()` it works but i can no longer use the [Design] window. – DED Apr 20 '20 at 07:09
  • The Panel/Container would probably work, forgot that was a thing, but I might as well just have duplicate code if there's any disadvantage to using it, I'll test it out. – DED Apr 20 '20 at 07:11
  • 1
    Forget about `InitializeComponent()`, this is related to how Docking and z-order/z-index work and combine. Controls with lower z-index have higher z-order. See the quick notes [here](https://stackoverflow.com/a/58469851/7444103) and [here](https://stackoverflow.com/a/51476266/7444103). It's still not clear what `TableMainGridView` is when the Form is created. Is it an already Docked Control or a just Field set to `null`? – Jimi Apr 20 '20 at 07:23
  • it's a null field. – DED Apr 20 '20 at 07:37

0 Answers0