代码如下:  
private void MainFrm_Load(object sender, EventArgs e) 
{    //通过用户编号查询权限清单控制主窗体菜单使能状态 
   this.sendStrSQL = "select mnuBase,mnuEmployee,mnuBaseCharge,mnuOtherCharge,mnuDeductCharge"; 
   sendStrSQL += ",mnuKaoqin,mnuDinner,mnuDorm,mnuRationHour,mnuInsurance,mnuWage,mnuAdd,mnuManage "; 
   sendStrSQL+="from hr_User where ID = '" + LoginFrm.strID + "'"; 
   DataTable tempDataTable = link.SelectDataBase(sendStrSQL); 
   for (int i = 0; i < tempDataTable.Columns.Count; i++) 
   { 
       if (ushort.Parse(tempDataTable.Rows[0][i].ToString()) == 1) 
       { 
           string name = getFrmName(i);//取出相应的菜单项目name 
           Control control = this.Controls[name]; 
           control.Enabled = true; 
       }    }
}
红色为报错部分,错误显示为:未将对象引用设置到对象的实例

解决方案 »

  1.   

    Control control = this.Controls[name];  //没有找到这个控件
    control.Enabled = true; 
      

  2.   

    Control control = this.Controls[name];
    这一句 没有找到这个name的控件。 
      

  3.   

    在你的窗体上没有ID是name的控件
    Control control = this.Controls[name]; 
    if(control != null)
    {
              control.Enabled = true;
    }
      

  4.   

            //------选择需要修改的项目------
            private string getFrmName(int titleCount)
            {
                switch (titleCount)
                {
                    case 0:
                        return "mnuBase";
                    case 1:
                        return "mnuEmployee";
                    case 2:
                        return "mnuBaseCharge";
                    case 3:
                        return "mnuOtherCharge";
                    case 4:
                        return "mnuDeductCharge";
                    case 5:
                        return "mnuKaoqin";
                    case 6:
                        return "mnuDinner";
                    case 7:
                        return "mnuDorm";
                    case 8:
                        return "mnuRationHour";
                    case 9:
                        return "mnuInsurance";
                    case 10:
                        return "mnuWage";
                    case 11:
                        return "mnuAdd";
                    case 12:
                        return "mnuManage";
                    default:
                        return null;
                }
            }
    在事件里面取的是菜单名称
      

  5.   

    测试了,是没有ID是NAME的控件,该怎么改呢?
      

  6.   

    Control control = this.Controls[name]; 窗体里必须要有名为 name 的控件啊
    你注意 design.cs里面的代码 每个控件new出来后 都有个Name属性
      

  7.   

    使用一个对象前先检查是否为null
      

  8.   

    private System.Windows.Forms.ToolStripMenuItem mnuBase;
    在design.cs页面中是有Name属性的,this.Controls[name]中的name是一个变量,是从getFrmName(int titleCount)过程的返回值,该过程我已经贴出来了。现在可能的原因是
    Control control = this.Controls[name]没有取出值。
    我的意思是把菜单的名称动态取出来,然后对它的Enabled属性赋值 
      

  9.   


    this.Controls[name] 这个name参数是字符串 我知道是从getFrmName返回的 比方说你返回的是“mnuBase” 那么你是否生命了一个NAME为“mnuBase”的控件 并且把它添加到this.Controls中了呢?
    你不妨调试一下 this.Controls 到底有哪些数据
      

  10.   

    我建议 不要用this.Controls  你可以自己声明一个hashtable KEY就是 getFrmName 方法中的那些值 或者 tempDataTable.Rows[0][i] 中的值 value就是对应的控件 在声明控件的时候 添加进去 
      

  11.   

    this.Controls[name]这是什么控件?
      

  12.   


    if (ushort.Parse(tempDataTable.Rows[0][i].ToString()) == 1)
          {
              string name = getFrmName(i);//取出相应的菜单项目name
              Control control = this.Controls[name];
              control.Enabled = true;
          } 这段代码,还是好好的去跟踪一下吧,看看这里的 this.Controls[name]是指什么,到底存不存在。
      

  13.   

    大家都说了,你只要去判断一下是不是存在那个控件名称了
    我估计是你自己的名字有差错,你仔细检查一下!
    Control control = this.Controls[name]; 
    if(control != null) 

              control.Enabled = true; 

      

  14.   

     for (int i = 0; i < tempDataTable.Columns.Count; i++) 
      { 
          if (ushort.Parse(tempDataTable.Rows[0][i].ToString()) == 1) 
          { 
             try
             {
              string name = getFrmName(i);//取出相应的菜单项目name         
              Control control = this.Controls[name]; 
              control.Enabled = true;
             } 
             finally
             {
               continue;
             }
          }   } 
    把错误忽略掉
      

  15.   

    你Control 这个只是个容器,你要可用和不可用  你在容器里的控件进行
      

  16.   


     for (int i = 0; i < tempDataTable.Columns.Count; i++) 
      { 
          if (ushort.Parse(tempDataTable.Rows[0][i].ToString()) == 1) 
          { 
              string name = getFrmName(i);//取出相应的菜单项目name 
              //Control control = this.Controls[name]; 
              //control.Enabled = true; 
              Control [] ctls = this.Controls.Find(name,false);
              if(ctls.Length > 0)
              {
                ctls[0].Enabled = true;
              }
          } 
      } 

      

  17.   

    Control control = this.Controls["name"]; 
    if(control != null) 

              control.Enabled = true; 
    }
      

  18.   

    用最原始的方法解决了
    判断后直接用
    this.mnuBase.Enabled=ture;
      

  19.   

    this.controls[...] 只能查找this这个对象下面一层的子控件,并不能递归查找子子空间(后代的后代)
    ls的代码很可能是因为这个问题,而导致出现这个错误