形式之间传递数据 [英] Passing data between forms

查看:118
本文介绍了形式之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种形式。首先, Form1中拥有一批中,一些标签和一个列表框。我preSS一个按钮,新的窗体2 打开并包含一些文本。我想在窗体2 中的文本转移到列表框中的 Form1中

到目前为止,我所做的是使列表框的改性剂的公共的,然后把这个code在窗体2 的按钮

  Form1中FRM =新Form1的();
frm.ListBox.items.Add(textBox.Text);

但神奇的是,这种不添加任何价值。我以为我是错的插入,所以我提出了同样的过程。这一次,我做了一个标签公开,并添加文本框的值到文本属性,但失败了。

任何想法?


解决方案

  Form1中FRM =新Form1的();

现在FRM是Form1类的新实例。

FRM的不引用被显示给用户Form1的原始实例

一个解决方案是,创建窗体2的实例时,参考它传递到您当前的Form1的实例。

I have two forms. First, Form1 has a group box, some labels and a listbox. I press a button and new Form2 is opened and contains some text. I want to transfer the text in Form2 to the listbox in the Form1.

So far, what I have done is make modifier of listbox to public and then put this code in the button of Form2

Form1 frm = new Form1();
frm.ListBox.items.Add(textBox.Text);

But amazingly, this does not add any value. I thought I was mistaken with the insertion so I made the same procedure. This time, I made a label public and added textbox value to its Text property but it failed.

Any ideas?

解决方案

Form1 frm = new Form1();

frm is now a new instance of class Form1.

frm does not refer to the original instance of Form1 that was displayed to the user.

One solution is, when creating the instance of Form2, pass it a reference to your current instance of Form1.

这篇关于形式之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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