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

Recommended Answers

All 3 Replies

Read about http://www.dotnetperls.com/extension extension methods.
An extension method has simplified calling syntax. It represents static methods as instance methods. An extension method uses the this-keyword in its parameter list. It must be located in a static class.

opps, i fugured it out.

i didnt realize that iam disposing the context menu before triggering the the event.

so i just remove the using statement.

BTW thanks for the reply.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.