有一个!toolstrip1 上面 有toolstripbutton  还有toolstriplabel 还有
toolStripSeparator我只想遍历到button  怎么弄啊?
string[] a = GlobalClass.userinfo.Qx;
            foreach (ToolStripButton mi in toolStrip1.Items)
            {
                mi.Enabled = false;
                for (int i = 0; i < a.Length; i++)
                {
                    if (a[i] == mi.Tag.ToString())
                    {
                        mi.Enabled = true;
                        i = a.Length;
                    }
                }            }这么写  报错 无法将toolstriplabel 强制转换为toolstripbutton  
求解释帝!!

解决方案 »

  1.   

    要加个类型判断
    if (mi.GetType() == typeof(ToolStripButton))foreach (ToolStripItem  mi in toolStrip1.Items)
                {
                    
                    if (mi.GetType() == typeof(ToolStripButton))
                    {
                        mi.Enabled = false;
                        for (int i = 0; i < a.Length; i++)
                        {
                            if (a[i] == mi.Tag.ToString())
                            {
                                mi.Enabled = true;
                                i = a.Length;
                            }
                        }                }
                }
      

  2.   

    typeof 
    或is 判断类型
      

  3.   


                foreach (ToolStripItem mi in toolStrip1.Items)
                {
                    mi.Enabled = false;
                    if (mi is ToolStripButton)
                    {
                        Console.WriteLine("Button");
                    }
                                }
      

  4.   

    会啦! 嘿嘿  
    我强制转换了下!!!foreach (object mi in toolStrip1.Items)
                {
                    if (mi is ToolStripButton)
                    {
                        ((ToolStripButton)mi).Enabled = false;
                        for (int i = 0; i < a.Length; i++)
                        {
                            if (a[i] == ((ToolStripButton)mi).Tag.ToString())
                            {
                                ((ToolStripButton)mi).Enabled = true;
                                i = a.Length;
                            }
                        }
                    }
                }
      

  5.   

     ToolStripMenuItem ms = new ToolStripMenuItem();
     ms = (System.Windows.Forms.ToolStripMenuItem)toolstrip1.Items[];
    强制转换