views:

578

answers:

2

Hi!

I want to create a simple tabbed interface app. I did it before in WPF using an usercontrol inside of each tab, and I comunicate with the principal form searching for a parent element (the form, or the tab control)

How could I made the same in WinForms? Which element need I put inside of each tab?

Thanks.

+1  A: 

TabPage and TabControl might be classes and components you would want to look into. Using the Parent property you can access the owning form and thus communicate with it or between tabs, should you need it.

Though, as Fredrik Mörk suggests, using events is a far better practice. =)

J. Steen
+3  A: 

You can use pretty much the same approach in a winforms application. You can add user controls to the tab pages of a TabControl on a form. Inside the user control code you can use the FindForm method to get hold of the parent form.

However, I would suggest that you instead use events to communicate "out" from the controls, in order to make them less dependent on their surroundings.

Fredrik Mörk
Why FindForm instead of the Parent property? Just curious. =)
J. Steen
The Parent property returns the Parent, which may very well be another control on the form. If you put a panel on a form, and the user control in that panel, you would need to use Parent.Parent to reach the form. You could consider FindForm a shortcut that leads directly to the form.
Fredrik Mörk
Oh, I know how FindForm works, I just hadn't considered the Parent.Parent.Parent recursive issue. Thanks! ^^
J. Steen