将文本传递给C#中的其他表单 [英] Passing text to a different Form in C#

查看:82
本文介绍了将文本传递给C#中的其他表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨大家好



我的Windows窗体项目中有两个表单,即form1表单2.



表单包含一个按钮,表单2包含一个文本框。



我想在单击form1按钮时将form2传递给显示文本。

例如



// form1代码



Hi Guys

I have got two forms in my windows form project, namely form1 form 2.

form contains a button and form two contain a textbox.

I want to pass form2 to display text when form1 button is clicked.
for example

//form1 code

public partial class Form1 : Form
  {
      
      public Form1()
      {
          InitializeComponent();
      }

      private void button1_Click(object sender, EventArgs e)
      {
          textBox1.Text("Hi");
      }





}



如何通过form2 textbox to form1?请帮忙?



Abs。



}

How can i pass form2 textbox to form1? please help?

Abs.

推荐答案

这类问题是最常见的问题,回答,这里是CodeProject。这是我在8月发布的一个几乎相同的问题的答案:[ ^ ]。



并且,该线程还有其他响应,我认为您会发现有用。



关键是在Form2上公开类型'TextBox的公共属性,然后分配实例 Form2中的TextBox将该事件加载到该属性。
This kind of question is among the most frequently asked, and answered, here on CodeProject. Here's an answer to an almost identical question that I posted in August: [^].

And, there are other responses on that thread I think you'll find helpful.

The key thing is to expose a Public Property of Type 'TextBox on 'Form2, and then assign the instance of the TextBox in Form2's Load Event to that Property.
// in Form2
public TextBox form2TextBox { get; set; }

private void Form2_Load(object sender, EventArgs e)
{
    form2TextBox = textBox1;
}

//In Form1:
private Form2 form2 = new Form2();

private void Form1_Load(object sender, EventArgs e)
{
    form2.Show();
}

private void btnWriteToForm2_Click(object sender, EventArgs e)
{
    form2.form2TextBox.Text = "Test writing text from Form1";
}

Form1,即创建Form2的Form,可以通过Public属性访问Form2上的TextBox,并且能够从中读取,写入,更改其外观等。



注意这只是使Form1上的TextBox可用于Form1的一种方法。在这种情况下,由于您需要以异步按需方式访问TextBox,我认为这是合适的。

Form1, the Form that creates Form2, has access to the TextBox on Form2 through the Public Property, and is able to read from it, write to it, change its appearance, etc.

Note this is only one way you could make the TextBox on Form2 available to Form1. In this case, since you need access to the TextBox in an asynchronous on-demand way, I think it's appropriate.


这是关于表单协作的流行问题。最强大的解决方案是在表单类中实现适当的接口,并传递接口引用而不是引用Form的整个实例。有关更多详细信息,请参阅我以前的解决方案:如何以两种形式复制列表框之间的所有项目 [ ^ ]。



另请参阅此处的其他解决方案讨论。如果应用程序足够简单,解决方案就像在一个表单中声明一些 internal 属性并将对一个表单的实例的引用传递给另一个表单的实例一样简单形成。对于更复杂的项目,这种违反严格封装的样式和松散耦合可能会增加代码的意外复杂性并引发错误,因此封装良好的解决方案将是优惠。



另请参阅:

http://en.wikipedia.org/wiki/Accidental_complexity [ ^ ],

http://en.wikipedia.org/wiki/Loose_coupling [ ^ ]。



-SA
This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA


这篇关于将文本传递给C#中的其他表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆