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控件啊,我想自己写一个对当前窗体所有自己指定的类型控件都做相庆的初始化代码。吃个饭回来。希望大家解决到。
{
//这里肯定可以执行到的。
//因为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控件啊,我想自己写一个对当前窗体所有自己指定的类型控件都做相庆的初始化代码。吃个饭回来。希望大家解决到。
解决方案 »
- 流图片类型
- 怎么认定才能让GridView中字体大小不随所用的浏览器字体的大小变化而变化
- TransactionScope超过10分钟出现Unable to get error message (6107) (0)
- vs2005的插件或补丁
- 服务器同时向多个客户端发送文件
- 求一个"单击两个按钮,怎么知道点击的是哪一个按钮"的问题
- 帮我看看:如何利用存储过程插入数据呢?
- c#中怎样传递一个variant给控件?
- 求一个winfrom做的源码记事本项目,要求(vs08或者vs10做的)
- 水晶报表中的图表?
- XML搜索中的like关键字与 SQL中的like关键字
- 【高手】如何让 C# 控制台程序重复执行并且能够响应响应的事件处理 寻求最优解送100
if(myControl is NumericUpDown) 足矣。
先感谢朋友的帮忙。不过我想说的,还是没有解决我的问题。
其它原因,可能是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);
}
}
}
{
NumericUpDown tempNumbericUpDown = myControl as NumericUpDown;//这里为null
tempNumbericUpDown.Value = 1;//所以对null对象.value是不可能的。会有异常
continue;
}不论是上面的is判断结果为false,还是使用as结果为null,都说明myControl并不是一个NumericUpDown,哪怕它看起来是。
把is NumbericUpDown判断放前面做。没有找到NumbericUpDown的类说明,不是标准库的吧?
恩,刚刚想到另一种方法解决了。其实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);
}
}
}
好了,既然问题自己解决了。也就准备结贴了。
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);//递归
}
}
}
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);//递归
}
}
}
-----------------------------------------
看到这些,应该是Arge_myConctrol是NumericUpDown对吧?
那么,你一开始用来判断的主体就错了。
不。
主要,我不知道NumbericUpDown也是一个容器。
我前面都总结了可能NumbericUpDown是以一个组件形式的,那他是组件。那他肯定就是容器了。唉。那时真还没考虑到。脑子不够灵活了。
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的值一样
--------------------------
如果你不觉得,我什么都没说...
这才是标准答案。