using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. The Text;
Mand namespace WindowsFormsApp2.Com
{
Internal static class StaticHelper
{
Internal static T LastByName(this System. Windows. Forms. Control. ControlCollection CLL, Func Func) where T: System. Windows. Forms. Control
{
If (CLL is null)
{
Throw new ArgumentNullException (nameof (CLL));
}
If (func is null)
{
Throw new ArgumentNullException (nameof func ());
}
IListList=new List (a);
The foreach (T item in CLL)
{
List. The Add (item);
}
List. OrderBy (col=& gt; Col. Name);
Return the list. The Last (func);
}
Internal static T Clone(this T col, Func FuncName) where T: System. Windows. Forms. Control
{
Col. SuspendLayout ();
If (col==null)
{
return null;
}
Type colType=typeof (T);
System. Windows. Forms. Control restultCol=the Activator. The CreateInstance (colType) as System. Windows. Forms. The Control;
System. Reflection. PropertyInfo [] proInfo=colType. The GetProperties ();
System. Reflection. FieldInfo [] fieInfo=colType. The GetFields ();
Foreach (System. Reflection. FieldInfo item in fieInfo)
{
The object newValue=https://bbs.csdn.net/topics/item.GetValue (col);
If (newValue=null https://bbs.csdn.net/topics/=
{
continue;
}
Item. SetValue (restultCol, newValue);
}
Foreach (System. Reflection. PropertyInfo item in proInfo)
{
The object newValue=https://bbs.csdn.net/topics/item.GetValue (col);
If (newValue=null https://bbs.csdn.net/topics/=
{
continue;
}
The if (item Name=="Name")
{
NewValue=https://bbs.csdn.net/topics/funcName (col);
}
If (item. CanWrite)
{
Item. SetValue (restultCol, newValue);
}
}
If (col. Controls. Count & gt; 0)
{
Foreach (System. Windows. Forms. The Control item in col. Controls)
{
RestultCol. Controls. The Add (item. Clone (funcName));
}
}
Col. ResumeLayout ();
Return restultCol as T.
}
}
}
///& lt; Summary>
///copy an identical control
///& lt;/summary>
///& lt; Param name="col" & gt; & lt;/param>
///& lt; Returns> & lt;/returns>
Private void AddGroupBox ()
{
This. GroupBoxResult. Hide ();
Var regex=new System. Text. RegularExpressions. Regex (@ "(\ D +) (\ D +)");
ListTuples=this. GetTuples (enclosing groupBoxBroder);
GroupBox group=this. GroupBox1. Clone (t=& gt;
{
If (regex. IsMatch (t.N ame))
{
String firstName=regex Matches (t.N ame) [0]. Groups [1]. The Value;
String endIndex=regex Matches (t.N ame) [0]. Groups. [2] the Value;
Int tempNum=int. Parse (endIndex);
TupleThe tuple=tuples. FirstOrDefault (s=& gt; S.I tem1==t.G etType ());
If (a tuple!=null)
{
While (tempNum & lt;=the tuple. Item2)
{
TempNum++;
}
}
The else
{
}
Tuples. Remove (a tuple);
Tuples. Add (new Tuple(t.G etType (), tempNum));
Return firstName + tempNum;
}
The else
{
Return t.N ame.
}
});
System. Text. RegularExpressions. Regex reg=new System. Text. RegularExpressions. The Regex (@ "^ groupBox \ d + $");
Group. The Location=new Point (173, 40);
GroupBox group1=this. GroupBoxBroder. Controls. LastByName(t=& gt; Reg. IsMatch (t.N ame));
Group1. Location=new Point (173, 80);
}
Test code example:
CodePudding user response:
not copy Winform controls,Because Winform controls on the bottom, it is supported by the Windows kernel object, simply copy the Control of private members are not regular and the practice of safety,
CodePudding user response: