How to get the toplevel control of a RadMenuItem

4 posts, 0 answers
  1. Hardy
    Hardy avatar
    9 posts
    Member since:
    Oct 2012

    Posted 10 Mar 2013 Link to this post

    Hi all,

    I like to get the top level control of a RadMenuItem.
    For example: there is a RadForm with two RadGridView controls.
    Each containing a ContextMenu. For both I'm using the same handler which should recognize on which RadGridView the menu is clicked.
    How can I retrieve the  RadGridView which is the top level for the ContextMenu?

    Regards
    Hardy 
  2. Stefan
    Admin
    Stefan avatar
    2890 posts

    Posted 13 Mar 2013 Link to this post

    Hello Hardy,

    Thank you for writing.

    You can save the grid instance in the Tag property of the menu and later retrieve it in the click event handler. Here is a small sample:
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
     
            Random r = new Random();
            DataTable table = new DataTable();
            table.Columns.Add("ID", typeof(int));
            table.Columns.Add("Name", typeof(string));
            table.Columns.Add("Bool", typeof(bool));
            table.Columns.Add("DateColumn", typeof(DateTime));
     
            for (int i = 0; i < 10; i++)
            {
                table.Rows.Add(i, "Row " + i, r.Next(10) > 5 ? true : false, DateTime.Now.AddHours(i));
            }
     
            this.radGridView1.DataSource = table;
            this.radGridView2.DataSource = table;
     
            RadMenuItem item = new RadMenuItem("item");
            item.Click += item_Click;
            menu.Items.Add(item);
     
            radGridView1.ContextMenuOpening += radGridView1_ContextMenuOpening;
            radGridView2.ContextMenuOpening += radGridView1_ContextMenuOpening;
        }
     
        RadContextMenu menu = new RadContextMenu();
     
        void radGridView1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
        {
            e.ContextMenu = menu.DropDown;
            menu.DropDown.Tag = sender;
        }
     
        void item_Click(object sender, EventArgs e)
        {
            RadMenuItem item = (RadMenuItem)sender;
            RadGridView grid = (RadGridView)item.OwnerControl.Tag;
        }

    I hope that you find this information useful.
     

    Kind regards,
    Stefan
    the Telerik team
    WinForms Q1 2013 boasts PivotGrid, PDF Viewer, Chart enhancements and more. Check out all of the latest highlights.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Hardy
    Hardy avatar
    9 posts
    Member since:
    Oct 2012

    Posted 14 Mar 2013 Link to this post

    Hi Stefan,

    it's a possible workaround.
    I would like to get a property like menu.parent or menu.toplevelcontrol...
    Using the tag property is ok but in my solution I'm using the tag for other information.

    I solved the problem using a lookup table where any menu is stored with its grid
    Now I can select the menu's parent from the table.

    Regards
    Hardy
  5. Stefan
    Admin
    Stefan avatar
    2890 posts

    Posted 19 Mar 2013 Link to this post

    Hello Hardy,

    I am glad that you have found a suitable solution for your case.

    The parent of the menu items will be the element from the popup element tree, and since this popup is not part of the control, you cannot find the control in the element tree.

    Since you mention that the tag of the context menu was already taken, you can create a custom object that will hold both your object and the parent or you can use the Tag of the menu item.

    I hope that you find this information useful.
     

    All the best,
    Stefan
    the Telerik team
    WinForms Q1 2013 boasts PivotGrid, PDF Viewer, Chart enhancements and more. Check out all of the latest highlights.
Back to Top