This is a migrated thread and some comments may be shown as answers.

Disable context menu for Document Window

2 Answers 443 Views
Dock
This is a migrated thread and some comments may be shown as answers.
Deepak Shakya
Top achievements
Rank 1
Deepak Shakya asked on 02 Dec 2009, 11:58 PM

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 Answers, 1 is accepted

Sort by
0
Georgi
Telerik team
answered on 03 Dec 2009, 07:42 AM
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.
0
Deepak Shakya
Top achievements
Rank 1
answered on 03 Dec 2009, 08:00 AM
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.
Tags
Dock
Asked by
Deepak Shakya
Top achievements
Rank 1
Answers by
Georgi
Telerik team
Deepak Shakya
Top achievements
Rank 1
Share this question
or