As others have said, there are many ways to do it. Some of them might be intimidating by their complexity but would be more likely to be used professionally. The following would be less professional but hopefully easier to understand and use.
First, the User Control should not depend (know about) the form. You say you pass your form as the form to the usercontrols. You want to avoid (not do) that.
There are complicated ways to solve the problem of how to notify the form when something happens. As a beginner you should at least understand how to use delegates.
So let us say that you have a button in the User Control and you want the form to do something when the button is pressed. You can create a method in the form that is to execute when the button is pressed; something like:
Private Sub ButtonClicked(ByVal text As String)
label1.Text = text
End Sub
In the User Control define and declare a delegate as in:
Public Delegate Sub ButtonClickedEvent(ByVal text As String)
Public ButtonClicked As ButtonClickedEvent = Nothing
Notice how void ButtonClickedEvent(string text)
matches the actual method. Note also that ButtonClicked is set to null. Then create an event handler for the button and add:
ButtonClicked?.Invoke("Button clicked at " & Date.Now.ToString())
Note that the combination of the "?" and Invoke
will check ButtonClicked
for null and if it is null then it is not called. So if the form does not use the delegate nothing happens. Then in the form's constructor (after InitializeComponent
) set the delegate to the method, as in:
UserControl11.ButtonClicked = AddressOf ButtonClicked
Assuming you have everything wired properly, when the button is clicked in the User Control, the form will update the label.
What if you want the form to do something when a text box is changed? You could do something similar except use the Leave event of the text box instead of the click event. Note that the Leave event does not fire when the form is closed so you might need to do something more for situations like that.
The following is the complete VB.Net code that implements events for both a button clicked and to leave a test box. Note that you will need a button and a text box in the user control and the user control and two labels in the form.
Public Class UserControl1
Public Delegate Sub ButtonClickedEvent(ByVal text As String)
Public ButtonClicked As ButtonClickedEvent = Nothing
Public Delegate Sub SomethingChangedEvent(ByVal text As String)
Public SomethingChanged As SomethingChangedEvent = Nothing
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ButtonClicked?.Invoke("Button clicked at " & Date.Now.ToString())
End Sub
Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
SomethingChanged?.Invoke(TextBox1.Text)
End Sub
End Class
Public Class Form1
Public Sub New()
InitializeComponent()
UserControl11.ButtonClicked = AddressOf ButtonClicked
UserControl11.SomethingChanged = AddressOf SomethingChanged
End Sub
Private Sub ButtonClicked(ByVal text As String)
Label1.Text = text
End Sub
Private Sub SomethingChanged(ByVal text As String)
Label2.Text = text
End Sub
End Class