使用c#中的按钮从另一个表单中获取多个文本框值

使用c#中的按钮从另一个表单中获取多个文本框值

问题描述:

当我单击第一个表单中的按钮时,它会显示第二个表单中文本框的所有值。你能告诉我一些代码吗?

when i click the button in first form it display all the values of text boxes in the second form. can you show me some codes?

很少引用可以使用的代码:

1. 从子窗体访问父窗体的控件 [ ^ ]

2. 如何访问文本框父母表格? [ ^ ]

3. 如何获取特定于Windows窗体的所有子控件类型(按钮/文本框)? [ ^ ]

你需要通过公开或使用一些文本框来公开你的文本框返回文本框或文本框内容的其他公共方法,读取前两个链接,为上述任务提供解决方案。如果需要获取表单中的所有文本框控件,可以使用第3个链接中描述的技术之一,并将文本框公开为公共方法。最后,您可以从子控件中调用该方法或直接调用文本框控件。希望这可以帮助。 :)
few references with code which can be used:
1. accessing controls on parentform from childform[^]
2. How to access textbox on parent form? [^]
3. How to get ALL child controls of a Windows Forms form of a specific type (Button/Textbox)?[^]
you need to expose your textboxes by make them public or using some other public method which return text boxes or the textbox contents, read the first two links which provide solution for above task. If you need to get all the textbox controls in the form you can use one of the techniques described in 3rd link and expose textboxes as public method. At the end you can call that method or directly textbox control from the child control. hope this helps. :)


假设第一个Form('FirstForm)是Win Forms项目的主窗体,并创建第二个Form('SecondForm):
Assuming the first Form ('FirstForm) is the Main Form of a Win Forms project, and creates the second Form ('SecondForm):
// In the first (Main) Form:

// required
 using System.Collections.Generic;

SecondForm secondForm;

List<string> SecondFormTextBoxContent;

private void FirstForm_Load(object sender, EventArgs e)
{
    secondForm = new SecondForm();
    
    SecondFormTextBoxContent = new List<string>();
}

private void GetTextBoxValues_Click(object sender, EventArgs e)
{
   SecondFormTextBoxContent = secondForm.GetTextBoxContent();

   DisplaySecondFormContent();
}

private void DisplaySecondFormContent()
{
    // do something here to display the TextBoxes content as you require
    Console.WriteLine(string.Join(Environment.NewLine, SecondFormTextBoxContent); 
}

// In second Form:

// required
using System.Collections.Generic;
using System.Linq;

public SecondForm()
{
    InitializeComponent();

    // see note for why this is done here, not in the Form_Load EventHandler
    TextBoxesToReport = new List<textbox>
    {
        textBox1, textBox2, textBox3
    };
}

private List<textbox> TextBoxesToReport;

public List<string> GetTextBoxContent()
{
    return TextBoxesToReport.Select(tbx => tbx.Text);
}

评论:



1.目标是在任何一个表单/对象中暴露给外部只有什么其他表格/对象可能需要知道。在这种情况下,可以选择不直接在第二个表单上公开TextBox,但是,通过表单上的公共方法间接提供它们的内容。



那是称为封装,是关注点分离原则的一种形式。我们可以将第一种形式称为第二种形式的消费者,将第二种形式称为第一种形式的数据提供者。



2在这种情况下,第二个Form公开了一个方法,它返回TextBoxes当前内容(字符串)的List。



3.注意:在第二个我们必须在第二种形式的构造函数中设置TextBoxes的内部(私有)列表的内容:因为第一种形式显示第二种形式的Load事件,所以不会调用它。我认为这是Windows窗体的一个缺陷。常见的解决方法是使用'保证被调用的显示事件。

Comments:

1. the goal is to expose to the "outside" in any one Form/object only what other Form(s)/Objects(s) may "need to know." In this case a choice is made to not expose the TextBoxes on the second form directly, but, to provide their content indirectly via a public method on the form.

That's called "encapsulation" and is a form of the principle of "separation of concerns." We could refer to the first form as the "consumer" of the second form, and the second form as the "provider" of data to the first form.

2. in this case the second Form exposes a method which returns a List of the current contents (strings) of the TextBoxes on it.

3. note: in the second form we have to set up the contents of the internal (private) list of TextBoxes in the constructor of the second form: because the second form's Load event is not called since the first form shows it. I consider this a flaw of Windows Forms. The common work-around for this is to use the 'Shown event which is guaranteed to be called.


您需要在form2中创建公共变量以从第一个表单中获取值! />




ex:in form2代码:

公共字符串a;



in button_Click form1代码:



Form2 f2 = new Form2();

f2.a = txtvalue.Text ;

f2.Show();



希望对你有帮助!
you need create public variables in form2 to get the value from the first form!


ex: in form2 code:
public string a;

in button_Click form1 code:

Form2 f2 = new Form2();
f2.a = txtvalue.Text;
f2.Show();

hope help for you!