Pergunta

I have created a User Control in a Visual Basic Form application. It has a property called ID

Private intID As Integer

Public Property ID() As Integer
    Get
        Return intID
    End Get
    Set(value As Integer)
        intID = value
    End Set
End Property

And in the Load method of the User Control I am refreshing the components in the User Control based on the ID.

Private Sub UserControl_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    UpdateUserControlFields() ' This Method uses ID within
End Sub

Now I want this control to be shown with the correct ID whenever I do something in a Form that I added this User Control to. For example I have a DataGridView and whenever selection changes I want to update the user control:

Private Sub MyDataGridView_SelectionChanged(sender As Object, e As EventArgs) Handles MyDataGridView.SelectionChanged
    If Not IsNothing(MyBindingSource.Current) Then
        UserControlONE.ID = MyBindingSource.Current("someID")
        UserControlONE.Refresh() ' Doesn't Work.
        UserControlONE.Update() ' Doesn't Work.
    End If
End Sub

The problem is the user control loads correctly with the selected ID only the first time and I cannot seem to be able to reload it's data. Meaning if the value of the property ID changes I don't know how to enforce reloading the User Control. It shows the same data as the first ID it was loaded with. Any help will be appreciated.

Foi útil?

Solução

Instead of updating the ID in the control load event, update it when the property changes:

Private intID As Integer

Public Property ID() As Integer
    Get
        Return intID
    End Get
    Set(value As Integer)
        intID = value
        UpdateUserControlFields() ' This Method uses ID within
    End Set
End Property

The UserControl_Load event "Occurs before the control becomes visible for the first time."

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top