|
Article relates to
|
RadDock for WinForms, Q3 2008
|
|
Created by
|
Nikolay Diyanov
|
|
Last modified
|
Nov 27, 2008
|
|
Last modified by
|
Nikolay Diyanov
|
HOW-TO
This article reveals the customizing possibilities that you have for the context menus in RadDock. We are going to create a simple project that demonstrates these possibilies.
SOLUTION
In the project we have two DockPanels - dockPanel1 and dockPanel2. We are going to make specific customizations to the dockPanel1's context menu and another set of customizations to the dockPanel2's context menu.
So, let's start. First, declare two global vairables of type DockMenuItem. These will be the variables of the custom menu items.
| DockMenuItem doSomethingItem1; |
| DockMenuItem doSomethingItem2; |
Then, in the Form constructor subscribe to the DropDownOpening event of the dockPanel1.DockContextMenu and dockPanel2.DockContextMenu:
| public Form1() |
| { |
| InitializeComponent(); |
| |
| dockPanel1.DockContextMenu.DropDownOpening += new CancelEventHandler(DockContextMenu_DropDownOpening); |
| dockPanel2.DockContextMenu.DropDownOpening += new CancelEventHandler(DockContextMenu_DropDownOpening); |
| } |
Further, put the logic for the menu customization in the DropDownOpening event handler:
| void DockContextMenu_DropDownOpening(object sender, CancelEventArgs e) |
| { |
| DockContextMenu dockMenu = sender as DockContextMenu; |
| if (dockMenu.DockWindow == dockPanel1) |
| { |
| dockMenu.GetDefaultItem("Hide").Visibility = ElementVisibility.Collapsed; |
| |
| if (doSomethingItem1 == null) |
| { |
| doSomethingItem1 = new DockMenuItem(DockState.Floating, "Custom Item for DockPanel1"); |
| |
| doSomethingItem1.Click += new EventHandler(doSomethingItem1_Click); |
| dockMenu.Items.Add(doSomethingItem1); |
| } |
| } |
| else if (dockMenu.DockWindow == dockPanel2) |
| { |
| dockMenu.GetDefaultItem("Floating").Visibility = ElementVisibility.Collapsed; |
| |
| if (doSomethingItem2 == null) |
| { |
| doSomethingItem2 = new DockMenuItem(DockState.AutoHide, "Custom Item for DockPanel2"); |
| |
| doSomethingItem2.Click += new EventHandler(doSomethingItem2_Click); |
| dockMenu.Items.Add(doSomethingItem2); |
| } |
| |
| if (dockMenu.DockWindow.DockState == DockState.TabbedDocument) |
| { |
| doSomethingItem2.Visibility = ElementVisibility.Collapsed; |
| } |
| else |
| { |
| doSomethingItem2.Visibility = ElementVisibility.Visible; |
| } |
| } |
| } |
Let me explain what we are doing after with the above code snippet: We check which is the context menu invoker through the DockWindow property. If the invoker is dockPanel1, we get its "Hide" item throught the GetDefaultItem method and we set its Visibility to Collapsed. Then, if an instance of doSomethingItem1 is not created, we create it, subscribe to its Click event and add it to the current context menu. Please note that DockMenuItem takes DockState as a parameter. This parameter is later used in the Click event handler.
The situation with dockPanel2 is almost the same, but if the dockPanel2 is TabbedDocment then custom item is not shown.
Reagarding the Click event handlers, we take the DockState from the respective DockMenuItem and set it to the context menu invoker. This is the place to implement your custom logic as well:
| void doSomethingItem2_Click(object sender, EventArgs e) |
| { |
| dockPanel2.DockState = ((DockMenuItem)sender).DockState; |
| // Do something here |
| } |
| |
| void doSomethingItem1_Click(object sender, EventArgs e) |
| { |
| dockPanel1.DockState = ((DockMenuItem)sender).DockState; |
| // Do something here |
| } |
Sample projects in both C# and VB.NET are provided to demonstrate the above approach.
Please
Sign In
to rate this article.