Store old value of reference type

71 Views Asked by At

I am creating custom designer for my MainTabControl (extended TabControl). In OnAddTab method I am changing TabPageCollection by adding a new tab to it.

How could I provide oldValue for RaiseComponentChanged? TabPageCollection is a reference type.

PS. I know about deep copy, but looking for better solution.

Docs:
TabPageCollection

RaiseComponentChanged

public class MainTabControlDesigner : ControlDesigner
{
    DesignerVerbCollection _verbs;
    IDesignerHost _designerHost;

    //...

    public override DesignerVerbCollection Verbs
    {
        get
        {
            if(_verbs == null)
            {
                _verbs = new DesignerVerbCollection()
                {
                    new DesignerVerb("Add Tab", new EventHandler(OnAddTab)),
                    new DesignerVerb("Remove Tab", new EventHandler(OnRemoveTab))
                };

                MainTabControl mainTabControl = Control as MainTabControl;
                if (mainTabControl != null)
                {
                    if (mainTabControl.TabPages.Count == 0) _verbs[1].Enabled = false;

                    else _verbs[1].Enabled = true;
                }
            }

            return _verbs;
        }
    }

    public void OnAddTab(object sender, EventArgs e)
    {
        MainTabControl parentControl = Control as MainTabControl;
        MainTabControl.TabPageCollection oldTabs = parentControl.TabPages;

        DesignerTransaction transaction = null;
        try
        {
            transaction = _designerHost.CreateTransaction("Add Tab");

            RaiseComponentChanging(TypeDescriptor.GetProperties(parentControl)["TabPages"]);

            MainTabPage newPage = (MainTabPage)_designerHost.CreateComponent(typeof(MainTabPage));
            newPage.Text = newPage.Name;
            parentControl.TabPages.Add(newPage);
            parentControl.SelectedTab = newPage;

            RaiseComponentChanged(TypeDescriptor.GetProperties(parentControl)["TabPages"], oldTabs, parentControl.TabPages);

            transaction.Commit();
        }
        catch
        {
            MessageBox.Show("Exception occured during adding the tab");
            transaction?.Cancel();
        }

    }

    //...
}
0

There are 0 best solutions below