Windows窗体用户控件在其属性更新时不会更新

时间:2013-04-03 15:52:27

标签: vb.net properties user-controls form-load

我在Visual Basic窗体应用程序中创建了一个用户控件。它有一个名为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

在用户控件的Load方法中,我根据ID刷新用户控件中的组件。

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

现在,每当我在添加此用户控件的表单中执行某些操作时,我希望此控件显示正确的ID。例如,我有一个DataGridView,每当选择更改时,我想更新用户控件:

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

问题是用户控件仅在第一次正确加载所选ID时,我似乎无法重新加载它的数据。这意味着如果属性ID的值发生更改,我不知道如何强制重新加载用户控件。它显示与加载的第一个ID相同的数据。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

不是更新控件加载事件中的ID,而是在属性更改时更新它:

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

UserControl_Load event “在控件变为第一次之前发生。”

相关问题