Disable context menu for Document Window

3 posts, 0 answers
  1. Deepak Shakya
    Deepak Shakya avatar
    118 posts
    Member since:
    Aug 2007

    Posted 02 Dec 2009 Link to this post

    I am using Telerik RadControls for WinForms Q3 2009 (v2009.3.9.1103). How do I disable the context menu that allows user to close the document.

    • My requirement is that the user should not be able to close any of the document window.

    I have hidden the Close button but the user can still access the context menu.

    Is there any solution for this issue or this is just a part of the design?

  2. Georgi
    Admin
    Georgi avatar
    296 posts

    Posted 03 Dec 2009 Link to this post

    Hi Deepak,

    Thank you for the interesting question.

    There are two approaches here:

    - The easiest one is to use DockWindow.AllowedDockState property and remove the Hidden bit:

    this.documentWindow1.AllowedDockState &= ~AllowedDockState.Hidden;

    This will prevent the window from being closed.

    - The other one is to exploit the context menu system itself.

    Since Q3 2009 all context menu related operations are handled by a stand alone service, registered with RadDock - ContextMenuService. Each context menu request is passed to the service, which on its hand creates the appropriate menu items and raises several events, which allows users to modify existing items, add their own or even cancel the request. Here is a sample code that may be used in your case:

    ContextMenuService menuService = this.radDock1.GetService<ContextMenuService>();
    menuService.ContextMenuDisplaying += menuService_ContextMenuDisplaying;
     
    private void menuService_ContextMenuDisplaying(object sender, ContextMenuDisplayingEventArgs e)
    {
        //the menu request is associated with a valid DockWindow instance, which resides within a DocumentTabStrip
        if (e.MenuType == ContextMenuType.DockWindow &&
            e.DockWindow.DockTabStrip is DocumentTabStrip)
        {
            //remove the "Close" menu item
            for (int i = 0; i < e.MenuItems.Count; i++)
            {
                RadMenuItemBase menuItem = e.MenuItems[i];
                if (menuItem.Text == "Close")
                {
                    menuItem.Enabled = false;
                }
            }
        }
    }

    Or you may handle the ContextMenuItemClicked event of the service and cancel the default action for the Close command:

    ContextMenuService menuService = this.radDock1.GetService<ContextMenuService>();
    menuService.ContextMenuItemClicked += menuService_ContextMenuItemClicked;
     
    private void menuService_ContextMenuItemClicked(object sender, ContextMenuItemClickEventArgs e)
    {
        if (e.Item.Text == "Close")
        {
            //mark the action as Handled and thus prevent the default action
            e.Handled = true;
        }
    }


    I hope this helps. Do not hesitate to contact us if you have other questions.

    Regards,
    Georgi
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Deepak Shakya
    Deepak Shakya avatar
    118 posts
    Member since:
    Aug 2007

    Posted 03 Dec 2009 Link to this post

    Hi Georgi,

    Thank you so much for the quick response. Your first solution works great. I have implemented in my project. Now the users can't close document window ot the tool window. they are still allowed to AutoHIde the tool window.

    Really appreciate your help. I was pulling my hair off the whole day trying to fix this problem. ;-)

    You guys are awesome!

    Regards,
    Deepak.
Back to Top