Solved

Adding CLICK event when creating TREENODES

Posted on 2010-11-23
3
509 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
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now