代码之家  ›  专栏  ›  技术社区  ›  0x49D1

NumericUpDown类型有问题

  •  0
  • 0x49D1  · 技术社区  · 14 年前

    当我这样做的时候:

    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集合中找不到它们(controls=myForm.controls),但是还有其他类型的控件(updownbutton,updownedit)。问题是我想得到NumericUpDown的Tag属性,但在使用检查表单控件的递归方法时却无法得到它。

    1 回复  |  直到 14 年前
        1
  •  1
  •   SLaks    14 年前

    这个 Tag property 是由 Control 班级。

    object tag = control.Tag;
    

    因为控件的实际类型(例如, NumericUpDown )没有定义一个单独的 财产,以及 GetProperty 不搜索基类属性。


    顺便说一下,在你的第一次 if

    if (control is TextBox)