Windows Develop Bookmark and Share   
 index > Windows Forms Sample Applications > Treeview with contextMenustrip control
 

Treeview with contextMenustrip control

Hi,

I am working on windows application and c#.net.

I have one treeview control. while executing the applicaiton it is having the root node as student.

if i am select Student(rootnode) and right click on the the contectmenu would be "Add Student" it will add

"Student1" node to student.(this means if i select student and add student it wiill add the nodes

like student1,student2, student3).

And if i select "Studnet1" node and right click the context menu would be "Add StudentDetails" it will add the

"StudentDetails" node to "Student1" ...

And if i select "StudentDetails" and right click the contextmenu would be "Add First Name" and "Add Last

Name" then we can select one then it will add the node to "StudentDetails"


here the xml file design. And if i select studentdetails node then corresponding details should displayed in

the textboxes.

+Student
+Student1
+StudentDetails
+FirstName
+Lastname
+Student1
+StudentDetails
+FirstName
+Lastname
+Student1
+StudentDetails
+FirstName
+Lastname
+Student1
+StudentDetails
+FirstName
+Lastname

Dotnetbubbles  Saturday, May 17, 2008 1:31 PM

Hi,

Thanks for your post.

You can drag two ContextMenuStrip to the form, one has an "Add Student" menu item, and the other has two menu items for adding student details. And handle the NodeMoustClick event to show the proper context menu according to the node selected, something like this

Code Snippet

treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)

{

if (e.Button == MouseButtons.Right)

{

this.treeView1.SelectedNode = e.Node;

if (e.Node.Parent == null) //click on the root

{

this.contextMenuStrip1.Show(this.treeView1,e.Location);

}

else if (e.Node.Parent.Parent == null)//click on the student nodes

{

this.contextMenuStrip2.Show(this.treeView1,e.Location);

}

}

}

int studentCount = 1;

private void addStudentToolStripMenuItem_Click(object sender, EventArgs e)

{

this.treeView1.SelectedNode.Nodes.Add("Student" + studentCount.ToString());

studentCount++;

}

private void firstNameToolStripMenuItem_Click(object sender, EventArgs e)

{

this.treeView1.SelectedNode.Nodes.Add("First Name");

}

private void lastNameToolStripMenuItem_Click(object sender, EventArgs e)

{

this.treeView1.SelectedNode.Nodes.Add("Last Name");

}



Hope this helps.
If you have any more questions, please feel free to let me know.Thanks in advance for any feedback.

Best Regards
Zhi-xin Ye

Zhi-Xin Ye  Monday, May 19, 2008 2:03 PM

Hi,

Thanks for your post.

You can drag two ContextMenuStrip to the form, one has an "Add Student" menu item, and the other has two menu items for adding student details. And handle the NodeMoustClick event to show the proper context menu according to the node selected, something like this

Code Snippet

treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)

{

if (e.Button == MouseButtons.Right)

{

this.treeView1.SelectedNode = e.Node;

if (e.Node.Parent == null) //click on the root

{

this.contextMenuStrip1.Show(this.treeView1,e.Location);

}

else if (e.Node.Parent.Parent == null)//click on the student nodes

{

this.contextMenuStrip2.Show(this.treeView1,e.Location);

}

}

}

int studentCount = 1;

private void addStudentToolStripMenuItem_Click(object sender, EventArgs e)

{

this.treeView1.SelectedNode.Nodes.Add("Student" + studentCount.ToString());

studentCount++;

}

private void firstNameToolStripMenuItem_Click(object sender, EventArgs e)

{

this.treeView1.SelectedNode.Nodes.Add("First Name");

}

private void lastNameToolStripMenuItem_Click(object sender, EventArgs e)

{

this.treeView1.SelectedNode.Nodes.Add("Last Name");

}



Hope this helps.
If you have any more questions, please feel free to let me know.Thanks in advance for any feedback.

Best Regards
Zhi-xin Ye

Zhi-Xin Ye  Monday, May 19, 2008 2:03 PM

You can use google to search for other answers

Custom Search

More Threads

• Diffgram questions
• Error while executing PocketpC Taskvision
• Sql Method calls
• I need help on TaskVision someone plz!
• * BUG - I though the Nox and Asgard were nasty bugs.
• Localization form
• Server Not Responding Work Around
• Help
• Complete overhaul of Task vision Web service Layer
• Setup and deployment in vs.net 2005