Solved

Adding CLICK event when creating TREENODES

Posted on 2010-11-23
3
511 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 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

808 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