在自定义控件中获取父表单名称 [英] Get parent form name in custom control
问题描述
您好,专家,
如何在自定义控件中获取父表单名称.
例如:如果自定义控件以mytest形式放置在选项卡控件中,则我在自定义控件中的预期结果应为mytest.
我尝试使用自定义控件提供的以下选项
1)this.parent.Findform():如果将自定义控件放置在容器中,即tabcontrol,groupbox等中,则返回null值(如果将自定义控件直接放置在窗体中,则此方法无效).
2)this.FindForm():返回空值
3)this.Parent.Parent:未预期的结果
注意:在用户控件中,有this.parentform选项,它返回表单名称.自定义控件中没有相同的功能
感谢在这方面获得帮助.
问候
Kartheesh M
Hi Experts,
How to get the parent form name in custom control.
For ex: If the custom control is placed within tab control in mytest form, then my expected result in custom control shall be mytest.
I tried using following options available with custom control
1) this.parent.Findform() : returning null value (this works if the custom control is placed in form directly) fails if the custom control is placed in container i.e in tabcontrol,groupbox etc.
2) this.FindForm() : returning null value
3) this.Parent.Parent: not expected result
Note: In usercontrol there is option available as this.parentform which returns form name. same is not available in custom control
Appreciate to get help in this regard.
Regards
Kartheesh M
推荐答案
我遇到了类似的问题,但是我不记得确切的解决方法.您应该能够在本文和随附的代码中找到线索(它在VB中,但这只是次要的细节,不应更改任何内容):
脏按钮 [
I had a similar problem, but I don''t remember exactly how I solved it. You should be able to find a clue in this article and the accompanying code (it''s in VB, but that''s a minor detail that shouldn''t change anything):
Dirty Button[^]
It works for me at least both when the control is placed directly on the form and in a groupbox/tab control
I could be wrong, but I think actually thatthis.FindForm()
SHOULD return the correct form object.
I just checked my code, and I did indeed usethis.FindForm()
to get the correct result. Don''t understand why iut doesn''t work for you?!?!? [/EDIT]
您需要使用反射来获取类名.否则,您最好的办法就是获取表单标题或类似的标题.
You need to use reflection to get class names. Otherwise, the best you can do is to get the form title or something like that.
这篇关于在自定义控件中获取父表单名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!