Опять глюки... Сначала создал класс наследник от TreeNode, потому-что мой TreeView напрямую работает с файлами,и для удобства было бы удобно хранить полный путь к файлу прямо в TreeNode. Проблема в том, что при первом обращении к selectedNode его значение полного пути m_strPathName всегда равен родительскому, а во второй раз - нормальное значение. В чем бока?
| Код | public class FileTreeNode : TreeNode { public string m_strPathName = ""; }
public partial class Form1 : Form { public Form1() { InitializeComponent(); InitTreeView(treeView1); } private void InitTreeView(System.Windows.Forms.TreeView tr) { DriveInfo[]dr = DriveInfo.GetDrives(); FileTreeNode nod = null; foreach (DriveInfo d in dr) { nod = new FileTreeNode(); nod.m_strPathName = d.Name; nod.Text = d.Name; tr.Nodes.Add(nod); } } void treeView1_NodeMouseClick(object sender, System.Windows.Forms.TreeNodeMouseClickEventArgs e) { System.Windows.Forms.TreeView tr = (System.Windows.Forms.TreeView)sender; System.Windows.Forms.TreeNode node = tr.SelectedNode; FileTreeNode fnod = (FileTreeNode)node; DirectoryInfo dirsInf=null; FileInfo[] files = null; DirectoryInfo[] dirs = null; try { dirsInf = new DirectoryInfo(fnod.m_strPathName); files = dirsInf.GetFiles(); dirs = dirsInf.GetDirectories(); } catch { return; } fnod.Nodes.Clear(); FileTreeNode tmpNod = null; foreach (DirectoryInfo d in dirs) { tmpNod = new FileTreeNode(); tmpNod.m_strPathName = d.FullName; tmpNod.Text = d.Name; fnod.Nodes.Add(tmpNod); } // throw new System.Exception("The method or operation is not implemented."); } }
|
|