从另一种形式访问另一种形式的面板控件 [英] access the another form of panel control from another form

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

问题描述

我想从另一种表格中填充另一种形式的面板控件
形式1
panelcontrol1(public)

form2
form1 obj = new form1()
obj.panelcontrol1.controls.clear();
其运行成功,但无输出显示
在此先感谢.

i want to fill the another form of panel control from another form
form1
panelcontrol1(public)

form2
form1 obj=new form1()
obj.panelcontrol1.controls.clear();
its run successfully but no output display
thanks in advance.

推荐答案

这里有很多错误的东西!
第一个问题是您要创建一个新实例:
Loads of things wrong here!
First problem is that you are creating a new instance:
form1 obj=new form1()

这将创建表单的 new 实例,而不是让您访问原始表单的实例.这类似于买车:如果您将钱包放在手套箱中,然后为女友买一辆新车,您是否希望钱包在她的手套箱中?您需要引用原始表单实例.

其次,从控件的形式之外访问控件是一种非常不好的做法:它将两种形式联系在一起,这意味着您不能在不考虑对另一种形式的影响的情况下更改其中一种形式.这与OOP的整体思想背道而驰,使以后的生活变得复杂!

相反,在创建Form1实例时将其传递给Form2,并在form1中使用public属性为面板设置新值.
或者更好的方法是,在Form2中创建一个您在Form1中订阅的事件,该事件显示我需要更新您的信息,请使用此信息.".

That creates a new instance of the form, not gives you access to the original one. It''s similar to buying a car: If you put your wallet in your glove box, then buy a new car for your girlfriend would you expect the wallet to be in her glovebox? You need to refer to the original form instance.

Secondly, it is very bad practice to access controls from outside the form they are on: it ties the two forms together and means that you cannot change the one without considering the effects on the other. This is against the whole idea of OOP, and makes life complicated later!

Instead, pass the Form1 instance to Form2 when you create it, and use a public property in form1 to set new values to the panel.
Or better still, create an event in Form2 which you subscribe to in Form1 which says "I need to update you, please use this information".


这是有关表单协作的常见问题(但通常是由于设计不善和缺乏基本知识所致).它是通过表单类实现适当接口的最可靠的方法.请查看我过去的解决方案;另请参阅其他建议和讨论:
如何在列表框之间复制所有项目两种形式 [ ^ ].

-SA
This is a popular question on form collaboration (but usually a result of poor design and lack of understanding of the basics). The most robust way it to implement appropriate interface by the form class. Please see my past solution; also see other suggestions and the discussion:
How to copy all the items between listboxes in two forms[^].

—SA


您使用的是obj.show()还是obj.showdialog();
我不清楚您的期望.也许您可以提出自己的问题.
did you use obj.show() or obj.showdialog();
I am not clear what you are expecting. May be you can re phrase you question.


这篇关于从另一种形式访问另一种形式的面板控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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