How to use multiple panels in a windows form
-
hello everybody!! iam generating a windows form which has a treeview at the left and a splitpanel at its right. the treeview consists of 3 parent nodes with 1 child node in each parent node. the parent nodes have a contextmenu with "Start" and "Stop" as menu items and child nodes have a contextmenu with "Properties" as menuitem. each of the menuitem has an OnClick event handler. what i want is, on clicking the menuitem of each of the child nodes, a different panel should be displayed each time. for this, i need to have 3 panels, one for each childnode. when one panel is being displayed, others should be hidden. my requirement is something similar to the WindowsExplorer, but iam not able to generate the desired o/p. Also, when i right-click on any of the node, the contextmenu is displayed, but after clicking the desired menuitem, the contextmenu just doesnt disappear. am i missing some property tat has to be set for the contextmenu to disappear after selecting a menuitem? this is a part of my code:
mnuParentMenu.MenuItems.Add(0, new MenuItem("Start", new System.EventHandler(mnuStart_Click))); mnuParentMenu.MenuItems.Add(1, new MenuItem("Stop", new System.EventHandler(mnuStop_Click))); mnuChildMenu.MenuItems.Add(0, = new MenuItem("Properties", new System.EventHandler(mnuPropertiesC_Click))); TreeNode node = new TreeNode("Parent"); node.Tag = "ParentNodeMenu"; node.ContextMenu = mnuParentMenu; treeView1.Nodes.Add(node); node = new TreeNode("Child"); node.Tag = "ChildNodeMenu"; node.ContextMenu = mnuChildMenu; treeView1.Nodes[0].Nodes.Add(node);
I would be thankful to all who wish to take some time and help me out.Sonu
-
hello everybody!! iam generating a windows form which has a treeview at the left and a splitpanel at its right. the treeview consists of 3 parent nodes with 1 child node in each parent node. the parent nodes have a contextmenu with "Start" and "Stop" as menu items and child nodes have a contextmenu with "Properties" as menuitem. each of the menuitem has an OnClick event handler. what i want is, on clicking the menuitem of each of the child nodes, a different panel should be displayed each time. for this, i need to have 3 panels, one for each childnode. when one panel is being displayed, others should be hidden. my requirement is something similar to the WindowsExplorer, but iam not able to generate the desired o/p. Also, when i right-click on any of the node, the contextmenu is displayed, but after clicking the desired menuitem, the contextmenu just doesnt disappear. am i missing some property tat has to be set for the contextmenu to disappear after selecting a menuitem? this is a part of my code:
mnuParentMenu.MenuItems.Add(0, new MenuItem("Start", new System.EventHandler(mnuStart_Click))); mnuParentMenu.MenuItems.Add(1, new MenuItem("Stop", new System.EventHandler(mnuStop_Click))); mnuChildMenu.MenuItems.Add(0, = new MenuItem("Properties", new System.EventHandler(mnuPropertiesC_Click))); TreeNode node = new TreeNode("Parent"); node.Tag = "ParentNodeMenu"; node.ContextMenu = mnuParentMenu; treeView1.Nodes.Add(node); node = new TreeNode("Child"); node.Tag = "ChildNodeMenu"; node.ContextMenu = mnuChildMenu; treeView1.Nodes[0].Nodes.Add(node);
I would be thankful to all who wish to take some time and help me out.Sonu
Sonu.T wrote:
what i want is, on clicking the menuitem of each of the child nodes, a different panel should be displayed each time. for this, i need to have 3 panels, one for each childnode. when one panel is being displayed, others should be hidden. my requirement is something similar to the WindowsExplorer, but iam not able to generate the desired o/p.
How have you implemented it currently and what happens if you click on a child node? Simply setting the
Visible
property of the panel to be hidden to false and sending it to the back of the ZOrder usingSendToBack
should work fine, I think. Another approach would be to have a single panel, but differentUserControl
derived classes. That way, designing the UI would be easier.Sonu.T wrote:
Also, when i right-click on any of the node, the contextmenu is displayed, but after clicking the desired menuitem, the contextmenu just doesnt disappear.
I'm surprised that you even get the context menu. This[^] KB article shows how to add multiple context menus for different nodes to treeviews. Basically, you subscribe to the
MouseUp
event, get the point where the mouse was clicked, use theGetNodeAt
method to get theTreeNode
at that point and then show the appropriate popup menu.Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro
-
Sonu.T wrote:
what i want is, on clicking the menuitem of each of the child nodes, a different panel should be displayed each time. for this, i need to have 3 panels, one for each childnode. when one panel is being displayed, others should be hidden. my requirement is something similar to the WindowsExplorer, but iam not able to generate the desired o/p.
How have you implemented it currently and what happens if you click on a child node? Simply setting the
Visible
property of the panel to be hidden to false and sending it to the back of the ZOrder usingSendToBack
should work fine, I think. Another approach would be to have a single panel, but differentUserControl
derived classes. That way, designing the UI would be easier.Sonu.T wrote:
Also, when i right-click on any of the node, the contextmenu is displayed, but after clicking the desired menuitem, the contextmenu just doesnt disappear.
I'm surprised that you even get the context menu. This[^] KB article shows how to add multiple context menus for different nodes to treeviews. Basically, you subscribe to the
MouseUp
event, get the point where the mouse was clicked, use theGetNodeAt
method to get theTreeNode
at that point and then show the appropriate popup menu.Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro
hello Senthil thanx for replying. i understand that there is no clarity in my query.... iam pasting my entire code here...hope this wud make better sense
public partial class Form1 : Form { private System.Windows.Forms.ContextMenu mnuParentMenu = new System.Windows.Forms.ContextMenu(); private System.Windows.Forms.ContextMenu mnuChildMenu = new System.Windows.Forms.ContextMenu(); private MenuItem mnuStart; private MenuItem mnuStop; private MenuItem mnuProperties; private MenuItem mnuPropertiesC; private TreeNode m_OldSelectNode; private TreeNode node; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { mnuParentMenu.MenuItems.Add(0, mnuStart = new MenuItem("Start", new System.EventHandler(mnuStart_Click))); mnuParentMenu.MenuItems.Add(1, mnuStop = new MenuItem("Stop", new System.EventHandler(mnuStop_Click))); mnuParentMenu.MenuItems.Add(2, mnuProperties = new MenuItem("Properties", new System.EventHandler(mnuProperties_Click))); mnuChildMenu.MenuItems.Add(0, mnuPropertiesC = new MenuItem("Properties", new System.EventHandler(mnuPropertiesC_Click))); node = new TreeNode("Credit Files"); node.Tag = "ParentNodeMenu"; node.ContextMenu = mnuParentMenu; treeView1.Nodes.Add(node); node = new TreeNode("VIO Copy Process"); node.Tag = "ChildNodeMenu"; node.ContextMenu = mnuChildMenu; treeView1.Nodes[0].Nodes.Add(node); node = new TreeNode("Human Resources"); node.Tag = "ParentNodeMenu"; node.ContextMenu = mnuParentMenu; treeView1.Nodes.Add(node); node = new TreeNode("Secondary Batch Update Process"); node.Tag = "ChildNodeMenu"; node.ContextMenu = mnuChildMenu; treeView1.Nodes[1].Nodes.Add(node); node = new TreeNode("TimeDeposits"); node.Tag = "ParentNodeMenu"; node.ContextMenu = mnuParentMenu; treeView1.Nodes.Add(node); node = new TreeNode("PurgeProcess"); node.Tag = "ChildNodeMenu"; node.ContextMenu = mnuChildMenu; treeView1.Nodes[2].Nodes.Add(node); node = new TreeNode("SecondaryBatchUpdateProcess"); nod
-
hello Senthil thanx for replying. i understand that there is no clarity in my query.... iam pasting my entire code here...hope this wud make better sense
public partial class Form1 : Form { private System.Windows.Forms.ContextMenu mnuParentMenu = new System.Windows.Forms.ContextMenu(); private System.Windows.Forms.ContextMenu mnuChildMenu = new System.Windows.Forms.ContextMenu(); private MenuItem mnuStart; private MenuItem mnuStop; private MenuItem mnuProperties; private MenuItem mnuPropertiesC; private TreeNode m_OldSelectNode; private TreeNode node; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { mnuParentMenu.MenuItems.Add(0, mnuStart = new MenuItem("Start", new System.EventHandler(mnuStart_Click))); mnuParentMenu.MenuItems.Add(1, mnuStop = new MenuItem("Stop", new System.EventHandler(mnuStop_Click))); mnuParentMenu.MenuItems.Add(2, mnuProperties = new MenuItem("Properties", new System.EventHandler(mnuProperties_Click))); mnuChildMenu.MenuItems.Add(0, mnuPropertiesC = new MenuItem("Properties", new System.EventHandler(mnuPropertiesC_Click))); node = new TreeNode("Credit Files"); node.Tag = "ParentNodeMenu"; node.ContextMenu = mnuParentMenu; treeView1.Nodes.Add(node); node = new TreeNode("VIO Copy Process"); node.Tag = "ChildNodeMenu"; node.ContextMenu = mnuChildMenu; treeView1.Nodes[0].Nodes.Add(node); node = new TreeNode("Human Resources"); node.Tag = "ParentNodeMenu"; node.ContextMenu = mnuParentMenu; treeView1.Nodes.Add(node); node = new TreeNode("Secondary Batch Update Process"); node.Tag = "ChildNodeMenu"; node.ContextMenu = mnuChildMenu; treeView1.Nodes[1].Nodes.Add(node); node = new TreeNode("TimeDeposits"); node.Tag = "ParentNodeMenu"; node.ContextMenu = mnuParentMenu; treeView1.Nodes.Add(node); node = new TreeNode("PurgeProcess"); node.Tag = "ChildNodeMenu"; node.ContextMenu = mnuChildMenu; treeView1.Nodes[2].Nodes.Add(node); node = new TreeNode("SecondaryBatchUpdateProcess"); nod
Sonu.T wrote:
about some UserControls in one panel
You could have separate UserControls instead of separate panels (Right click on Project in Solution Explorer -> Select Add -> UserControl). That way, each of your "panels" will be separate entities at design time. If you put everything in panels directly, then the main form holding all the panels would be congested with controls from different panels (when designing the UI from Visual Studio). So you could have one panel to the right of the splitter and UserControl derived class for each of your screens. You could then load the appropriate control depending on the node selected in the tree. Something like
TreeNode selectedNode = ...;
switch(selectedNode.Text)
{
case "Weekly Processes";
panel.Controls.Clear();
panel.Controls.Add(weeklyProcessesControl);
break;
...
}where panel is the System.Windows.Forms.Panel to the right of the splitter and weeklyProcessesControl is the usercontrol.
Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro