[英]How do I retrieve textbox from an embedded form in a tabpage?
Im fairly new to C#, I have a parent form (form1) with a tabcontrol. 我对C#来说还很陌生,我有一个带有Tab控件的父窗体(form1)。 the first tab is static and i have some stuff in there. 第一个选项卡是静态的,我那里有东西。 After that i can dinamicaly add embedded forms (all the same form2) on the tab with a button. 之后,我可以使用按钮在选项卡上添加嵌入的表单(所有相同的form2)。 The newly created forms each have a single textbox from which i want to retrieve the textboxname.Text and write it on a textbox outside the tabcontrol in form1.. 新创建的表单每个都有一个文本框,我要从中检索textboxname.Text并将其写在form1中tabcontrol之外的文本框中。
in form2 i added public TextBox TextBox1 { get { return textBox1; 在form2中,我添加了公共TextBox TextBox1 {get {return textBox1; } } and in form1 the private Form1 otherForm; }}和在form1中私有的Form1 otherForm; ... ...
but i dont know where to go from there i think i have to do a foreach (TabPage tab in tabControl1.TabPages) but im not sure ^^ 但我不知道从那里去,我认为我必须做一个foreach(tabControl1.TabPages中的Tab页面选项卡),但我不确定^^
Try this out...the key is you need to CAST the control in the TabPage back to Form2 before you can access the property you added to it: 尝试一下...关键是您需要先将TabPage中的控件投射回Form2,然后才能访问添加到它的属性:
private void button2_Click(object sender, EventArgs e)
{
if (tabControl1.SelectedTab != null)
{
if (tabControl1.SelectedTab.Controls.Count > 0)
{
if (tabControl1.SelectedTab.Controls[0] is Form2)
{
Form2 f2 = (Form2)tabControl1.SelectedTab.Controls[0];
label1.Text = f2.TextBox1.Text;
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.