Ahmed Nazmy
Ahmed Nazmy

Reputation: 419

VB.Net WinForms UserControl

I have two UserControls already loaded how to change TextBox text property on a UserControl from the other loaded one.

Upvotes: 0

Views: 904

Answers (1)

Jay
Jay

Reputation: 6017

Lets say your user controls are named UserControl1 and UserControl2. Unless UserControl1 has a reference to UserControl2 it can't directly make changes to it. In that situation the one solution is to allow the form or parent control to handle making the change, by adding an event to UserControl1 and handling it on the form.

In UserControl1:

'Define an Event the form can handle at the class level
Public Event SomePropertyUpdated()

Then in whatever method you need it to be in, when you would want to change the textbox on the other control raise your event:

RaiseEvent SomePropertyUpdated()

In the form:

 'The sub that is called when the second control needs updated
 Public Sub UpdateTextBoxes()
      UserControl2.Textbox1.text = userControl1.Property
 End Sub

In the load event of the form Add the handler for your created event:

 AddHandler UserControl1.SomePropertyUpdated, AddressOf UpdateTextBoxes

In the closed Event of the form remove the handler for the event:

RemoveHandler UserControl1.SomePropertyUpdated, AddressOf UpdateTextBoxes

That is one of a few ways to handle the situation. The specifics of what you are trying to do usually dictates what method to use.

Upvotes: 2

Related Questions