Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

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 that this.FindForm() SHOULD return the correct form object.

[EDIT] I just checked my code, and I did indeed use this.FindForm() to get the correct result. Don't understand why iut doesn't work for you?!?!? [/EDIT]
 
Share this answer
 
v2
Comments
kartheesh 8-Jul-10 4:15am    
this.findform works if the control is placed directly on the form, it won't work if the control is placed in container i.e tabcontrol
Johnny J. 8-Jul-10 4:16am    
Don't understand that, because it works for me...
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.
 
Share this answer
 
Comments
Johnny J. 8-Jul-10 3:40am    
Reason for my vote of 1
Not correct in this connection

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900