如何在继承的用户控件中获取父表单 [英] How to get parent form in inherited user control

查看:108
本文介绍了如何在继承的用户控件中获取父表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

表单parent =(Form)FindForm();



这适用于DropDownTextBox类:UserControl

但是我在课堂上使用它DropDownText:System.Windows.Forms.TextBox

它返回null



我只想在移动时找到父窗体的位置

提前致谢。

Form parent = (Form)FindForm();

this works in class DropDownTextBox : UserControl
but I use this in class DropDownText : System.Windows.Forms.TextBox
it returns null

I just wants location of parent form when it moved
Thanks in advance.

推荐答案

不要。从OOP的角度来看,这是一个糟糕的想法,因为控件不应该知道嵌入到它中的内容。

相反,父表单应该在需要更新时向用户控件提供信息,或者控件的功能绝对需要访问它包含表单信息,然后它应该与控件构造函数一起传递。
Don't. It's a poor idea from an OOPs point of view, because the control should not know what it is embedded into.
Instead, the parent form should feed information to the user control when it needs updating, or if the functionality of the control absolutely requires access to it's containing form information, then it should be passed with the control constructor.


这篇关于如何在继承的用户控件中获取父表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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