)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to sort the contextmenu items in WinForms TabbedGroupedMDIManager?

Platform: WinForms |
Control: TabbedGroupedMDIManager |
Published Date: June 21, 2018 |
Last Revised Date: January 27, 2020 |
.NET Framework: 4

Sort the context menu

We can sort the context menu items in the TabbedMDIManager by handling the BeforePopup event of ContextMenuItem. In this event we are getting the context menu items using ArrayList and perform sorting using CustomComparerItems class which is inherited from ICompare interface. The following code demonstrates the same.

C#

BarItem item = new BarItem();
item.Text = "CustomItem";
 
BarItem item2 = new BarItem();
item2.Text = "ArrangeItem";
 
BarItem item3 = new BarItem();
item3.Text = "OrderItem";
 
Form2 form2 = new Form2();
form2.MdiParent = this;
form2.Show();
Form2 form3 = new Form2();
form3.MdiParent = this;
form3.Text = "Form3";
form3.Show();
 
//Add Custom context menu items
this.tabbedGroupedMDIManager1.ContextMenuItem.Items.Add(item2);
this.tabbedGroupedMDIManager1.ContextMenuItem.Items.Add(item);
this.tabbedGroupedMDIManager1.ContextMenuItem.Items.Add(item3);
this.tabbedGroupedMDIManager1.ContextMenuItem.BeforePopup += new CancelEventHandler(ContextMenuItem_BeforePopup);
 
void ContextMenuItem_BeforePopup(object sender, CancelEventArgs e)
{
  ArrayList items = new ArrayList();
  foreach (object contextItem in this.tabbedGroupedMDIManager1.ContextMenuItem.Items)
  {
    items.Add(contextItem);
  }
  this.tabbedGroupedMDIManager1.ContextMenuItem.Items.Clear();
  //For ContextMenuItem sorting
  items.Sort(new CustomComparerItems());
  foreach (object item in items)
  {
    this.tabbedGroupedMDIManager1.ContextMenuItem.Items.Add(item);
  }
}
 
public class CustomComparerItems : IComparer
{
  Comparer compar = new Comparer(System.Globalization.CultureInfo.CurrentCulture);
  public int Compare(object first, object second)
  {
    string item1 = (first as BarItem).Text.ToString();
    string item2 = (second as BarItem).Text.ToString();
    return compar.Compare(item1, item2);
  }
}

VB

'For Context Menu Sorting
AddHandler tabbedGroupedMDIManager1.ContextMenuItem.BeforePopup, AddressOf ContextMenuItem_BeforePopup
 
Dim item As New BarItem()
item.Text = "CustomItem"
 
Dim item2 As New BarItem()
item2.Text = "ArrangeItem"
 
Dim item3 As New BarItem()
item3.Text = "OrderItem"
 
Dim form2 As New Form2()
form2.MdiParent = Me
form2.Show()
Dim form3 As New Form2()
form3.MdiParent = Me
form3.Text = "Form3"
form3.Show()
 
'Add Custom context menu items
Me.tabbedGroupedMDIManager1.ContextMenuItem.Items.Add(item2)
Me.tabbedGroupedMDIManager1.ContextMenuItem.Items.Add(item)
Me.tabbedGroupedMDIManager1.ContextMenuItem.Items.Add(item3)
 
Private Sub ContextMenuItem_BeforePopup(ByVal sender As Object, ByVal e As CancelEventArgs)
  Dim items As New ArrayList()
  For Each contextItem As Object In Me.tabbedGroupedMDIManager1.ContextMenuItem.Items
    items.Add(contextItem)
  Next contextItem
  Me.tabbedGroupedMDIManager1.ContextMenuItem.Items.Clear()
  'For ContextMenuItem sorting
  items.Sort(New CustomComparerItems())
  For Each item As Object In items
    Me.tabbedGroupedMDIManager1.ContextMenuItem.Items.Add(item)
  Next item
End Sub
 
Public Class CustomComparerItems
  Implements IComparer
  Private compar As New Comparer(System.Globalization.CultureInfo.CurrentCulture)
  Public Function Compare(ByVal first As Object, ByVal second As Object) As Integer    Implements IComparer.Compare
    Dim item1 As String = (TryCast(first, BarItem)).Text.ToString()
    Dim item2 As String = (TryCast(second, BarItem)).Text.ToString()
    Return compar.Compare(item1, item2)
  End Function
End Class

 

Screenshot

Show the sort context menu items

 

Samples:

C#:  SortingSample

VB:  SortingSample

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon