System.Windows.Forms.NumericUpDown myControl=new System.Windows.Forms.NumericUpDown();if(myControl.GetType().ToString() == "System.Windows.Forms.UpDownBase+UpDownEdit")
{
   //这里肯定可以执行到的。
   //因为myContorl类型就是System.Windows.Forms.NumericUpDown
   //但问题没有System.Windows.Forms.UpDownBase+UpDownEdit这个类型的控件。
   //System.Windows.Forms.UpDownBase这类型倒有。
}if(myControl.GetType().ToString() == "System.Windows.Forms.NumericUpDown")
{
   System.Windows.Forms.NumericUpDown tempNumbericeUpDown=myControl as System.Windows.Forms.NumericUpDown;//这句话不会出错,
   //但就是执行过后,temNumbericeUpDown的值还是null的。
   
   //这里很明显就是不可以执行到的。
   //哪System.Windows.Forms.NumericUpDown控件,类型不为System.Windows.Forms.NumericUpDown怎么搞?
}看我以下代码:        private void ResetAllControl(Control Arge_myControl)
        {
            foreach (Control myControl in Arge_myControl.Controls)
            {
                if (!(myControl.HasChildren))
                {
                    if (myControl.GetType().ToString() == "System.Windows.Forms.TextBox")
                    {
                        TextBox tempTexBox = myControl as TextBox;
                        tempTexBox.Text = "";
                        continue;
                    }
                    if (myControl.GetType().ToString() == "System.Windows.Forms.RadioButton")
                    {
                        RadioButton tempRadioButton = myControl as RadioButton;
                        tempRadioButton.Checked = false;
                        continue;
                    }
                    if (myControl.GetType().ToString() == "System.Windows.Forms.CheckBox")
                    {
                        CheckBox tempCheckBox = myControl as CheckBox;
                        tempCheckBox.Checked = false;
                        continue;
                    }
                    //if (myControl.GetType().ToString() == "System.Windows.Forms.NumbericUpDown")//这里是不可以执行到的,我也不知道为啥
                    if (myControl.GetType().ToString() == "System.Windows.Forms.UpDownBase+UpDownEdit")
                    {
                        NumericUpDown tempNumbericUpDown = myControl as NumericUpDown;//这里为null
                        tempNumbericUpDown.Value = 1;//所以对null对象.value是不可能的。会有异常
                        continue;
                    }
                }
                else
                {
                    ResetAllControl(myControl);//递归
                }
            }
        }
调用以上方法时,Control用当前窗体类试度就知道了ResetAllControl(this);就是找不到System.Windows.Forms.NumericUpDown的控件。郁闷,处理不了System.Windows.Forms.NumericUpDown控件啊,我想自己写一个对当前窗体所有自己指定的类型控件都做相庆的初始化代码。吃个饭回来。希望大家解决到。

解决方案 »

  1.   

    今天怎么这么多判断控件类型的:
    if(myControl is NumericUpDown) 足矣。
      

  2.   


    先感谢朋友的帮忙。不过我想说的,还是没有解决我的问题。
    其它原因,可能是NumbericUpDown也是MS开发的一个合成组件。因为我在以下代码处理TextBox.Text=""结果也对这个有NumbericUpDown控件有影了。
            private void ResetAllControl(Control Arge_myControl)
            {
                foreach (Control myControl in Arge_myControl.Controls)
                {
                    if (!(myControl.HasChildren))
                    {
                        //if (myControl.GetType().ToString() == "System.Windows.Forms.TextBox")
                        //这里倒奇怪了,也把我的NumbericUpDown都空为空了,但他的NumbericUpDown.Value还是有的。
                        //这也说明了,原来NumbericUpDown就是一个合成的控件。因为我处理TextBox,也对他有影响。
                        //看来楼上朋友所说的myControl is NumbericUpDown还是解决不了我的问题。
                        if (myControl is System.Windows.Forms.TextBox)
                        {
                            TextBox tempTexBox = myControl as TextBox;
                            tempTexBox.Text = "";
                            continue;
                        }
                        if (myControl.GetType().ToString() == "System.Windows.Forms.RadioButton")
                        {
                            RadioButton tempRadioButton = myControl as RadioButton;
                            tempRadioButton.Checked = false;
                            continue;
                        }
                        if (myControl.GetType().ToString() == "System.Windows.Forms.CheckBox")
                        {
                            CheckBox tempCheckBox = myControl as CheckBox;
                            tempCheckBox.Checked = false;
                            continue;
                        }
                        //if (myControl.GetType().ToString() == "System.Windows.Forms.NumbericUpDown")
                        //if (myControl.GetType().ToString() == "System.Windows.Forms.UpDownBase+UpDownEdit")
                        if(myControl is NumericUpDown)//这里没有这个可以成功执行
                        {
                            NumericUpDown tempNumbericUpDown = myControl as NumericUpDown;//这里为null
                            tempNumbericUpDown.Value = 1;//所以对null对象.value是不可能的。会有异常
                            continue;
                        }
                    }
                    else
                    {
                        ResetAllControl(myControl);//递归
                        //Thread myThread = new Thread(delegate() { ResetAllControl(myControl); });
                        //myThread.Start();                    //myControl.Invoke(mydelegate(),myControl);
                    }
                }
            }
      

  3.   

                        if(myControl is NumericUpDown)//这里没有这个可以成功执行
                        {
                            NumericUpDown tempNumbericUpDown = myControl as NumericUpDown;//这里为null
                            tempNumbericUpDown.Value = 1;//所以对null对象.value是不可能的。会有异常
                            continue;
                        }不论是上面的is判断结果为false,还是使用as结果为null,都说明myControl并不是一个NumericUpDown,哪怕它看起来是。
      

  4.   

    至于上面的is TextBox之类的判断更不会成功。
      

  5.   

    可能NumbericUpDown是从TextBox继承过来的,所以NumbericUpDown is TextBox是会成功的。
    把is NumbericUpDown判断放前面做。没有找到NumbericUpDown的类说明,不是标准库的吧?
      

  6.   

    错了,是winform的控件,也不是继承TextBox,不好意思。
      

  7.   

    foreach (Control myControl in Arge_myControl.Controls)只要Arge_myControl这个控件内的所有子控件不全部是NumericUpDown,is判断肯定会有返回false的时候。正常情况,使用is判断是足够了。
      

  8.   


    恩,刚刚想到另一种方法解决了。其实NumbericUpDown是继续UpDownBase的。NumbericUpDown我就觉得奇怪,他如果是普通控件的吧,我的代码为什么遍历不到他呢?脑子带着这个疑问,现在终于搞到了。就是调测试的时候,一直监视该控件的各各属性。才知道,NumbericUpDown是一个容器,并非普通的控件。因此,他此他我那个代码过滤了所以导致,内部相要处理的没有处理到。if (!(myControl.HasChildren))就是这句话,因为NumbericUpDown是容器。所以执行else里面的代码里的递归了。现在终于解决了。贴一下代码,给一些和我遇到同样问题的朋友可以得到解决。代码如下:
            private void ResetAllControl(Control Arge_myControl)
            {
                foreach (Control myControl in Arge_myControl.Controls)
                {
                    if (!(myControl.HasChildren))
                    {
                        if (myControl.Parent is System.Windows.Forms.NumericUpDown)
                        {
                            NumericUpDown tempNumberic = myControl.Parent as NumericUpDown;
                            tempNumberic.Value = 1;//把实际值改为1,当然这个是不会在UI上显示的                        //所以接下来我们要处理显示在UI的,代码如下:                        Control tempControl = tempNumberic;                        #region 方法一
                            tempControl.Text = tempNumberic.Value.ToString();//将显示在UI上的值显示和实际的temmpNumberic.Value的值一样
                            #endregion                        #region 方法二
                            //foreach (Control internalemyControl in tempNumberic.Controls)
                            //{
                            //    if (internalemyControl is TextBox)
                            //        internalemyControl.Text = tempNumberic.Value.ToString();
                            //    continue;
                            //}
                            continue;
                            #endregion                    }
                        if (myControl is System.Windows.Forms.TextBox)
                        {
                            NumericUpDown temp = myControl as NumericUpDown;
                            if (temp != null)
                            {
                                temp.Value = 1;
                                continue;
                            }
                            else
                            {
                                if (myControl.Parent is System.Windows.Forms.NumericUpDown)//因为NumbericeUpDown的子控件有多么,所以在TextBox里面处理的就不处理什么了,直接跳到下一个控件
                                {
                                    continue;
                                }
                                TextBox tempTexBox = myControl as TextBox;
                                if (tempTexBox.Text == "2" || tempTexBox.Text == "3")
                                {
                                    NumericUpDown t = myControl as NumericUpDown;
                                    if (t != null)
                                    {
                                        t.Value = 1;
                                        MessageBox.Show("可转为Numberic");
                                        continue;
                                    }
                                    else
                                    {
                                        MessageBox.Show("不可转为Numberic");
                                        //continue;
                                    }
                                }
                                tempTexBox.Text = "";
                                continue;
                            }
                        }
                        if (myControl is System.Windows.Forms.RadioButton)
                        {
                            RadioButton tempRadioButton = myControl as RadioButton;
                            tempRadioButton.Checked = false;
                            continue;
                        }
                        if (myControl is System.Windows.Forms.CheckBox)
                        {
                            CheckBox tempCheckBox = myControl as CheckBox;
                            tempCheckBox.Checked = false;
                            continue;
                        }
                    }
                    else
                    {
                        ResetAllControl(myControl);//递归
                        //Thread myThread = new Thread(delegate() { ResetAllControl(myControl); });
                        //myThread.Start();                    //myControl.Invoke(mydelegate(),myControl);
                    }
                }
            }
    好了,既然问题自己解决了。也就准备结贴了。
      

  9.   

    唉,上面代码没有优化的还有一些多余的代码。重新发一下比较好的:
            private void ResetAllControl(Control Arge_myControl)
            {
                foreach (Control myControl in Arge_myControl.Controls)
                {
                    if (!(myControl.HasChildren))
                    {
                        if (myControl.Parent is System.Windows.Forms.NumericUpDown)
                        {
                            NumericUpDown tempNumberic = myControl.Parent as NumericUpDown;
                            tempNumberic.Value = 1;//把实际值改为1,当然这个是不会在UI上显示的                        //所以接下来我们要处理显示在UI的,代码如下:                        Control tempControl = tempNumberic;                        #region 方法一
                            tempControl.Text = tempNumberic.Value.ToString();//将显示在UI上的值显示和实际的temmpNumberic.Value的值一样
                            #endregion                        #region 方法二
                            //foreach (Control internalemyControl in tempNumberic.Controls)
                            //{
                            //    if (internalemyControl is TextBox)
                            //        internalemyControl.Text = tempNumberic.Value.ToString();
                            //    continue;
                            //}
                            continue;
                            #endregion                    }
                        if (myControl is System.Windows.Forms.TextBox)
                        {
                            if (myControl.Parent is System.Windows.Forms.NumericUpDown)//因为NumbericeUpDown的子控件有多么,所以在TextBox里面处理的就不处理什么了,直接跳到下一个控件
                            {
                                continue;
                            }
                            TextBox tempTexBox = myControl as TextBox;
                            if (tempTexBox.Text == "2" || tempTexBox.Text == "3")
                            {
                                NumericUpDown t = myControl as NumericUpDown;
                                if (t != null)
                                {
                                    t.Value = 1;
                                    MessageBox.Show("可转为Numberic");
                                    continue;
                                }
                                else
                                {
                                    MessageBox.Show("不可转为Numberic");
                                    //continue;
                                }
                            }
                            tempTexBox.Text = "";
                            continue;
                        }
                        if (myControl is System.Windows.Forms.RadioButton)
                        {
                            RadioButton tempRadioButton = myControl as RadioButton;
                            tempRadioButton.Checked = false;
                            continue;
                        }
                        if (myControl is System.Windows.Forms.CheckBox)
                        {
                            CheckBox tempCheckBox = myControl as CheckBox;
                            tempCheckBox.Checked = false;
                            continue;
                        }
                    }
                    else
                    {
                        ResetAllControl(myControl);//递归
                    }
                }
            }
      

  10.   

    狂汗,上面还是有多余的测试代码。再发:晕死。
            private void ResetAllControl(Control Arge_myControl)
            {
                foreach (Control myControl in Arge_myControl.Controls)
                {
                    if (!(myControl.HasChildren))
                    {
                        if (myControl.Parent is System.Windows.Forms.NumericUpDown)
                        {
                            NumericUpDown tempNumberic = myControl.Parent as NumericUpDown;
                            tempNumberic.Value = 1;//把实际值改为1,当然这个是不会在UI上显示的                        //所以接下来我们要处理显示在UI的,代码如下:                        Control tempControl = tempNumberic;                        #region 方法一
                            tempControl.Text = tempNumberic.Value.ToString();//将显示在UI上的值显示和实际的temmpNumberic.Value的值一样
                            #endregion                        #region 方法二
                            //foreach (Control internalemyControl in tempNumberic.Controls)
                            //{
                            //    if (internalemyControl is TextBox)
                            //        internalemyControl.Text = tempNumberic.Value.ToString();
                            //    continue;
                            //}
                            continue;
                            #endregion                    }
                        if (myControl is System.Windows.Forms.TextBox)
                        {
                            if (myControl.Parent is System.Windows.Forms.NumericUpDown)//因为NumbericeUpDown的子控件有多么,所以在TextBox里面处理的就不处理什么了,直接跳到下一个控件
                            {
                                continue;
                            }
                            TextBox tempTexBox = myControl as TextBox;
                            tempTexBox.Text = "";
                            continue;
                        }
                        if (myControl is System.Windows.Forms.RadioButton)
                        {
                            RadioButton tempRadioButton = myControl as RadioButton;
                            tempRadioButton.Checked = false;
                            continue;
                        }
                        if (myControl is System.Windows.Forms.CheckBox)
                        {
                            CheckBox tempCheckBox = myControl as CheckBox;
                            tempCheckBox.Checked = false;
                            continue;
                        }
                    }
                    else
                    {
                        ResetAllControl(myControl);//递归
                    }
                }
            }
      

  11.   

    foreach (Control myControl in Arge_myControl.Controls)if (myControl.Parent is System.Windows.Forms.NumericUpDown)
    -----------------------------------------
    看到这些,应该是Arge_myConctrol是NumericUpDown对吧?
    那么,你一开始用来判断的主体就错了。
      

  12.   


    不。
    主要,我不知道NumbericUpDown也是一个容器。
    我前面都总结了可能NumbericUpDown是以一个组件形式的,那他是组件。那他肯定就是容器了。唉。那时真还没考虑到。脑子不够灵活了。
      

  13.   

    以递归的方式,重置所有控件包括子控件的状态。我说错了么?我说的麻烦,指的是这里:
                        if (myControl.Parent is System.Windows.Forms.NumericUpDown)
                        {
                            NumericUpDown tempNumberic = myControl.Parent as NumericUpDown;
                            tempNumberic.Value = 1;//把实际值改为1,当然这个是不会在UI上显示的                        //所以接下来我们要处理显示在UI的,代码如下:                        Control tempControl = tempNumberic;                        #region 方法一
                            tempControl.Text = tempNumberic.Value.ToString();//将显示在UI上的值显示和实际的temmpNumberic.Value的值一样
    --------------------------
    如果你不觉得,我什么都没说...
      

  14.   

    应该用:myControl.GetType() == typeof(System.Windows.Forms.NumericUpDown);
    这才是标准答案。