NumericUpDown 类型的问题
当我做这样的事情时:
public static void BindData<T>(this System.Windows.Forms.Control.ControlCollection controls, T bind)
{
foreach (Control control in controls)
{
if (control.GetType() == typeof(System.Windows.Forms.TextBox) || control.GetType().IsSubclassOf(typeof(System.Windows.Forms.TextBox)))
{
UtilityBindData(control, bind);
}
else
{
if (control.Controls.Count == 0)
{
UtilityBindData(control, bind);
}
else
{
control.Controls.BindData(bind);
}
}
}
}
private static void UtilityBindData<T>(Control control, T bind)
{
Type type = control.GetType();
PropertyInfo propertyInfo = type.GetProperty("BindingProperty");
if (propertyInfo == null)
propertyInfo = type.GetProperty("Tag");
// rest of the code....
其中控件是 System.Windows.Forms.Control.ControlCollection
并且在作为参数传递给这段代码的表单上的控件中有 NumericUpDowns,我找不到它们位于控件集合(controls=myForm.Controls)中,但还有其他类型的控件(updownbutton、updownedit)。问题是我想获取 NumericUpDown 的 Tag 属性,但在使用检查表单控件的递归方法时无法获取它。
When i do something like this:
public static void BindData<T>(this System.Windows.Forms.Control.ControlCollection controls, T bind)
{
foreach (Control control in controls)
{
if (control.GetType() == typeof(System.Windows.Forms.TextBox) || control.GetType().IsSubclassOf(typeof(System.Windows.Forms.TextBox)))
{
UtilityBindData(control, bind);
}
else
{
if (control.Controls.Count == 0)
{
UtilityBindData(control, bind);
}
else
{
control.Controls.BindData(bind);
}
}
}
}
private static void UtilityBindData<T>(Control control, T bind)
{
Type type = control.GetType();
PropertyInfo propertyInfo = type.GetProperty("BindingProperty");
if (propertyInfo == null)
propertyInfo = type.GetProperty("Tag");
// rest of the code....
where controls is System.Windows.Forms.Control.ControlCollection
and among controls on the form that is passed as a parameter to this piece of code there are NumericUpDowns, i cant find them in the controls collection (controls=myForm.Controls), but there are controls of other types(updownbutton, updownedit). The problem is that i want to get NumericUpDown's Tag property and just cant get it when using that recursive method of checking form controls.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Tag
属性 由Control
类定义。因此,你根本不需要反思;您可以简单地编写
您的代码不起作用,因为控件的实际类型(例如,
NumericUpDown
)没有定义单独的Tag
属性,并且GetProperty 不搜索基类属性。
顺便说一下,在你的第一个
if
状态中,你可以简单地写The
Tag
property is defined by theControl
class.Therefore, you don't need reflection at all; you can simply write
Your code isn't working because the control's actual type (eg,
NumericUpDown
) doesn't define a separateTag
property, andGetProperty
doesn't search base class properties.By the way, in your first
if
statemeant, you can simply write