2

How to create a menu in winforms which totally resembles the menu which appears on Left Hand Frame in Windows Explorer when we Explore any folder. The menu contains tree nodes and root nodes which appear & disappear by clicking on the + & - symbols.

sqlchild
  • 8,754
  • 28
  • 105
  • 167
  • Not sure but check this http://stackoverflow.com/questions/2416963/how-to-create-an-explorer-like-folder-browser-control – Developer Mar 19 '11 at 06:49

2 Answers2

2

Well, that's not a menu, it's a tree view. You can use the WinForms tree view, but out of the box it won't look exactly like the Explorer tree view. You need to apply the Explorer window theme.

You need to P/Invoke to call SetWindowTheme passing the window handle of the tree and use "explorer" as the theme.

Paste the following code into a new class in your project, compile, and use this custom control instead of the built-in TreeView control.

public class NativeTreeView : System.Windows.Forms.TreeView
{
    [DllImport("uxtheme.dll", CharSet = CharSet.Unicode)]
    private extern static int SetWindowTheme(
        IntPtr hWnd, 
        string pszSubAppName,
        string pszSubIdList
    );

    protected override void CreateHandle()
    {
        base.CreateHandle();
        SetWindowTheme(this.Handle, "explorer", null);
    }
}

Note that this trick also works exactly the same way for the ListView control.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • sir, its giving the following error---: 'menu_resembling_windows_menu.newmenu.CreateHandle()': no suitable method found to override – sqlchild Mar 25 '11 at 05:23
1

You can have multiple 'root' nodes in a WinForms treeview:

treeView.Nodes.Add("Root 1");
treeView.Nodes.Add("Root 2");

Instead of text above, they could be full nodes w/ children.

Jonas
  • 4,454
  • 3
  • 37
  • 45
  • how to make a child node of Root 1- like ChildRoot1 – sqlchild Mar 19 '11 at 10:24
  • also, i want that when i click on "Root 2" node then a form opens. is this possible? please mention the code also sir. – sqlchild Mar 19 '11 at 10:28
  • 1
    TreeNode tn = new TreeNode("Root 1"); tn.Children.Add(new TreeNode("ChildRoot1"); treeView.Nodes.add(tn); //etc, etc – Jonas Mar 20 '11 at 02:11
  • sir, it gives an Error : System.Windows.Forms.TreeNode' does not contain a definition for 'Children' – sqlchild Mar 22 '11 at 05:22
  • 1
    err, make that: tn.Nodes.Add(new TreeNode("ChildRoot1")); – Jonas Mar 22 '11 at 16:05
  • @Jonas: sir, this is also working , tn.Nodes.Add("ChildRoot1"); , but what's the difference in this and the one you have mentioned--tn.Nodes.Add(new TreeNode("ChildRoot1")); – sqlchild Mar 23 '11 at 04:54
  • 1
    Different overloads of the .Add method...if you wanted multiple children with their own children, you'd add individual nodes, if you just want the text, you can do it that way. – Jonas Mar 23 '11 at 16:56
  • @Jonas: So for childroot1 , how would i create its further child namely SubChildofChildRoot1 – sqlchild Mar 24 '11 at 12:54
  • 1
    Same way: TreeNode tn = new TreeNode("Node name"); TreeNode otherTn = new TreeNode("Another node"); TreeNode yetAnotherTn = new TreeNode("Yet another node"); //so on, so forth otherTn.Nodes.Add(yetAnotherTn); tn.Nodes.Add(otherTn); (edit: Lord, how does that code mini-markdown thing work for comments?) – Jonas Mar 24 '11 at 21:43