How do I click on a text file in treeview to display its contents in richtextbox? There is a problem with the path, I can not understand how to connect the path and the name of the selected file. How to handle pressing the TreeView?

public string derevo_papok(string path) { TreeNode tn = new TreeNode(); treeView1.Nodes.Add("", path,2); string[] Directorii = Directory.GetDirectories(path); string[] Files = Directory.GetFiles(path); foreach (string s2 in Directorii) { string t = s2.Substring(s2.LastIndexOf('\\') + 1); ((TreeNode)treeView1.Nodes[0]).Nodes.Add(s2, t, 0); } foreach (string s3 in Files) { string r = s3.Substring(s3.LastIndexOf('\\') + 1); ((TreeNode)treeView1.Nodes[0]).Nodes.Add(s3, r, 0); } return path; } 

The code of the tree itself

  • Read about task decomposition into simple tasks. Your task is to (1) handle pressing the TreeView element, (2) read and write additional information to the TreeView node, (3) read the contents of the text file, (4) install the TichTextBox content on the specified string. These are four completely unrelated problems. Ask about them separately. - VladD
  • Then the question is how to handle pressing the element TreeView? - Andrei
  • Well, you are no longer new to the site? Then you know that questions should be asked not in the comments, but using the “Ask a Question” button. - VladD

1 answer 1

The TreeView has an AfterSelect event that occurs after a node has been selected in the tree. As an argument, the variable e is passed to the event handler, including the link to the node itself. For example, you can display the node name in the following image in the event handler: MessageBox.Show(e.Node.Text);

Thus, we solved the task of tracking the selection of the TreeView element through the AfterSelect event.

In general, the Node tree element has the Tag property, in which you can store a lot of things, but we will try to store the path to the file there. To do this, when creating a node, it will be enough to do this:

 string Filename = @"d:\folder\somefile.txt"; NewNode.Tag = Filename; 

Then our event handler will look something like this:

 private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { string fn = (string)e.Node.Tag; if (File.Exists(fn)) { richTextBox1.LoadFile(fn, RichTextBoxStreamType.PlainText); //или так: //richTextBox1.LoadFile(fn); } } 

Pay attention - an attempt to download a file from the text will be made when you select a node from the keyboard or with a single click of the mouse button.

  • Maybe I did something wrong, but nothing happens when I select a file. - Andrei
  • Do you have an exactly assigned event handler for treeView1 for the AfterSelect event? And is it exactly assigned to the treeView1_AfterSelect method? - BlackWitcher
  • private void treeView1_AfterSelect (object sender, TreeViewEventArgs e) - Andrey
  • And in what? And in your tree building method public string derevo_papok(string path) you specify tn.Tag ? - BlackWitcher
  • In the handler that you threw off. Okay, thanks for the help, I will not torment you, I will understand it myself. - Andrei