Search code examples
c#.netvisual-studioanchorparent

Fix Anchoring when changing parent


I have a RichTextBox, a Button and a List Box with Anchor set to Top, Bottom, Right.
Whenever I change the form size and then change their parent (from one tab of a TabControl to another) their position gets messed up.

http://imgur.com/WiQhRJU,FyFIQTL,7K7HMTt,Nqrm20y

First image: Before re-size ✓
Second image: After re-size ✓
Third image: After re-size on different tab ✖
Fourth image: Switched back to original tab ✖

This is the code i use to change the parent:

private void tabTabs_Selected(object sender, TabControlEventArgs e)
{
    if (tabTabs.SelectedTab == tabChat || tabTabs.SelectedTab == tabCmd)
    {
        lstUsers.Parent = tabTabs.TabPages[tabTabs.SelectedIndex];
        rtbSend.Parent = tabTabs.TabPages[tabTabs.SelectedIndex];
        btnSend.Parent = tabTabs.TabPages[tabTabs.SelectedIndex];
    }
}

Solution

  • I know I'm a year late, but I just had this same problem with tabs and anchoring. How I fixed it was by adding a panel that contains all of the elements and setting Dock to Fill. Then by only changing that container panel's parent, it worked flawlessly. In reality this is a bug that should be reported to MS, but for now this workaround works wonderfully.

    Edit: You can also reduce/remove flicking when changing tabs by double buffering all of the controls including the tab group, tab pages, container panels, and all of the elements inside the container panel.