简体   繁体   English

如何从选项卡页中的嵌入式表单检索文本框?

[英]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.

相关问题 C#WinForms - 如何通过另一种形式从一个表单上的文本框中检索数据? - C# WinForms - How Do i Retrieve Data From A Textbox On One Form Via Another Form? 如何在 c# UWP 中获取标签页的文本框? - How can I get the Textbox of a Tabpage in c# UWP? 如何在Silverlight中定位标签页的内容? - How do I position the content of a tabpage in Silverlight? 如何通过用户控件访问我的标签页名称? C# - How do i access my tabpage name form a usercontrol? C# 如何通过单击第一个表单中的按钮,在第二个TabPage中创建DataGridView - How can I create a DataGridView in a TabPage of 2nd from by clicking the button in the 1st Form 从 TabControl 中删除 TabPage 时,如何在 tabcontrol 中选择下一次? C# - When removing a TabPage from the TabControl, how do I select the next time in the tabcontrol? C# 创建新标签页时,如何在代码中克隆标签页中的所有内容 - How do I clone everything inside a tabpage in code while creating a new tabpage 如何在TabControl(TabPage)中更改标题的形式? - How can I change form of headers in TabControl (TabPage)? 如何在Windows窗体标签页中添加WPF窗体? - How can I add wpf form inside windows forms tabpage? 用户点击提交按钮后,如何从文本框中检索用户输入? - How do I retrieve user input from a textbox after users hit the submit button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM