开发者

MouseHover on Root node of a tree view

I am having a treeview with one root node . I have written MouseHoverEvent as follows

   private void tvwACH_NodeMouseHover(object sender, TreeNodeMouseHoverEventArgs e)
    {

        string strFile = string.Empty;
        if (e.Node.Parent.Text == "FileHeader")
        {
            strFile = e.Node.ToString();

            string str = strFile.Substring(10);
            StringComparison compareType = StringComparison.InvariantCultureIgnoreCase;
            string fileName = Path.GetFileNameWithoutExtension(str);
            string extension = Path.GetExtension(str);
            if (extension.Equals(".txt", compareType))
            {

                StringBuilder osb = new StringBuilder();
                objFileHeader.getFileHeader(str, out osb);
                e.Node.ToolTipText = Convert.ToString(osb);
            }
        }

    }

But if i had my mouse on the root node i am getting an error as null exceptio handled. If 开发者_如何转开发i had my mouse hover the root node nothing should be happened. Can any one help me please.


private void tvwACH_NodeMouseHover(object sender, TreeNodeMouseHoverEventArgs e)
{
     string strFile = string.Empty;

     // the problem is here, root node does not have a parent
     // also added a fix
     if (e.Node.Parent != null && e.Node.Parent.Text == "FileHeader")
     {
          strFile = e.Node.ToString();

          string str = strFile.Substring(10);
          StringComparison compareType = StringComparison.InvariantCultureIgnoreCase;
          string fileName = Path.GetFileNameWithoutExtension(str);
          string extension = Path.GetExtension(str);
          if (extension.Equals(".txt", compareType))
          {
              StringBuilder osb = new StringBuilder();
              objFileHeader.getFileHeader(str, out osb);
              e.Node.ToolTipText = Convert.ToString(osb);
          }
     }
}


Root Node does not have parent node set.So you should not refer e.Node.Parent.Text for Root node. You need to use conditional statement checking whether this node is root node or not.If current node is root you should properly handle it.You can handle the exceptions as well to resolve this problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜