C#如何判断某个控件类型为System.Windows.Forms.NumericUpDown

Jave.Lin 2009-08-07 12:29:31
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控件啊,我想自己写一个对当前窗体所有自己指定的类型控件都做相庆的初始化代码。

吃个饭回来。希望大家解决到。
...全文
2731 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
mjp1234airen4385 2009-08-08
  • 打赏
  • 举报
回复 1
应该用:myControl.GetType() == typeof(System.Windows.Forms.NumericUpDown);
这才是标准答案。
dancingbit 2009-08-07
  • 打赏
  • 举报
回复
今天怎么这么多判断控件类型的:
if(myControl is NumericUpDown)

足矣。
Jave.Lin 2009-08-07
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 dancingbit 的回复:]
以递归的方式,重置所有控件包括子控件的状态。我说错了么?

我说的麻烦,指的是这里:
                    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的值一样


--------------------------
如果你不觉得,我什么都没说...
[/Quote]

不这样做,还能怎么做?

因为我窗体内有很多容器控件。

而我要是不用这种方法。

那么,很多容器控件里的子控件就不可以重置了。

哪有麻烦。

那你给个更好的算法我瞅瞅。

好的话,我肯定会要的。
dancingbit 2009-08-07
  • 打赏
  • 举报
回复
以递归的方式,重置所有控件包括子控件的状态。我说错了么?

我说的麻烦,指的是这里:
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的值一样


--------------------------
如果你不觉得,我什么都没说...
Jave.Lin 2009-08-07
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 dancingbit 的回复:]
如果我的判断不错的话,不知道为什么要搞得这么麻烦?
[/Quote]

什么?这句话不理解?

什么你的“判断不错的话,不知道为什么要搞得这么麻烦?”

不懂。。。

你意思是说:“没必要搞这么麻烦”?

那你知道我那些代码是有什么用吗?

如果你知道我那些代码作用。就不会说这样的话。

除非说你有更好的窗体重置。效果。

以前我自己也用过,点“重置”按钮,就是将当前窗体关了,再重新打开,这样就初始化了?

这是笨方法。
Jave.Lin 2009-08-07
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 dancingbit 的回复:]
foreach (Control myControl in Arge_myControl.Controls)

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

不。
主要,我不知道NumbericUpDown也是一个容器。
我前面都总结了可能NumbericUpDown是以一个组件形式的,那他是组件。那他肯定就是容器了。

唉。那时真还没考虑到。脑子不够灵活了。
dancingbit 2009-08-07
  • 打赏
  • 举报
回复
如果我的判断不错的话,不知道为什么要搞得这么麻烦?
dancingbit 2009-08-07
  • 打赏
  • 举报
回复
foreach (Control myControl in Arge_myControl.Controls)

if (myControl.Parent is System.Windows.Forms.NumericUpDown)
-----------------------------------------
看到这些,应该是Arge_myConctrol是NumericUpDown对吧?
那么,你一开始用来判断的主体就错了。
Jave.Lin 2009-08-07
  • 打赏
  • 举报
回复
狂汗,上面还是有多余的测试代码。

再发:

晕死。


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);//递归
}
}
}
Jave.Lin 2009-08-07
  • 打赏
  • 举报
回复
唉,上面代码没有优化的还有一些多余的代码。

重新发一下比较好的:


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);//递归
}
}
}
Jave.Lin 2009-08-07
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 lunasea0_0 的回复:]
错了,是winform的控件,也不是继承TextBox,不好意思。
[/Quote]

恩,刚刚想到另一种方法解决了。

其实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);
}
}
}


好了,既然问题自己解决了。

也就准备结贴了。
dancingbit 2009-08-07
  • 打赏
  • 举报
回复
foreach (Control myControl in Arge_myControl.Controls)

只要Arge_myControl这个控件内的所有子控件不全部是NumericUpDown,is判断肯定会有返回false的时候。

正常情况,使用is判断是足够了。
Jave.Lin 2009-08-07
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 dancingbit 的回复:]
if(myControl is NumericUpDown)//这里没有这个可以成功执行
                    {
                        NumericUpDown tempNumbericUpDown = myControl as NumericUpDown;//这里为null
                        tempNumbericUpDown.Value = 1;//所以对null对象.value是不可能的。会有异常
                        continue;
                    }

不论是上面的is判断结果为false,还是使用as结果为null,都说明myControl并不是一个NumericUpDown,哪怕它看起来是。
[/Quote]

那我该怎么解决我现在遇到的问题呢。
lunasea0_0 2009-08-07
  • 打赏
  • 举报
回复
错了,是winform的控件,也不是继承TextBox,不好意思。
lunasea0_0 2009-08-07
  • 打赏
  • 举报
回复
可能NumbericUpDown是从TextBox继承过来的,所以NumbericUpDown is TextBox是会成功的。
把is NumbericUpDown判断放前面做。

没有找到NumbericUpDown的类说明,不是标准库的吧?
dancingbit 2009-08-07
  • 打赏
  • 举报
回复
至于上面的is TextBox之类的判断更不会成功。
dancingbit 2009-08-07
  • 打赏
  • 举报
回复
if(myControl is NumericUpDown)//这里没有这个可以成功执行
{
NumericUpDown tempNumbericUpDown = myControl as NumericUpDown;//这里为null
tempNumbericUpDown.Value = 1;//所以对null对象.value是不可能的。会有异常
continue;
}

不论是上面的is判断结果为false,还是使用as结果为null,都说明myControl并不是一个NumericUpDown,哪怕它看起来是。
Jave.Lin 2009-08-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 dancingbit 的回复:]
今天怎么这么多判断控件类型的:
if(myControl is NumericUpDown)

足矣。
[/Quote]

先感谢朋友的帮忙。

不过我想说的,还是没有解决我的问题。
其它原因,可能是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);
}
}
}

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧