Only dock control once

6 posts, 1 answers
  1. scottw
    scottw avatar
    21 posts
    Member since:
    Oct 2010

    Posted 10 Feb 2011 Link to this post

    How can I ensure that a UserControl that has been docked to an MDIForm doesn't get docked a 2nd time?  That is, I need a control to only be visible once.  I tried making it a singleton class, but calling the DockControl of the RadDock still added a new visual element, though one was an empty shell.

    I tried to find a list of controls or some other addressable way but failed.

    Thanks,

    VSmirk
  2. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 12 Feb 2011 Link to this post

    Hi VSmirk,

    Have a look at my replies in this forum post which may help you. If that's not what you mean, then let me know a bit more about what you have and I'll do my best to help.
    Thanks
    Richard
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 14 Feb 2011 Link to this post

    Hello,

    Did this help? If so please remember to mark as answer. If you need further assistance, let me know
    thanks
    Richard
  5. Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 15 Feb 2011 Link to this post

    Hi Vania,

    Like Richard said, you can find and use the solution from the given forum thread. In addition, you can use following code snippet for your scenario:

    public partial class DockForm : Form
    {
        public DockForm()
        {
            InitializeComponent();
        }
     
        private void radButton1_Click(object sender, EventArgs e)
        {
            UserControl myControl = new UserControl();
            DockControl(myControl);
        }
     
        private void DockControl(UserControl myControl)
        {
            foreach (DockWindow dockwindow in this.radDock1.DockWindows)
            {
                if (dockwindow.Controls.Count > 0 && dockwindow.Controls[0] == myControl)
                {
                    return;
                }
            }
     
            this.radDock1.DockControl(myControl, DockPosition.Fill);
        }
    }

    I hope this was helpful.

    Kind regards,
    Julian Benkov
    the Telerik team
    Q3’10 SP1 of RadControls for WinForms is available for download; also available is the Q1'11 Roadmap for Telerik Windows Forms controls.
  6. scottw
    scottw avatar
    21 posts
    Member since:
    Oct 2010

    Posted 15 Feb 2011 Link to this post

    Julian,

    Thank you for your post.  Your solution presumes the control is a singleton, and does not work otherwise as the caller of the function instantiates an entirely new instance.  But I think I can make it work.  

    While I can make the UC a singleton and avoid the creation of the control the 2nd time, I am not able to delete the static instance of the user control as I cannot find a reliable event in which to do this.

    I tried a variation on your code and tested the name rather than the instance, but it appears there is a different problem.  Closing the control does not appear to unload it from the dock window's controls.

    private void ShowLibraryMaintenance()
    {
        ucLibraryInfoBar _ucLibraryInfoBar = new ucLibraryInfoBar(_logic);
        _ucLibraryInfoBar.Name = "LibraryInfoBar";
        ShowInfoBar(_ucLibraryInfoBar);
     
        FrmLibraryMaintenance _frmLibraryMaintenance = FrmLibraryMaintenance.Instance(_logic, OnSelectedSongChanged);
        _frmLibraryMaintenance.MdiParent = this;
        _frmLibraryMaintenance.Show();
     
        //sets visibility of menu items based on access control
        SetMenuItems();
     
    }
     
    private void ShowInfoBar(UserControl libraryInfoBar)
    {
        foreach (DockWindow dockwindow in this.rdMainDock.DockWindows)
        {
            if (dockwindow.Controls.Count > 0 && dockwindow.Controls[0].Name == "LibraryInfoBar")
            {
                return;
            }
        }
        HostWindow _libHost = rdMainDock.DockControl(libraryInfoBar, DockPosition.Left);
        _libHost.DockTo(_libHost, DockPosition.Fill);
        ((ucLibraryInfoBar) libraryInfoBar).LoadControls();
        _libHost.Text = "Library";
    }

    When I close the LibraryInfoBar and refire the ShowLibraryMaintenance method, the dockwindow.Controls.Count is still greater than 0. 


    Thank you!
  7. Answer
    Julian Benkov
    Admin
    Julian Benkov avatar
    1135 posts

    Posted 18 Feb 2011 Link to this post

    Hi VSmirk,

    In this case you can change the close action of created HostWindow:

    ...
     
    HostWindow _libHost  = rdMainDock.DockControl(libraryInfoBar, DockPosition.Left);
    _libHost.CloseAction = DockWindowCloseAction.CloseAndDispose;
     
    ...

    Kind regards,
    Julian Benkov
    the Telerik team
Back to Top
UI for WinForms is Visual Studio 2017 Ready