将数据从一种形式传输到另一种形式 [英] to transfer a data from one form to another

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

问题描述

我有一个mdi父表单和两个子表单.

在从form1单击按钮时,应打开表单2,并在选择单选按钮时,我需要将该单选按钮文本检索到我的第一个表单文本框中.

我使用委托发送数据并在form1上显示它

但是我使用了形式1的新对象,我想要的是...将数据检索到已经打开的form1,其中其他文本框已经被填充....

我该如何解决

我在表格1中的代码是:

i have an mdi parent form and two child forms.

on button click from form1, form 2 should be opened and on a radiobutton selection, i need to retrieve that radio button text to my first form text box.

i send the data and displayed it on form1 using delegates

but i used a new object of the form 1, what i want.... is to retrieve data to the already opened form1 , where other text boxes are already filled....

how can i solve this

my code in form 1 is:

delegate void MyDelegate(string str);
public void PassingParameters(string item)
        {
            if (txtScheme.InvokeRequired)
            {
                MyDelegate d = new MyDelegate(PassingParameters);
                txtScheme.Invoke(d, new object[] { item });
            }
            else
            txtScheme.Text = item;
        }



在表格2中,事件是:



and in form 2 event is :

string  Scheme = rb.Text;
admission ad= new admission();
ad.PassingParameters(Scheme);
ad.MdiParent = this.ParentForm;
ad.Show();
this.Close();

推荐答案

这是有关表单协作的流行问题.最健壮的解决方案是在Form类中实现适当的接口,并传递接口引用而不是对Form的整个实例"的引用.请查看我过去的解决方案以获取更多详细信息:如何以两种形式在列表框之间复制所有项目 [ http://en.wikipedia.org/wiki/Accidental_complexity [ http://en.wikipedia.org/wiki/Loose_coupling [ http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages [如何在WPF中创建MDI父窗口? [ ^ ].

另请参阅我过去的答案:
在WPF中使用MDI窗口的问题 [ ^ ],
MDIContainer提供错误 [如何最大程度地设置子表单,最小化最后一个子表单 [ ^ ].

—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[^].

Also, better don''t use MDI. Who needs MDI, ever? Why torturing yourself and scaring off your users?
Do yourself a great favor: do not use MDI at all. You can do much easier to implement design without it, with much better quality. MDI is highly discouraged even by Microsoft, in fact, Microsoft dropped it out of WPF and will hardly support it. More importantly, you will scare off all your users if you use MDI. Just don''t. I can explain what to do instead. Please see:
http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages[^],
How to Create MDI Parent Window in WPF?[^].

Please also see my past answers:
Question on using MDI windows in WPF[^],
MDIContainer giving error[^],
How to set child forms maximized, last childform minimized[^].

—SA


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

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