你每个foreach后都应该返回,由于有时并不进入foreach,原因其不满足,即集合为空。

解决方案 »

  1.   

    如下就行了
    private TreeNode searchtreeview(string year,string month,string day)//如果没有记录,则返回null,有年tny,有月tnm,有日tnd,
    {
    int cny = tvlog.Nodes[0].Nodes.Count;
    if (cny != 0)
    {
    foreach(TreeNode tny in tvlog.Nodes[0].Nodes)
    {
    if (year == tny.Text)
    {
    MessageBox.Show("当前年纪录已经存在");
    //查找到年记录存在,继续查找月记录是否存在
    foreach(TreeNode tnm in tny.Nodes)
    {
    if (month == tnm.Text)
    {
    MessageBox.Show("当前月记录已经存在");//找到月记录,继续查找日记录
    foreach(TreeNode tnd in tnm.Nodes)
    {
    if (day == tnd.Text)
    {
    MessageBox.Show("找到日记录");
    return tnd;
    }
    else//没有相匹配的日,返回tnm
    {
    return tnm;
    }
    }
    return tnm;
    }
    else//如果月记录不存在,则返回tny
    {
    return tny;
    }
    }
    return tny;
    }
    else//没有相匹配的年纪录,返回null
    {
    return null;
    }
    }
    return null;
    }
    else
    {
    return null;
    }
    }