简体   繁体   中英

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

I have an UserControl where data from MySQL table loads in a ComboBox while it is loading.

 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

And this Subroutine is being called when the UserControl is being loaded.

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

Now user can add any Fee Group Name (if not added previously) by opening a form.

Now I want to call this LoadFeeGroup() subroutine from that form so that user can see the added Fee Group Name in the ComboBox of the UserControl after closing the form.

Something like...

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

I have tried to call the Subroutine like below,

子程序

but failed.

How can I do that ?

UPDATE

I have added a button in the 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

And in the FormClosing event

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

And in the Close button in the form,

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

My purpose is Partially served . Partially because, If I open the Form from the menu, the ComboBox is not updated.

You could create your own constructor for the form where you must pass a user control instance to it. That way you cannot create an instance of the form without specifying which user control it should be "tied" to.

For instance, put this in your form's code:

Public Property TargetFeeUc As AdmissionFeeUc

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

    Me.TargetFeeUc = FeeUc
End Sub

Then to create a new form instance you'll always be forced to give it an AdmissionFeeUc control.

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

And whenever you want to update the user control from your FormFeeGroup form you just need to call:

TargetFeeUc.LoadFeeGroup()

EDIT:

To get the user control instance if it was created dynamically you have to set the control's Name property when creating it, then you can reference it via:

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM