Home > Net >  How to get a child node of the full path of the treeview
How to get a child node of the full path of the treeview

Time:11-23

How to generate child nodes in I click the treeview (after file text file name), access to the I click on the file path of the files in the folder?

CodePudding user response:

String getPath (TreeNode tn)
{
String r=tn. The Text;
While (tn. The Parent!=null)
{
Tn=tn. The Parent;
R=tn. Text + "/" + r;
}
return r;
}

CodePudding user response:

reference 1/f, guiyang horse Ma Shanfu plugging waterproof engineering professional maintenance of swimming pool response:
string getPath (TreeNode tn)
{
String r=tn. The Text;
While (tn. The Parent!=null)
{
Tn=tn. The Parent;
R=tn. Text + "/" + r;
}
return r;
}


Private void TreeView1_Click (System. The Object sender, System. EventArgs e)
{
TreeNode tn.
String path;
Path=getPath (tn);
Using (FileStream fsRead=new FileStream (path, FileMode OpenOrCreate, FileAccess. Read))
{
Byte [] buffer=new byte [5242880] {};
Int r1=fsRead. Read (buffer, 0, buffer Length);
TextBox1, Text=Encoding UTF8. Get string (buffer, 0, r1);
}
}

Private string getPath (TreeNode tn)
{
String r=tn. The Text;
While (tn. The Parent!=null)
{
Tn=tn. The Parent;
R=tn. Text + "/" + r;
}
return r;
}

Because in the learning, so many do not understand, why I add you pick up the full path function after giving the following information

Warning 1 variable "tn" before the assignment is used, may lead to null reference exceptions at runtime,

CodePudding user response:

TreeView1_Click not with this, it should be use TreeView1_NodeMouseClick,
Method 1: if the parent node is a file path, child nodes is file name
//add:
TreeNode parentNode=new TreeNode (@ "C: \ ");//the parent node is the path to the file
TreeNode childNode=new TreeNode (@ "1. TXT");//child node is the filename
ParentNode. Nodes. The Add (childNode);
TreeView1. Nodes. The Add (parentNode);
//double gain
Private void TreeView1_NodeMouseClick (object sender, TreeNodeMouseClickEventArgs e)
{
TreeNode childNode=e.N ode;
If (childNode. Level==1)//click on the node Level is 1, namely whether child node
{
MessageBox. Show (childNode. Parent. Text + childNode. Text);
}
}

Method 2: add a Tag to node Tag
//add
String fullPath=@ "C: \ \ 1. TXT";
TreeNode childNode=new TreeNode ();//child node is the filename
ChildNode. Text=Path. GetFileName (fullPath);
ChildNode. Tag=fullPath;
TreeView1. Nodes. The Add (childNode);
//get
Private void TreeView1_NodeMouseClick (object sender, TreeNodeMouseClickEventArgs e)
{
TreeNode childNode=e.N ode;
If (the File. The Exists (childNode. Tag. The ToString ()))
{
MessageBox. Show (childNode. Tag. The ToString ());
}
}

CodePudding user response:

The
reference 3 floor kei ℡ response:
TreeView1_Click not use this, should be to use TreeView1_NodeMouseClick,
Method 1: if the parent node is a file path, child nodes is file name
//add:
TreeNode parentNode=new TreeNode (@ "C: \ ");//the parent node is the path to the file
TreeNode childNode=new TreeNode (@ "1. TXT");//child node is the filename
ParentNode. Nodes. The Add (childNode);
TreeView1. Nodes. The Add (parentNode);
//double gain
Private void TreeView1_NodeMouseClick (object sender, TreeNodeMouseClickEventArgs e)
{
TreeNode childNode=e.N ode;
If (childNode. Level==1)//click on the node Level is 1, namely whether child node
{
MessageBox. Show (childNode. Parent. Text + childNode. Text);
}
}

Method 2: add a Tag to node Tag
//add
String fullPath=@ "C: \ \ 1. TXT";
TreeNode childNode=new TreeNode ();//child node is the filename
ChildNode. Text=Path. GetFileName (fullPath);
ChildNode. Tag=fullPath;
TreeView1. Nodes. The Add (childNode);
//get
Private void TreeView1_NodeMouseClick (object sender, TreeNodeMouseClickEventArgs e)
{
TreeNode childNode=e.N ode;
If (the File. The Exists (childNode. Tag. The ToString ()))
{
MessageBox. Show (childNode. Tag. The ToString ());
}
}

Thank god! Let me try

CodePudding user response:

reference 2 floor brick response: 98
Quote: reference 1/f, guiyang horse Ma Shanfu plugging waterproof engineering professional maintenance of swimming pool response:
string getPath (TreeNode tn)
{
String r=tn. The Text;
While (tn. The Parent!=null)
{
Tn=tn. The Parent;
R=tn. Text + "/" + r;
}
return r;
}


Private void TreeView1_Click (System. The Object sender, System. EventArgs e)
{
TreeNode tn.
String path;
Path=getPath (tn);
Using (FileStream fsRead=new FileStream (path, FileMode OpenOrCreate, FileAccess. Read))
{
Byte [] buffer=new byte [5242880] {};
Int r1=fsRead. Read (buffer, 0, buffer Length);
TextBox1, Text=Encoding UTF8. Get string (buffer, 0, r1);
}
}

Private string getPath (TreeNode tn)
{
String r=tn. The Text;
While (tn. The Parent!=null)
{
Tn=tn. The Parent;
R=tn. Text + "/" + r;
}
return r;
}

Because in the learning, so many do not understand, why I add you pick up the full path function after giving the following information

Warning 1 variable "tn" before the assignment is used, may lead to null reference exceptions at runtime,


TreeNode tn.
- & gt;
TreeNode tn=treeView1. SelectedNode;
Or in NodeClick
TreeNode tn=e.N ode;nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related