Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

dynamic context menu

$
0
0

I tried to create a context menu for a griedview and when i test it it works greacefully

private void gridView1_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            ContextMenu c = new ContextMenu();
            DevExpress.XtraGrid.Views.Grid.GridView view = (DevExpress.XtraGrid.Views.Grid.GridView)sender;
            DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo HitInfo = view.CalcHitInfo(e.Point);
            if (HitInfo.InRow)
            {
                view.FocusedRowHandle = HitInfo.RowHandle;
                c.MenuItems.Clear();
                c.MenuItems.Add("sample", sampsss);
                c.MenuItems.Add("sample", sampsss2);
                c.Show(view.GridControl, e.Point);
            }
        }
        private static void sampsss(object sender, EventArgs e)
        {
            MessageBox.Show("sample 1");
        }
        private static void sampsss2(object sender, EventArgs e)
        {
            MessageBox.Show("12321344");
        }

the code above work as i wanted, then it comes in my mind that i am always using it.
so i decide to create a static method of this in the outside class so i will just call it
when i nedded. then i came up with this.

public static void PopupMenuX(object sender, PopupMenuShowingEventArgs e, Collections<string> menuItems, params EventHandler[] onCLick)
    {
        if (onCLick.Length < 1 || menuItems.Count() != onCLick.Length)
            throw new ArgumentOutOfRangeException("menuItems or onClick");
        using (ContextMenu c = new ContextMenu())
        {
            GridView View = (GridView)sender;
            DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo HitInfo = View.CalcHitInfo(e.Point);
            if (HitInfo.InRow)
            {
                View.FocusedRowHandle = HitInfo.RowHandle;
                for (int i = 0; i < onCLick.Length; i++)
                {
                    c.MenuItems.Add(menuItems.Value(i), onCLick[i]);
                }
                c.Show(View.GridControl, e.Point);
            }
        }
    }

after that i tried it like this.

private void gridView1_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            someclass.PopupMenuX(sender, e, new Collections<string>() { "sample", "sample2" }, sampsss, sampsss2);
        }
        private static void sampsss(object sender, EventArgs e)
        {
            MessageBox.Show("sample 1");
        }
        private static void sampsss2(object sender, EventArgs e)
        {
            MessageBox.Show("12321344");
        }

at first glance i thought it was working as i wanted, it showned the context menu with two options sample and sample2 but the problem come after i clicked onw of the options. it doesnt produced any responce, that i was expecting that it will show a messagebox. but one thing it doesnt also show a runtime. then i tried to dubug it it didnt call the methods selected.

so i was thinking that i am passing a method in the eventhandler in the lastparameter in the popupmenux. so i tried to use delegate, but the things get more complicated because in the parameter needed by the context menu.

c.MenuItems.Add(menuItems.Value(i), onCLick[i]);

the add method needs a eventhandler in the last parameter with a corresponding string for the first parameter. so i tried another options but it just needs some MenuItems.

i will appreciate for any help will come

thank you

sorry for my bad english



Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>