Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Adding CLICK event when creating TREENODES

Posted on 2010-11-23
3
Medium Priority
?
515 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 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

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

670 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