将文本框控件作为参数传递

时间:2018-02-16 15:28:21

标签: vb.net

以下代码包含一个从数据库中检索值的函数,并将它们放在四个文本框中,例如:txtCash,txtDebt,txtPayment,txtBalance。所以不是在函数内部写文本框,而是想用占位符替换它们并将值返回到包含这些文本框的表单

Public Sub LoadTotals()
    Dim AfCashAmount As Double
    Dim AfDebtAmount As Double
    Dim AfPayment As Double

    Try
        sqL = "SELECT InvoiceID, SUM(InvoiceAmount) As Total FROM Invoice WHERE PaymentType = 'Cash' AND AccountType='AF' GROUP BY InvoiceID"
        ConnDB()
        cmd = New SqlCommand(sqL, conn)
        dr = cmd.ExecuteReader

        AfCashAmount = 0

        Do While dr.Read = True
            AfCashAmount = AfCashAmount + dr(1)
        Loop

        '*************************************************************************

        sqL = "SELECT InvoiceID, SUM(InvoiceAmount) As Total FROM Invoice WHERE PaymentType = 'Debt' AND AccountType='AF' GROUP BY InvoiceID"
        ConnDB()
        cmd = New SqlCommand(sqL, conn)
        dr = cmd.ExecuteReader

        AfDebtAmount = 0

        Do While dr.Read = True
            AfDebtAmount = AfDebtAmount + dr(1)
        Loop

        '*************************************************************************

        sqL = "SELECT PaymentID, SUM(PaymentAmount) FROM Payment WHERE Currency='AF' GROUP BY PaymentID"
        ConnDB()
        cmd = New SqlCommand(sqL, conn)
        dr = cmd.ExecuteReader

        AfPayment = 0

        Do While dr.Read = True
            AfPayment = AfPayment + dr(1)
        Loop

        With frmStatistics
            .txtAFCash.Text = Format(AfCashAmount, "N2").ToString()
            .txtAFDebt.Text = Format(AfDebtAmount, "N2").ToString()
            .txtAFPayment.Text = Format(AfPayment, "N2").ToString()
            .txtAFBalance.Text = Format(AfDebtAmount - AfPayment, "N2").ToString()
        End With
    Catch ex As Exception
        MsgBox(ex.ToString)
    Finally
        cmd.Dispose()
        conn.Close()
    End Try
End Sub

1 个答案:

答案 0 :(得分:0)

您可以使用以下内容返回词典:

Public Function LoadTotals() As Dictionary(Of String, String)
    Dim AfCashAmount As Double
    Dim AfDebtAmount As Double
    Dim AfPayment As Double

    Try
        sqL = "SELECT InvoiceID, SUM(InvoiceAmount) As Total FROM Invoice WHERE PaymentType = 'Cash' AND AccountType='AF' GROUP BY InvoiceID"
        ConnDB()
        cmd = New SqlCommand(sqL, conn)
        dr = cmd.ExecuteReader

        AfCashAmount = 0

        Do While dr.Read = True
            AfCashAmount = AfCashAmount + dr(1)
        Loop

        '*************************************************************************

        sqL = "SELECT InvoiceID, SUM(InvoiceAmount) As Total FROM Invoice WHERE PaymentType = 'Debt' AND AccountType='AF' GROUP BY InvoiceID"
        ConnDB()
        cmd = New SqlCommand(sqL, conn)
        dr = cmd.ExecuteReader

        AfDebtAmount = 0

        Do While dr.Read = True
            AfDebtAmount = AfDebtAmount + dr(1)
        Loop

        '*************************************************************************

        sqL = "SELECT PaymentID, SUM(PaymentAmount) FROM Payment WHERE Currency='AF' GROUP BY PaymentID"
        ConnDB()
        cmd = New SqlCommand(sqL, conn)
        dr = cmd.ExecuteReader

        AfPayment = 0

        Do While dr.Read = True
            AfPayment = AfPayment + dr(1)
        Loop

        'The following to return string values.
        Dim dictTotals As New Dictionary(Of String, String)
        dictTotals.Add("CashAmount", Format(AfCashAmount, "N2").ToString())
        dictTotals.Add("DebtAmount", Format(AfDebtAmount, "N2").ToString())
        dictTotals.Add("Payment", Format(AfPayment, "N2").ToString())
        dictTotals.Add("Balance", Format(AfDebtAmount - AfPayment, "N2").ToString())

        ''The following to return the double values.
        ''In this case change the return value to Dictionary(Of String, Double)
        'Dim dictTotals As New Dictionary(Of String, Double)
        'dictTotals.Add("CashAmount", AfCashAmount)
        'dictTotals.Add("DebtAmount", AfDebtAmount)
        'dictTotals.Add("Payment", AfPayment)
        'dictTotals.Add("Balance",AfDebtAmount - AfPayment)

        Return dictTotals
    Catch ex As Exception
        MsgBox(ex.ToString)
        Return Nothing
    Finally
        cmd.Dispose()
        conn.Close()
    End Try
End Sub

因此,您可以将结果用于您想要的任何其他Form,如下所示:

Dim dictTotals As Dictionary(Of String, String) = LoadTotals()
txtAFCash.Text = dictTotals("CashAmount")
txtAFDebt.Text = dictTotals("DebtAmount")
txtAFPayment.Text = dictTotals("Payment")
txtAFBalance.Text = dictTotals("Balance")