Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Adding CLICK event when creating TREENODES

Posted on 2010-11-23
3
Medium Priority
?
516 Views
Last Modified: 2012-05-10
I have a C# Windows form that progamatically creates treenodes when user selects a button.  After the treenode is created it needs to have a CLICK event added.  How do I assign an event when creating a treenode?

treeGX1.Nodes[0].Nodes.Add(newNode);

After I create the treenode I need to assign an event called "TReeNodeCLick".
0
Comment
Question by:Hojoformo
  • 2
3 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 34200855
you are either going to have to derive a class of your own and create the event

or you can just go by HitTest on the Treeview, when clicked, to get the node at x/y coordinates.
0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 2000 total points
ID: 34200872
The standard TreeNode doesn't have a Click event, but if you're using some other control:
TreeNode newNode = new TreeNode("Node Text");
newNode.TreeNodeClick += node_click; // Assign a function to the event
treeGX1.Nodes[0].Nodes.Add(newNode);

private void node_click(object sender, EventArgs e)
{
  TreeNode clickedNode = (TreeNode)sender;
  MessageBox.Show("The following node was clicked: " + clickedNode.Text);
}

Open in new window


If you're using the standard TreeView control, you'd handle the Click event of the TreeView itself (not of the node), and then use the SelectedNode property of the TreeView to know which node is selected.
public partial class Form1 : Form
{
	public Form1()
	{
		InitializeComponent();
	}

	private void Form1_Load(object sender, EventArgs e)
	{
		treeView1.Click += treeView1_Click; // Assign Click event handler of TreeView

		// Add a node
		treeView1.Nodes.Add("Hello WOrld");
	}

	private void treeView1_Click(object sender, EventArgs e)
	{
		MessageBox.Show("The following node was clicked: " + treeView1.SelectedNode.Text);
	}
}

Open in new window

0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34200899
Or, probably better to handle the AfterSelect event of the TreeView.

public partial class Form1 : Form
{
	public Form1()
	{
		InitializeComponent();
	}

	private void Form1_Load(object sender, EventArgs e)
	{
		treeView1.AfterSelect += new TreeViewEventHandler(treeView1_AfterSelect);

		// Add a node
		treeView1.Nodes.Add("One");
		treeView1.Nodes.Add("Two");
		treeView1.Nodes.Add("Three");
	}

	void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
	{
		MessageBox.Show("Selected node: " + e.Node.Text);
	}
}

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question