Solved

Adding CLICK event when creating TREENODES

Posted on 2010-11-23
3
514 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

623 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