我有一个 Windows 窗体,其中有一个button1
,当单击它时,UserControl
动态添加到代码中的是:
int c = 0;
private void button1_Click(object sender, EventArgs e)
{
int v;
v = c++;
panel1.VerticalScroll.Value = VerticalScroll.Minimum;
UserControl1 us = new UserControl1();
us.Name = "us" + v;
us.Location = new Point(50, 5 + (30 * v));
us.Tag = btn;
panel1.Controls.Add(us);
}
包含 4 个控件、UserControl
2 个组合框和 2 个文本框
即combobox1
,和combobox2
_textbox1
textbox2
有 4 个文本框在同一个表单上
still-textbox1
, still-textbox2
,still-textbox3
和still-textbox4
有button2
,它会将文本传输到组合框和文本框,它们是oldcombobox1
、和oldcombobox2
oldtextbox1
oldtextbox2
单击两次时button1
,它将UserControls
向表单添加两个。我想以以下格式传输文本
oldcombobox1.text = still-textbox1.text + "," + combobox1.text(which is dynamically generated) + "," + combobox1.text (which is dynamically generated)
等combobox1
来自 UserControl 的所有文本(动态添加)
oldcombobox2.text = still-textbox2.text + "," + combobox2.text (which is dynamically generated) + "," + combobox2.text (which is dynamically generated)
等combobox2
来自 UserControl 的所有文本(动态添加)
oldtextbox1.text = still-textbox3 + "," + textboox1.text (which is dynamically generated) + "," + textbox1.text (which is dynamically generated)
等所有textbox1
文本UserControl
(动态添加)
意味着当still-textbox1.text = first
和当动态UserControl
添加三次时它将包含3次combobox1
然后oldcombobox1
应该包含:
first
, combobox1.text
, combobox1.text
,combobox1.text
我已经制作了这段代码,但它不起作用
foreach (Control ctrl in panel1.Controls)
{
if (ctrl is UserControl)
{
UserControl1 myCrl = ctrl as UserControl1;
oldcombobox1.text = still-textbox1.text + "," + myCrl.comboBox1.Text;
oldcombobox2.Text =still-textbox2.text + "," + myCrl.comboBox2.Text;
oldtextbox1.Text = still-textbox3.text + "," + myCrl.textBox1.Text;
oldtextbox2.Text.Text = still-textbox4.text + "," + myCrl.textBox2.Text;
}
}