17,748
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApp2.Command
{
internal static class StaticHelper
{
internal static T LastByName<T>(this System.Windows.Forms.Control.ControlCollection cll, Func<T, bool> 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));
}
IList<T> list = new List<T>();
foreach (T item in cll)
{
list.Add(item);
}
list.OrderBy(col => col.Name);
return list.Last(func);
}
internal static T Clone<T>(this T col, Func<System.Windows.Forms.Control, string> funcName) where T : System.Windows.Forms.Control
{
col.SuspendLayout();
if (col == null)
{
return null;
}
Type colType =typeof(T);
System.Windows.Forms.Control restultCol = Activator.CreateInstance(colType) as System.Windows.Forms.Control;
System.Reflection.PropertyInfo[] proInfo = colType.GetProperties();
System.Reflection.FieldInfo[] fieInfo = colType.GetFields();
foreach (System.Reflection.FieldInfo item in fieInfo)
{
object newValue = item.GetValue(col);
if (newValue == null)
{
continue;
}
item.SetValue(restultCol, newValue);
}
foreach (System.Reflection.PropertyInfo item in proInfo)
{
object newValue = item.GetValue(col);
if (newValue == null)
{
continue;
}
if (item.Name == "Name")
{
newValue = funcName(col);
}
if (item.CanWrite)
{
item.SetValue(restultCol, newValue);
}
}
if (col.Controls.Count > 0)
{
foreach (System.Windows.Forms.Control item in col.Controls)
{
restultCol.Controls.Add(item.Clone(funcName));
}
}
col.ResumeLayout();
return restultCol as T;
}
}
}
/// <summary>
/// 复制一个一模一样的控件
/// </summary>
/// <param name="col"></param>
/// <returns></returns>
private void AddGroupBox()
{
this.groupBoxResult.Hide();
var regex = new System.Text.RegularExpressions.Regex(@"(\D+)(\d+)");
List<Tuple<Type, int>> tuples = this.GetTuples(this.groupBoxBroder);
GroupBox group = this.groupBox1.Clone(t =>
{
if (regex.IsMatch(t.Name))
{
string firstName = regex.Matches(t.Name)[0].Groups[1].Value;
string endIndex = regex.Matches(t.Name)[0].Groups[2].Value;
int tempNum = int.Parse(endIndex);
Tuple<Type, int> tuple = tuples.FirstOrDefault(s => s.Item1 == t.GetType());
if (tuple != null)
{
while (tempNum <= tuple.Item2)
{
tempNum++;
}
}
else
{
}
tuples.Remove(tuple);
tuples.Add(new Tuple<Type, int>(t.GetType(), tempNum));
return firstName + tempNum;
}
else
{
return t.Name;
}
});
System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(@"^groupBox\d+$");
group.Location = new Point(173, 40);
GroupBox group1 = this.groupBoxBroder.Controls.LastByName<GroupBox>(t => reg.IsMatch(t.Name));
group1.Location = new Point(173, 80);
}
)
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Text = "点击鼠标,增加选项";
this.MouseClick += 添加选项;
}
private void 添加选项(object sender, MouseEventArgs e)
{
var g = new MyGroupControl();
g.Location = new Point(10, 选项数目 * (g.Height + 10));
this.Controls.Add(g);
选项数目++;
}
int 选项数目;
}
// 用户控件一般用可视编辑,这里直接用代码简化例子。
public class MyGroupControl : UserControl
{
GroupBox _groupBox = new GroupBox() { Dock = DockStyle.Fill, Text = "配置选项" };
FlowLayoutPanel _flowPanel = new FlowLayoutPanel() { Dock = DockStyle.Fill };
CheckBox _checkBox1 = new CheckBox() { Text = "彩色" };
CheckBox _checkBox2 = new CheckBox() { Text = "横向" };
CheckBox _checkBox3 = new CheckBox() { Text = "高清" };
public MyGroupControl()
{
_flowPanel.Controls.AddRange(new[] { _checkBox1, _checkBox2, _checkBox3 });
_groupBox.Controls.Add(_flowPanel);
this.Controls.Add(_groupBox);
this.Size = new Size(180, 120);
}
}
}
