سؤال

I have an encapsulated control in a derived TabPage, and I am having trouble docking it to the form MainForm that contains the derived TabPage.

I've added the TabPage and the control correctly, and they are showing up on MainForm. However, the control is not correctly docked (style: fill). You can verify this by resizing the form.

I've set the property _control.Dock = System.Windows.Forms.DockStyle.Fill and _control.Anchor = Left | Right | Top | Bottom in the derived TabPage's constructor.

Below is the sample of the code:

public class DerivedTab : TabPage {
    public DerivedTab(){
        ...
        _control= new BrightIdeasSoftware.TreeListView();
        this.Controls.Add(this._control);
        _control.Anchor = System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right | System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom;
        _control.Dock = System.Windows.Forms.DockStyle.Fill;
        ...
    }
}

In the MainForm, this is the function that creates the derived tab in run-time:

    private DerivedTab CreateTab(string name)
    {
        DerivedTab tab = new DerivedTab(this, name);
        tab.SuspendLayout();

        MainTab.Controls.Add(tab);
        return tab;
    }

Anyways, I generated this code using Visual Studio Designer, and the control docks correctly. I, then, created a class for the tabpage (since I will need many and varying number of these tabs) and copied everything relates to tabpage to the CreateTab function. I moved anything relates to the the property of the tabpage into its constructor. I encapsulated the control in the derived TabPage and moved everything related to the control in the derived tabpage.

So, what I am missing? Is it possible to dock the encapsulated control without implementing an eventhandler function?

هل كانت مفيدة؟

المحلول

I found the solution to my own problem. ResumeLayout has to be called after the TabControl adds the TabPage for the TabPage to dock properly.

private DerivedTab CreateTab(string name)
{
    DerivedTab tab = new DerivedTab(this, name);
    tab.SuspendLayout();

    MainTab.Controls.Add(tab);
    tab.ResumeLayout();
    return tab;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top