当然了,在foreach语句块的后面加上
return null;

解决方案 »

  1.   

    当pForm.Controls里面没有Button的时候,返回什么?
    修改如下:
    public static Button GetButton(string pButtonName,Form pForm)
    {
    foreach (Control c in pForm.Controls)
    {
    if (c.Name==pButtonName && c is Button) return (Button)c;
    }
             return null;
    }
      

  2.   

    你只有一个条件有返回值
    public static Button GetButton(string pButtonName,Form pForm)
    {
    foreach (Control c in pForm.Controls)
    {
    if (c.Name==pButtonName && c is Button) return (Button)c;
                      else return null;
    }
    }