TextBox 和 numericUpDown 貌似不能共存以下是源代码,在Form1中有TextBox、NumericUpDown、Button三个控件。点击按钮事件(调用buildUpControls方法)出现错误(TextBox的Name变成""),如果把NumericUpDown控件去掉或者放到一个容器里,就正常了,请各位看看,是我的代码有问题还是这个控件的问题。
PS:如果不在这个方法里,好像又没有问题Form1.csusing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace BugTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e)
        {
            Dictionary<string, ControlItem> controls = new Dictionary<string, ControlItem>();
            buildUpControls(this.Controls,controls);
        }        private Dictionary<string, ControlItem> buildUpControls(Control.ControlCollection controlCollection, Dictionary<string, ControlItem> controls)
        {
            ControlItem controlItem;
            foreach (Control control in controlCollection)
            {
                if ((control is TextBox) || (control is ComboBox) || (control is DateTimePicker))
                {
                    string name = control.Name;
                    Console.WriteLine(name);
                    controlItem = new ControlItem(name, name.Substring(3), control.GetType().ToString(), control.Text);
                    controls.Add(controlItem.FullName, controlItem);
                    continue;
                }
                if (control is RadioButton)
                {
                    controlItem = new ControlItem(control.Name, control.Tag.ToString(), control.GetType().ToString(), control.Text);
                    controls.Add(controlItem.FullName, controlItem);
                    continue;
                }
                if (control is ContainerControl)
                {
                    buildUpControls(control.Controls, controls);
                    continue;
                }
            }
            return controls;
        }
    }
}

解决方案 »

  1.   

    ControlItem.csusing System;
    using System.Collections.Generic;
    using System.Text;
    namespace BugTest
    {
        class ControlItem
        {
            public ControlItem() { }
            public ControlItem(string fullName, string simpleName, string controlType, string text)
            {
                this.fullName = fullName;
                this.simpleName = simpleName;
                this.controlType = controlType;
                this.text = text;
            }
            private string fullName;        public string FullName
            {
                get { return fullName; }
                set { fullName = value; }
            }        private string simpleName;        public string SimpleName
            {
                get { return simpleName; }
                set { simpleName = value; }
            }        private string controlType;        public string ControlType
            {
                get { return controlType; }
                set { controlType = value; }
            }
            private string text;        public string Text
            {
                get { return text; }
                set { text = value; }
            }    }
    }
      

  2.   

    调试一下就知道了,当遍历到NumericUpDown时,其有一个TextBox子控件,但是它没有Name属性,所以当你调用SubString的时候就出错了
    修改一下:                    if (!string.IsNullOrEmpty(name))
                        {
                            controlItem = new ControlItem(name, name.Substring(3), control.GetType().ToString(), control.Text);
                            controls.Add(controlItem.FullName, controlItem);
                        }
      

  3.   

    谢谢ojlovecd
    给分,结贴,呵呵