Scott G

Scott G

  • NA
  • 103
  • 7.8k

Use treeview node to open another form

Nov 15 2018 1:16 PM
I have a form (form1)with two panels, sidebar and content. I have added a treeview to the sidebar that I want to use to display a different form in content. I have done this before with a button using the following code. I am not sure how to do the same with a treeview node click.
public void nav(Form form, Panel panel)
{
form.TopLevel = false;
form.Size = panel.Size;
panel.Controls.Clear();
panel.Controls.Add(form);
form.Show();
}
private void Button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
nav(f2, pnlContent);
}
I tried adding the following to my form load and created a NodeMouseClick event but nothing happened.
treeView1.NodeMouseClick +=
new TreeNodeMouseClickEventHandler(treeView1_NodeMouseClick);
void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Node.Name == "Form2")
{
Form2 f2 = new Form2();
nav(f2, pnlContent);
}
}
Suggestions are greatly appreciated.