Click here to Skip to main content
15,896,387 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
See more: C#
So i want to do a TreeNode that when a child is selected it loads a text file (that i have) into a text box to the right of the node.

Here is a link to a picture of what i have so far.

http://i978.photobucket.com/albums/ae266/athenso/HA/nodeprevies.png[^]

i want it so like Desert Quest gets clicked. The text file for desert quest is loaded into the richtext box on the right. Its locked so it cant be edited. But yeah if anyone could help with that, i would appreciate it alot
Posted
Updated 16-May-12 6:38am
v2
Comments
athenso 16-May-12 12:53pm    
Hmm still kind of stumped.

I have

private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
{
string DesertFile = "Z:\\Tibia Tool\\Quests\\DesertQuest.txt";
rchQuests.LoadFile(DesertFile, RichTextBoxStreamType.PlainText);
}
}

i need it so if Desert is clicked it loads the DesertQuests.txt into the box. Im not sure where im goofing it up
Wendelius 16-May-12 13:01pm    
See the updated answers. If you have questions about the answers, you can use the "Have a Question or Comment?" button on each solution.
athenso 16-May-12 13:06pm    
I now have

private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{

try
{
// Look for a spacific text in the node
if (e.Node.Text.Contains("Desert Quest"))
{
string DesertFile = "Z:\\Tibia Tool\\Quests\\DesertQuest.txt";
rchQuests.LoadFile(DesertFile, RichTextBoxStreamType.PlainText);
}
}
// If the file is not found, inform the user.
catch
{
MessageBox.Show("File not found.");
}


}
the file doesn't load.
Wendelius 16-May-12 14:26pm    
As said, use the "Have a Question or Comment?" on each solution if you have questions about a solution. This way the person who wrote the solution get's notified.

Handle the TreeView.NodeMouseClick event and use the e.Node property to select the node.
You can then read the file using File.ReadAllLines and set the TextBox.Lines property.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-May-12 13:30pm    
Even though OP writes "click", it's more likely selection events.
Please see my answer.
--SA
To react on an node click, you can use the TreeView.NodeMouseClick Event[^].

So in that event you can check which node was clicked and then if the node is the correct one you can use the RichTextBox.LoadFile Method (String)[^] to fill the rich text box.

Addition:

To identify the node, you can use for example the Text[^] or Tag[^] properties.

The event arguments of the NodeMouseClick include the node that was selected. See: TreeNodeMouseClickEventArgs Class[^]

Addition 2:
Instead of presuming that the file isn't found check what the exception says. So change the code to:
C#
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
       {
           try
           {
               // Look for a  spacific text in the node
               if (e.Node.Text.Contains("Desert Quest"))
               {
                   string DesertFile = "Z:\\Tibia Tool\\Quests\\DesertQuest.txt";
                   rchQuests.LoadFile(DesertFile, RichTextBoxStreamType.PlainText);
               }
           }
           } catch (System.IO.FileNotFoundException) {
              MessageBox.Show("File not found");
           }
           catch (System.Exception exception)
           {
              MessageBox.Show(exception.Message);
           }
       }

That would give you the exact details also if the exception is something else than file not found.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 16-May-12 13:30pm    
Even though OP writes "click", it's more likely selection events.
Please see my answer.
--SA
Wendelius 16-May-12 14:27pm    
Quite possible, but the examples OP has now refer to node click. Not sure about it though...
athenso 16-May-12 14:40pm    
So the code doesnt throw errors but it doesnt work either.
athenso 16-May-12 14:44pm    
I can have my friends teacher look at it. Like i said the code doesnt throw errors like it was when i was trying it, but it wont load the text file.
Wendelius 16-May-12 14:47pm    
If you use the debugger, does the program ever go into loading the file, so is the condition

e.Node.Text.Contains("Desert Quest"))

true? If it isn't, using debugger, check the text of the node. Are there differences in capital letters etc.
Even though you write "click", most likely you mean handling the node selection events, TreeView.BeforeSelect, TreeView.AfterSelect:
http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.beforeselect.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.afterselect.aspx[^].

Besides, from your question it's not clear if you use WPF, Forms or something else. Please tag it, always. The idea is pretty much the same.

—SA
 
Share this answer
 

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