Reset ContextMenu options after ContextMenuOpening

2 posts, 1 answers
  1. Imad
    Imad avatar
    2 posts
    Member since:
    Jun 2008

    Posted 31 Jul Link to this post

    Hello,

    I have a RadTreeView with a context menu showing different options depending on the node level. However, I need to reset the context menu after ContextMenuOpening because once a menu item is removed from the context menu it doesn't display again.

     

    01.void radTreeEmployers_ContextMenuOpening(object sender, Telerik.WinControls.UI.TreeViewContextMenuOpeningEventArgs e){
    02.    if (e.Node.Level == 1 || e.Node.Level == 2)
    03.    {
    04.        for (int i = 0; i <= e.Menu.Items.Count - 1; i++)
    05.        {
    06.            if (e.Menu.Items[i].Name == "addEmployer")
    07.                e.Menu.Items.Remove(e.Menu.Items[i]);
    08.        }
    09.    }
    10.}

  2. Answer
    Dess
    Admin
    Dess avatar
    1605 posts

    Posted 01 Aug Link to this post

    Hello Imad,

    Thank you for writing. 

    Instead of removing the context menu item, you can just hide it:
    public Form1()
    {
        InitializeComponent();
     
        this.radTreeView1.AllowDefaultContextMenu = true;
        this.radTreeView1.ContextMenuOpening += radTreeView1_ContextMenuOpening;
    }
     
    bool isVisible = false;
     
    private void radTreeView1_ContextMenuOpening(object sender, Telerik.WinControls.UI.TreeViewContextMenuOpeningEventArgs e)
    {
        foreach (RadItem item in e.Menu.Items)
        {
            if (isVisible == false && item.Text == "&Copy")
            {
                item.Visibility = ElementVisibility.Collapsed;
            }
            else
            {
                item.Visibility = ElementVisibility.Visible;
            }
        }
    }
     
    private void radButton1_Click(object sender, EventArgs e)
    {
        isVisible = !isVisible;
    }

    I hope this information helps. Should you have further questions I would be glad to help.

    Regards,
    Dess
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top