1

I am adding to a MDI a side bar form (not a child) and docking it to the right. The top of the side bar appears to overlapping the toolbar. When doing a manual resizing (by dragging), the side bar is "magically" looks fine.

Before resizing:

enter image description here

After Resizing:

enter image description here

public partial class MDI : Form
{
    private void InitGenealogicalTree()
    {
        var form = new GenealogicalTreeForm();            
        form.TopLevel = false;
        form.WindowState = FormWindowState.Normal;
        form.Dock = DockStyle.Right;
        this.Controls.Add(form);
    }
}

Note: The ToolStrip was added in design mode.

How can I properly add the side bar form without having it overlapping the toolbar?

ehh
  • 3,412
  • 7
  • 43
  • 91

2 Answers2

1

A side-bar windows should not be movable and should not be resizeable and should not be maximize-able, so:

  • Set FormBorderStyle to FixedToolWindow.
  • Override WndProc of the side-bar window WndProc to prevent move and prevent maximize:

    protected override void WndProc(ref Message m)
    {
        const int WM_SYSCOMMAND = 0x0112;
        const int SC_MOVE = 0xf010;
        const int SC_MAXIMIZE = 0xf030;
        if (m.Msg == WM_SYSCOMMAND)
        {
            int command = m.WParam.ToInt32() & 0xfff0;
            if (command == SC_MOVE || command == SC_MAXIMIZE)
                return;
        }
        base.WndProc(ref m);
    }
    
  • Also when adding the side-bar, bring it to front. It means first toolbar should be docked, then the side-bar windows will be docked in the remaining area:

    this.Controls.Add(sideBar);
    sideBar.BringToFront();
    
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Wow, thank you for open mind. I didn't though how I will prevent from moving it. Thanks – ehh Jan 23 '19 at 14:20
  • You're welcome :) For such multi-window UI, I prefer to not use MDI. Instead, create a multi-tab UI. Each child form will be added as borderless child in a new Tab, you can show [close button on tab](https://stackoverflow.com/a/36900582/3110834). The sidebar is also a form which is hosted in a docked panel to the right. You can also use a splitter between the side panel and the tabcontrol to allow resizing the sidebar. – Reza Aghaei Jan 23 '19 at 14:24
0

Just put a panel in desigenermode where you want to have your sidepanel and display the sidepanel in the panel.


The following codes lets you display the side bar form in the panel:

var form = new GenealogicalTreeForm();            
form.TopLevel = false;
panel.Controls.Add(form);
form.Dock = DockStyle.Fill;
this.Controls.Add(form);
form.Show();
Christoph Blüm
  • 925
  • 1
  • 9
  • 23