使用的一种形式中控制到另一个 [英] Using The Controls Of One Form Into Another

查看:83
本文介绍了使用的一种形式中控制到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,以某种形式( Form1中)我有很多的复选框和文本框,并在窗体2 我只有一个文本框,但我希望把一些内容从 Form1中文本框,并把在窗体2 文本框中像这一点,但之间的形式:

I have a application that in one form(Form1) I have many checkBoxes and textBoxes and in Form2 I have only a textBox, but I want to put some contents from the Form1 textBoxes and put in the Form2 textBox, like this, but between forms:

textBox1.Text = "Test: " + textBox1 + "\n" + textBox3;



由于 textBox1的窗体2 第二 textBox1的 textBox3 Form1中,但我怎么能做到这一点?谢谢你。

As textBox1 in Form2 and the second textBox1 and textBox3 in Form1, but how I can do this? Thanks.

推荐答案

取决于你如何启动引用Form1上,或者如果你的第二种形式,你可以在窗体2分配成员对象。使用的MDI接口有一个形式集合,从中可以检索到你的Form1中引用

Depending on how you launch the second form you can either assign a member object in Form2 that references Form1 or if you are using an MDI interface there is a forms collection from which you can retrieve a reference to your Form1.

例如,你可以在你的窗体2类以下代码:

For example, you could have the following code in your Form2 class:

public partial class Form2 : Form
{
	public Form1 LaunchOrigin { get; set; }
	// and so on

现在您可以指定LaunchOrigin成员当您启动窗体2 。下面是一个例子:

Now you can assign the LaunchOrigin member when you launch Form2. Here is an example:

Form2 newForm = new Form2();
newForm.LaunchOrigin = this;
newForm.Show();

您现在可以访问到Form1和它的所有成员。下面是一个简单的例子:

You now have access to Form1 and all its members. Here is a simple example of that:

	private void Form2_Load(object sender, EventArgs e)
	{
		this.Text = LaunchOrigin.Text;
	}

您必须记住,控件被声明为private,所以你不会有直接的对它们的访问。你可以写在Form1上所引用该控件的属性,但这种是最常见的一个坏主意。为了完整起见,然而,这里是一些代码,你可以用它来揭露Form1上放置一个按钮:

You must remember that controls are declared as private so you won't have direct access to them. You could write a property on Form1 that referenced that control but this is most often a bad idea. For the sake of completeness, however, here is some code that you could use to expose a button on Form1:

public partial class Form1 : Form
{
	public Button theButton;
	public Form1()
	{
		InitializeComponent();
		theButton = button1; // where button1 is what I dragged on
	}
	// and so on

虽然你所要求的是相对比较容易实现,它使你在旅途中的一些的的应用结构上。认真思考什么是你正在试图形式之间暴露,也许它不愧为可以绑定到两种形式,这样,一旦你改变的基本类型,你改变上都表现的独特的类型。

Although what you're asking is relatively easy to accomplish, it puts you on the road to some brittle application structure. Think hard about what it is you're trying to expose between forms, perhaps it deserves to be a distinct type that you can bind to both forms so that once you change the underlying type, you change the representation on both.

这篇关于使用的一种形式中控制到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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