Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
Hi, I am working on a project and I am using the treeview tool to organize some functions on one side and a richtextbox on the other providing some descriptions of each one.

So far I know how to create a node using the properties menu but I am having some trouble doing events on them. For example I want it so that when someone double clicks on a node it will fire an if command that when a node is selected it will display a text on the richtextbox providing the description for that particular node.

I'm using Visual C# and my application is a window form

Thanks,
Posted
Updated 14-Dec-11 16:21pm
v3
Comments
Sergey Alexandrovich Kryukov 14-Dec-11 20:50pm    
Which TreeView? There are more than one class with this name. Fully-qualified name, please. Tag you UI library: WPF, Silverlight, Form, what?

Now, how about just reading MSDN help page? Everything is crystal-clear...
--SA
MR. AngelMendez 14-Dec-11 21:20pm    
sorry about that, my application is a windows form, now about the TreeView I don't know how to explain it because all I see is one but here is the tooltip definition.

Displays a hierarchical collection of labeled items to the user that optionally contains an image.
Sergey Alexandrovich Kryukov 14-Dec-11 22:08pm    
That's better. Just add the tag "WinForms" ("Improve question"). The solution of Robin is not really correct. Don't worry, give me some time...
--SA
MR. AngelMendez 14-Dec-11 22:18pm    
ok thanks, well Robins solution works because instead of textbox1.text = e.Node.text I changed it to textbox1.text = e.Node.tooltip

which all i do is change the tooltip to my description but if you have a better way then I would appreciate it.
Sergey Alexandrovich Kryukov 14-Dec-11 22:44pm    
No, no, don't do it! "Working" does not mean right.
I've demonstrated all the techniques you need. This code is clean.
And you need different events for a command and showing description.
--SA

You are asking about selection of the node, but by description of the behavior it looks like you need to handle selection of the node itself.

First of all, put some relevant data in each node to use while processing event.
C#
internal struct TreeNodeData {
    internal TreeNodeData(string name, string description) { fName = name; fDescription = description; }
    internal string Name { return fName; }
    internal string Description { return fDescription; }
    string fName, fDescription;
}


Add the items of this type to your tree view, add more relevant data (command, as you say). I would suggest you add a delegate instance which will run execute the command.

You should assign an instance of this structure to the property Tag of each node you want to process.

Now, I don't think you need a click to show description. The description should be shown on selection. Set it up like this:

C#
myTreeView.AfterSelect += (sender, eventArgs) => {
    if (eventArgs.Node.Tag == null) return;
    TreeNodeData nodeData = (TreeNodeData)eventArgs.Node.Tag; //this is selected node because "After Select"
    ProcessTreeViewNodeSelection(nodeData); //all clear, not more type casts
}

myTreeView.NodeMouseDoubleClick += (sender, eventArgs) => {
    TreeViewNode selectedNode = eventArgs.Node;
    if (selectedNode.Tag == null) return;
    TreeNodeData nodeData = (TreeNodeData)selectedNode.Tag;
    RunTreeViewCommand(nodeData);
}

void ProcessTreeViewNodeSelection(TreeNodeData nodeData) {
    textBoxNodeDescription.Text = nodeData.Description; //assuming your text box was added earlier
    //...
}

void RunTreeViewCommand(TreeNodeData nodeData) {
    // you did not ask about this part, so this is on you
    // I would suggest you store some delegate instance in each node,
    // and to implement a command you add some handler to the invocation list of each delegate instance
    // and just invoke the delegate instance right here
}


Both event handlers shown above should be added (+=) before the form is shown. I usually call the code adding handlers in the constructors after InitializeComponent.

Is it clear now?

Good luck,
—SA
 
Share this answer
 
v5
Comments
MR. AngelMendez 14-Dec-11 23:19pm    
ive been trying your code and I cant seem to figure out how to implement it. I am somewhat as a beginner/ almost intermediate :) so It's a lot to take in. Is there an easier way or can you try to explain it based at my skill level? I know im asking for a lot because based on how much programming is involved here but I would deeply appreciate some extra help.

To make things more clear I will explain what my project is. My project is to make a GUI form of the command prompt. How was I going to achieve this? well I learned from recent research that visual C# is able to call events from the command-line itself. So the real problem is just organizing all of its functions. So I decided to go with the treeview tool because there are so many sub-commands within a command in the command line. Like I said earlier I wanted a treeview with the name of all the functions and a textbox with the description when someone selects a command. then I would add a button which would call the command-line command.

well thats what I am aiming for, so I hope this info can help you help me better.
Sergey Alexandrovich Kryukov 15-Dec-11 5:39am    
You don't have to implement anything in the part related to the scope of your question. You can add this code literally and just change the names of controls myTreeView, textBoxNodeDescription which I assume you should have. I did not put much detail on event instances for commands. But you did not put enough information on your commands, so I left it to you.

If you feel delegates are difficult, add any data which would help you to implement the commands. Add it to TreeNodeData. You have a ready to use skeleton. Now, leave commands in this skeleton form, first make sure Description works, to feel the techniques.

Now, I cannot understand your ultimate goals. You should explain them in the very beginning, but clearly. What is "a GUI form of the command line"? "C# is able to call events from the command line itself" -- who told you that? Command line is just a source of string information passed on start nothing else, has nothing to do with events -- at all.

TreeView with the names of the functions won't help you to call the functions -- you need to have functions themselves, and the only reference to a function is the delegate instance. You need to create them, then you can store them in data such as TreeNodeData, with some human-readable names.

The question is: where will you have the methods to call as commands? They should be ultimately predefined in your call? How this is related to command line? I don't think it can be. By the way, for a command-line parser you can use my work published in my article, you will find the link in my CodeProject profile.

Please understand that I cannot afford writing the whole application for you. Also, the solution cannot be simpler unless I make a big mistake, but I don't think so. Just the opposite, I suggested you compact code skeleton which helps to avoid meticulous error-prone work.
--SA
MR. AngelMendez 15-Dec-11 13:20pm    
I got it to work thanks for all the help, now to explain myself. my project is to make a GUI form of command prompt which means graphic user interface. Pretty much I'm making a graphical menu for all the commands so you can just click on them to execute. Now for your question of "C# is able to call events from the command line itself" -- who told you that?" well here is a sample code which will shutdown the computer using a command-line command.

System.Diagnostics.Process.Start("Shutdown", "/s");

hope this clarifies your questions.
Sergey Alexandrovich Kryukov 15-Dec-11 14:19pm    
I see. The thing is: what you call event is not an event in the sense of event-oriented programming. Event in .NET is something expressed by a keyword "event".

Also, what you do via UI won't do anything about command line, as I understand. This is something like a menu command behavior driven by data. It could be a very good idea if you use a good approach.

Read about them on a fundamental level starting from delegates. Actually, the standard documentation is very obscure, so, on top of that, please read corresponding section in my article "Dynamic Method Dispatcher", see http://www.codeproject.com/KB/dotnet/DynamicMethodDispatcher.aspx
It explains the nature of them which looks mysterious.

By the way, this article can give you a good idea about your graphical commands and menu -- the technique I develop is a very general yet easy-to-use approach to dispatching of method calls by keys of any nature. For example, the keys could be menu or list items, any set of unique object. I even demonstrated the use of System.Type keys, etc. Do you see the picture? You can dynamically associate delegate instances with the keys like UI elements and lot more. So, read the articles, ask me questions if you have to.

Good luck, call again.
--SA
1.Add a treeview and a textbox.

Sample Code from the Form1.Designer.cs:

private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.TextBox textBox1;


2.Add a event handler to the treeview. The event is called nodemouseclick. See the events list and you will find it.

Sample code about eventhandler from Form1.Designer.cs:

this.treeView1.NodeMouseClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(this.treeView1_NodeMouseClick);


3.Now the code to put in Form1.cs:

C#
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
    textBox1.Text = e.Node.Text;
}
 
Share this answer
 
Comments
MR. AngelMendez 14-Dec-11 22:20pm    
thanks for the help, it works good but its not quite what I had in mind but thanks I'll use it until I find a better way.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900