views:

18

answers:

1

Hello,

I have a tabcontrol that creates tab pages from a "User Control" I created (a separate form in vb.net) using this code: (MainTab is the separate user control I created which has text boxes etc in it)

Dim tmpTab As New MainTab
myTabControl.TabPages.Add()
Dim tmpTabCount As Integer = myTabControl.TabPages.Count
myTabControl.TabPages.Item(tmpTabCount - 1).Controls.Add(tmpTab)
myTabControl.TabPages.Item(tmpTabCount - 1).Text = "Untitled"

I'm using the devexpress xtratab control so the code might look a bit different than the default vb.net tab control.

Now in my MainTab user control file file, I can't for the life of me figure out how to call a control in the form1 where the xtra tab control is placed on. "Me.Parent.Dispose" works for closing the tab when executed via the MainTab control, but that's as far as I can get for communicating with the parent from.

Does anyone know the solution? I'm not sure if I have to reference something in the MainTab user control or what in order to communicate with any objects on the default form1.

+1  A: 

Generally speaking, I avoid making my child controls cognizant of the parent. It leads to unpleasant coupling more often than I care for.

Consider adding a custom event to your MainTab class that your form can subscribe to. When you want to pass a message to the form, your user control can invoke the method, and your form's event handler can process it accordingly. This pattern helps keep your user control pluggable into other forms by reducing its dependency on its parent.

Creating a user control event in a windows form is discussed in this MSDN article:

http://msdn.microsoft.com/en-us/library/aa302342.aspx

kbrimington
After some more trial/error I managed to get the following to work, is this "safe" to use? Dim tmpParentForm As Form = Me.FindForm() If Not tmpParentForm Is Nothing Then
Joe
Should be fine. Depending on your circumstances, you may choose also go cast `tmpParentForm` to the form's type in order to gain access to your custom public methods.
kbrimington