Skip to main content
GameDev.net gamedev.net
🔒 Locked

[.net] TreeView Nodes

Started by mntlinstituteflr Jan 25, 2006 at 7:26 PM 3 replies 750+ views
Original Post
mntlinstituteflr
mntlinstituteflr
Hello, I have started up programming again, just breifly and wanted to make a TreeView type program. I have done almost everything I need to do EXCEPT, I can not figure out how to get a button (once clicked) to make it rename the selected node. It you have any help, code, or suggestions, reply back. Patrick
DrjonesDW3d
DrjonesDW3d
Should be pretty straight forward. Assuming you have a button created, and a function assigned to when that button is clicked, and the TreeView object is a member of the object the code is in, it should be something like this:

private void btn_Click(object sender, EventArgs e){    myTreeView.SelectedNode.Text="Hello World";}


Of course if you wanted it to change to something other than "Hello World!" you could always read the string from a text box or something when the button is clicked.

Basically, the key to working with a tree view is to know the node structure. The nodes are objects of type TreeNode stored in a TreeNodeContainer. myTreeView.Nodes gives you access to that. Because a TreeView supports multiple levels, each TreeNode also has a Nodes collection, so you can do stuff like:

myTreeView.Nodes[0].Nodes[1].Text = "Hello World!"

Which would take the first node in the top branch, and then go to the 2nd node under that branch and change the text.
mntlinstituteflr
mntlinstituteflr
Well, your kinda close. I want the user to be able to click the button (once the node is selected) and be able to type in what they want it to be called. From my programming I already have a name set everytime the user clicks the "New Node" button, if you click new node, it makes a child node to the one selected THAT IS ALREADY NAMED "NAME". So, what I need is once someone clicks that rename node button the user will be able to change the name of the node selected (remember, the default name for a newly created node is "Name" according to what I have programmed). Get it?

Patrick
mutex
mutex
You mean like how in Explorer's tree view you can click a node, and then an edit box will show up on the node that'll let you type in the new name?

Take a look at TreeNode.BeginEdit. You also need to set TreeView.LabelEdit to true.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.