开发者

vb.net - problem making a call from one usercontrol to another form

开发者 https://www.devze.com 2023-01-13 05:17 出处:网络
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 c开发者_如何学运维ontrol I created which has tex

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 c开发者_如何学运维ontrol 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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号