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

时间:2017-04-08 06:57:30

标签: vb.net user-controls

我有一个UserControlMySQL表中的数据在加载时加载到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()子例程,以便用户在关闭表单后可以在ComboBox的{​​{1}}中看到添加的费用组名称。

像...一样的东西。

UserControl

我试图像下面那样调用子程序,

subroutine

但失败了。

我该怎么做?

更新

我在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 ,则Form不会更新。

1 个答案:

答案 0 :(得分:1)

您可以为表单创建自己的构造函数,您必须将用户控件实例传递给它。这样你就不能创建一个表单实例而不指定它应该是哪个用户控件&#34;绑定&#34;到。

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

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")