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;
}
}
}
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;
}
}
}
解决方案 »
- rdlc报表,矩阵问题请教
- 刚学编程2月,想编个计算器,迷糊写了点代码有人能帮我看看代码吗?现在对编程还迷迷糊糊的
- 各高手请进,关于数据传输程序
- 关于像素的问题
- datagrid的问题
- 求drawArc和fillPolygon的具体实现方法(用画像素点setPixel的方法)
- See the end of this message for details on invoking just-in-time (JIT) debugging
- 搞定!!通过PictureBox播放视频,为实现诺言,加贴,给出本人的代码与大家交流,并给正解者zoujiaming两百分!!!
- C#的跨对象操作问题初探,希望你来看看,发表一下见解。
- 如何实现两个listview之间可以实现拖放?
- C# 怎样将8bit的图像转换成24bit
- 如何读取datatable.select的内容?
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; }
} }
}
修改一下: if (!string.IsNullOrEmpty(name))
{
controlItem = new ControlItem(name, name.Substring(3), control.GetType().ToString(), control.Text);
controls.Add(controlItem.FullName, controlItem);
}
给分,结贴,呵呵