哦,难道是我的控件有问题么? 我的控件代码:using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;namespace Login { public partial class myControl : UserControl { public myControl() { InitializeComponent(); } public TextBox myTextbox{ get { return textBox1; } set { textBox1 = value; } } public Label myLabel{ get { return label1; } set { label1 = value; } } private void myControl_Load(object sender, EventArgs e) {
} } }
public partial class Form1 : HSEI.Windows.Forms.RobbinForm { private List<TextBox> tbList = new List<TextBox>(); public Form1() { InitializeComponent(); Init(); } private void Init() { for(int i=0;i<10;i++) { TextBox tb = new TextBox(); tb.Text = ""; tbList.Add(tb); //TODO Layot Add Control } } private void Reset() { foreach(var tb in tbList) { tb.Text = "0"; } } } 你的程序中把TextBox换成自定义控件就行了
你用自定义控件肯定是有问题的。foreach在panel里面遍历到的是你的myControl,他根本就没有Text属性,你要改的是myControl里面的TextBox的Text的属性。改一下Clear按钮下的代码。 UserControl.ControlCollection cc = this.flowLayoutPanel1.Controls; foreach (UserControl con in cc) { if (con is UserControl) { con.Controls[0].Text = "0"; } // else { MessageBox.Show("没有找到文本框!"); } }
赋值完执行一下这个试试
虽然没你那个控件,用普通TextBox测试代码没问题.
我的控件代码:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace Login
{
public partial class myControl : UserControl
{
public myControl()
{
InitializeComponent();
}
public TextBox myTextbox{
get {
return textBox1;
}
set {
textBox1 = value;
}
}
public Label myLabel{
get
{
return label1;
}
set
{
label1 = value;
}
}
private void myControl_Load(object sender, EventArgs e)
{
}
}
}
public partial class Form1 : HSEI.Windows.Forms.RobbinForm
{
private List<TextBox> tbList = new List<TextBox>(); public Form1()
{
InitializeComponent();
Init();
} private void Init()
{
for(int i=0;i<10;i++)
{
TextBox tb = new TextBox();
tb.Text = "";
tbList.Add(tb);
//TODO Layot Add Control
}
} private void Reset()
{
foreach(var tb in tbList)
{
tb.Text = "0";
}
}
}
你的程序中把TextBox换成自定义控件就行了
foreach (UserControl con in cc)
{
if (con is UserControl)
{
con.Controls[0].Text = "0";
}
// else { MessageBox.Show("没有找到文本框!"); }
}