views:

161

answers:

2

I am using a Winforms ListView to show some files, but other than showing the files like explorer does, I want to have the same explorer right click menu when you get when you right click an item inside.

Is this possible? How do I enable it for my ListView app?

+1  A: 

The only way I know of to do this is to use pinvoke and COM to do it. The unmanaged API you want, I think, is SHCreateDefaultContextMenu(). Once you get the interop done (check pinvoke.net first), you can do the interop for all the other things required by DEFCONTEXTMENU. It won't be easy. Welcome to the land of PIDLs.

jeffamaphone
Thanks hope I can find my way from it.
Joan Venge
I looked at pinvoke but these don't seem to be there. I can't be the first person who wanted this behaviour, right?
Joan Venge
You can be the first person who bothers to share it with others. There is a lot of obvious stuff missing from there.
jeffamaphone
+1  A: 

I was actually having trouble getting a custom context menu to show with a right click: Erratic Behavior from ContextMenu

I still implemented slightly different:

    private void lstModules_MouseDown(object sender , MouseEventArgs e)
    {
        hitTest = lstModules.HitTest(e.Location);

        switch (e.Button)
        {
            case MouseButtons.Right:
                if (hitTest != null && hitTest.Item != null)
                {
                    // right clicking an item in the listview
                    selectedModule = hitTest.Item.Name;

                    lstModules.ContextMenuStrip = mnuContext_OptionsA;
                }
                else
                { 
                    // right clicking in white area of listview
                    lstModules.ContextMenuStrip = mnuContext_OptionsB; 
                }
                break;
        }
    }
dboarman