[C#] 纯文本查看 复制代码
/// <summary>
/// 泛型清除内容.T类型注意与控件一致,如果你的控件为自定义控件,记得是自定义控件的类名哦
/// </summary>
/// <param name="ctrlTop">清除的控件(含子控件)</param>
/// <param name="t">参数代入null即可</param>
public static void ClearText<T>(Control ctrlTop,T t)
{
if (ctrlTop.GetType()==typeof(T))
{
ctrlTop.Text = "";
}
else
{
foreach (Control ctrl in ctrlTop.Controls)
{
ClearText(ctrl,t); //循环调用
}
}
}
[C#] 纯文本查看 复制代码
namespace ClearTexts {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
public static void ClearText<T>(T t) {
var type = t.GetType();
if (type.GetProperty("Text") != null) {
type.GetProperty("Text").SetValue(t, string.Empty, null);
}
if (type.GetProperty("Controls") != null) {
var controls = (type.GetProperty("Controls").GetValue(t, null) as Control.ControlCollection);
foreach (Control item in controls) {
ClearText(item);
}
}
}
private void button1_Click(object sender, EventArgs e) {
ClearText(textBox3);
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {
ClearText(groupBox1);
}
}
}