日期:2014-05-18  浏览次数:21026 次

C# 动态复制控件
情况是这样的,我新建了个tabControl,在tabPage1里面有很多控件下拉的文本的,控件中还有数据,我想将所有控件及数据复制到新的tabPage2中去.要怎么做啊!



------解决方案--------------------
建议你做一个UserControl,把控件装进去。
------解决方案--------------------
得循环遍历,在tabPage1.Controls里遍历.并判断每个控件的类型,有的控件需要复制的内容更多,如ComboBox

foreach(Control ctrl in tabPage1.Controls)
{

if(ctrl.GetType() == typeof(ComboBox))
{
ComboBox cmb = new ComboBox();
cmb.Size = ctrl.Size;
cmb.Location = ctrl.Location;
......
foreach(object item in (ComboBox)ctrl)
{
cmb.Items.Add(item);
}
}
else if(ctrl.GetType() == typeof(TextBox))
 {
 }
}