VB.NET 如何从表单填充 UserControl 中的 ComboBox

VB.NET how to populate a ComboBox in an UserControl from a Form

我有一个 UserControl,其中来自 MySQL table 的数据在加载时加载到 ComboBox 中。

 Private Sub LoadFeeGroup()

    Try
        OpenConnection()
        qry = "SELECT GroupId, GroupName FROM master_fee_group WHERE Stat='1' ORDER BY GroupName ASC"

        Dim da As New MySqlDataAdapter(qry, conn)
        Dim ds As New DataSet
        da.Fill(ds, "master_fee_group")
        With CmbGroup
            .DataSource = ds.Tables("master_fee_group")
            .DisplayMember = "GroupName"
            .ValueMember = "GroupId"
        End With
        If CmbGroup.Items.Count > 0 Then
            CmbGroup.SelectedIndex = 0
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        CloseConnection()
    End Try
End Sub

并且在加载 UserControl 时调用此子例程。

Private Sub AdmissionFeeUc_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    LoadFeeGroup()
End Sub

现在用户可以通过打开表格添加任何费用组名称(如果以前没有添加)。

现在我想从该表单调用此 LoadFeeGroup() 子例程,以便用户可以在关闭表单后在 UserControlComboBox 中看到添加的费用组名称。

类似...

Private Sub FormFeeGroup_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
    ' Code for calling the UserControl subroutine..
End Sub

我试过像下面这样调用子例程,

但失败了。

我该怎么做?

更新

我在 UserControl 中添加了一个按钮。

Private Sub BtnNewGroup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNewGroup.Click
    Dim frmFeeGroup As New FormFeeGroup
    Dim dlgres As DialogResult = frmFeeGroup.ShowDialog()
    If DlgRes <> DialogResult.OK Then
        Return
    Else
        LoadFeeGroup()
    End If
End Sub

并且在FormClosing事件中

Private Sub FormFeeGroup_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    If Me.DialogResult <> Windows.Forms.DialogResult.OK Then
        Return
    Else
        'nothing to do
    End If
End Sub

并且在表单的 Close 按钮中,

Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click
    Me.DialogResult = Windows.Forms.DialogResult.OK
    Me.Close()
End Sub

我的目的是部分服务。部分原因是,如果我从菜单中打开 FormComboBox 不会更新。

您可以为必须向其传递用户控件实例的表单创建自己的构造函数。这样你就不能在不指定它应该"tied"到哪个用户控件的情况下创建表单的实例。

例如,将其放入您的表单代码中:

Public Property TargetFeeUc As AdmissionFeeUc

Public Sub New(ByVal FeeUc As AdmissionFeeUc)
    InitializeComponent() 'Must be called before anything else.

    Me.TargetFeeUc = FeeUc
End Sub

然后要创建一个新的表单实例,您将总是被迫给它一个 AdmissionFeeUc 控件。

'If written inside the user control's code:
Dim frmFeeGroup As New FormFeeGroup(Me)

'If written somewhere else:
Dim frmFeeGroup As New FormFeeGroup(<user control instance here>)

'...for example:
Dim frmFeeGroup As New FormFeeGroup(AdmissionFeeUc1)

每当您想从 FormFeeGroup 表单更新用户控件时,您只需调用:

TargetFeeUc.LoadFeeGroup()

编辑:

要获取动态创建的用户控件实例,您必须在创建它时设置控件的 Name property,然后您可以通过以下方式引用它:

<target control or form>.Controls("<user control name here>")

'For example:
Me.Controls("MyFeeUc")

'Or for sub-controls:
Me.Panel1.Controls("MyFeeUc")