ParentForm 为空(在 Form 内 UserControl 内的 Button 中)! [英] ParentForm is null (in a Button inside a UserControl inside a Form)!

查看:29
本文介绍了ParentForm 为空(在 Form 内 UserControl 内的 Button 中)!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我让这段代码工作了!

我的 UserControl 中有一个按钮,在设计时放在我的表单中.一切正常,当我在按钮事件中调用 this.ParentForm 时,它正确地返回给我(唯一的)父表单.

I have a button inside a my UserControl dropped at design time in a my Form. All worked well and when in a button's event I called this.ParentForm it correctly returned to me the (only) parent Form.

重构后,我将带有按钮的 UserControl 移动到另一个 NameSpace,同一段代码不再起作用.this.ParentForm 现在为 NULL

After a refactoring, I moved the UserControl with the button to another NameSpace and the same piece of code no longer works. this.ParentForm now is NULL!

我阅读了 MSDN 站点,它说仅当控件托管在 IE 或其他上下文中时 this.ParentForm 返回 null.但是我只移动了命名空间!

I read the MSDN site and it says that only when the control is hosted in IE or another context this.ParentForm returns null. But I moved only the namespace!

有人有想法吗?

我无法使用不同的构造函数将其传递给父窗体,因为在设计时 Visual Studio 不会呈现该窗体.

I cannot use a different constructor to pass it the parent form because at design time Visual Studio wouldn't render the Form.

推荐答案

查看包含设计器生成代码的代码隐藏文件.在重命名过程中,您可能混淆了 Visual Studio 设计器,并且它可能孤立"了 Form.designer.cs 文件中的一个控件实例.

Look at the code-behind file that contains the designer-generated code. During the renaming, you may have confused the Visual Studio designer and it may have "orphaned" an instance of your control in the Form.designer.cs file.

特别是,查看 InitializeComponent 方法中的代码,看看是否可以发现任何创建 UserControl 实例但未将其添加到容器中的代码,或将其添加到未添加到容器中的任何代码形式.

In particular, look at the code in the InitializeComponent method and see if you can spot any code that creates an instance of your UserControl but does not add it to a container, or adds it to a container that is not added to the form.

这篇关于ParentForm 为空(在 Form 内 UserControl 内的 Button 中)!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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