isx
isx

Reputation: 111

WinForms: Trouble Switching UserControl TabPages of TabControl in Designer

I have some problem and need a help. I create UserControl with TabControl with some TabPages. When I place it into form, I cant switch pages in designer, but into designer of UserControl its work.

By this code I can place into TabPages in form designer some elements, but not switch pages.

    [Designer(typeof(TestUserControlDesigner))]
    public partial class Test : UserControl
    {
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public TabControl TabControl1
        {
            get { return this.tabControl1; }
        }
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public TabControl TabControl2
        {
            get { return this.tabControl2; }
        }


        public Test()
        {
            InitializeComponent();
        }
    }

    internal class TestUserControlDesigner : ParentControlDesigner
    {
        public override void Initialize(System.ComponentModel.IComponent component)
        {
            base.Initialize(component);

            var uc = (Test)component;

            var ctl = uc.TabControl1 as TabControl;
            EnableDesignMode(ctl, ctl.Name);
            foreach (TabPage page in ctl.TabPages) EnableDesignMode(page, page.Name);

            ctl = uc.TabControl2 as TabControl;
            EnableDesignMode(ctl, ctl.Name);
            foreach (TabPage page in ctl.TabPages)
            {
                EnableDesignMode(page, page.Name);
            }
        }
    }

@Hans Passant it not work. I copy code to my control, but pages not view in designer. Also when I try it by note, I have error, because tabPage1 is not found.

namespace ControlsLibrary { [Designer(typeof(UserControl1Designer))] public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); } [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] public TabControl MyTabControl { get { return this.tabControl1; } } [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] public TabPage T1 { get { return this.tabPage1; } }

    private void InitializeComponent()
    {
        this.tabControl1 = new System.Windows.Forms.TabControl();
        this.SuspendLayout();
        this.tabControl1.Name = "tabControl1";
        this.tabControl1.Dock = DockStyle.Fill;
        this.Controls.Add(this.tabControl1);
        this.Name = "UserControl1";
        this.ResumeLayout(true);
    }
    private System.Windows.Forms.TabControl tabControl1;
}

public class UserControl1Designer : ParentControlDesigner
{
    public override void Initialize(System.ComponentModel.IComponent component)
    {
        base.Initialize(component);
        this.EnableDesignMode(((UserControl1)this.Control).MyTabControl, "MyTabControl");
        this.EnableDesignMode(((UserControl1)this.Control).T1, "T1");
    }
    public override void InitializeNewComponent(System.Collections.IDictionary values)
    {
        base.InitializeNewComponent(values);
        AddTab();
        AddTab();
    }
    private void AddTab()
    {
        TabControl tabControl = ((UserControl1)this.Control).MyTabControl;
        var svc = (IDesignerHost)this.GetService(typeof(IDesignerHost));
        if (svc != null)
        {
            var tab1 = (TabPage)svc.CreateComponent(typeof(TabPage));
            tab1.Text = tab1.Name;
            tab1.UseVisualStyleBackColor = true;
            tabControl.TabPages.Add(tab1);
            var property = TypeDescriptor.GetProperties(tabControl)["Controls"];
            base.RaiseComponentChanging(property);
            base.RaiseComponentChanged(property, null, null);
        }
    }
}

} enter image description here

Upvotes: 0

Views: 31

Answers (0)

Related Questions