从另一个表单vb.net调用方法

时间:2016-06-13 16:05:57

标签: vb.net

我已经做了一段时间的这个应用程序,现在我想从主窗体调用一个方法,并将其包含在另一个窗体中,以使其运行我想要的。在我的主要形式中,这是我的方法:

Public Sub MostrarResultadoTotal()
    SQLCon = New SqlConnection
    SQLCon.ConnectionString = "......"
    Try
        SQLCon.Open()
        Query = "SELECT 
                CAST(SUM(CAST(Filesize as float)) / 1024 / 1024 AS DECIMAL(10,2))
                FROM infofile"
        SqlCmd = New SqlCommand(Query, SQLCon)
        SqlDR = SqlCmd.ExecuteReader
        If SqlDR.Read() Then
            If SqlDR.IsDBNull(0) Then
                lblResultadoTotal.Text = "0,00"
            Else
                lblResultadoTotal.Text = SqlDR.GetDecimal(0)
            End If
        End If
        SQLCon.Close()
    Catch ex As SqlException
        MsgBox(ex.Message)
    Finally
        SQLCon.Dispose()
    End Try
End Sub

我想在删除按钮上调用它。你知道如何解决我的问题吗?

这是方法:

'Mudar a cor das labels dependendos to tamanho total
    Sub ChangeLabelColor()
        If lblResultadoTotal.Text <= 3.9 Then
            lblResultadoTotal.ForeColor = Color.Green
            lblGB.ForeColor = Color.Green
        ElseIf (lblResultadoTotal.Text >= 4) And (lblResultadoTotal.Text <= 6.9) Then
            lblResultadoTotal.ForeColor = Color.Orange
            lblGB.ForeColor = Color.Orange
        ElseIf lblResultadoTotal.Text >= 7 Then
            lblResultadoTotal.ForeColor = Color.Red
            lblGB.ForeColor = Color.Red
        End If

1 个答案:

答案 0 :(得分:2)

在这种情况下,您的方法包含数据访问逻辑,应该与UI层逻辑(Forms)分开。它应该在一个单独的类中,然后由两种形式调用。

Friend Class DataAccess
    Public Function MostrarResultadoTotal() As String
        SQLCon = New SqlConnection
        SQLCon.ConnectionString = "......"
        Try
            SQLCon.Open()
            Query = "SELECT 
                    CAST(SUM(CAST(Filesize as float)) / 1024 / 1024 AS DECIMAL(10,2))
                    FROM infofile"
            SqlCmd = New SqlCommand(Query, SQLCon)
            SqlDR = SqlCmd.ExecuteReader
            If SqlDR.Read() Then
                If SqlDR.IsDBNull(0) Then
                    Return "0,00"
                Else
                    Return SqlDR.GetDecimal(0)
                End If
            End If

        Catch ex As SqlException
            MsgBox(ex.Message)
        Finally
            ' Always close connection 
            SQLCon.Close()
            SQLCon.Dispose()
        End Try
    End Function
End Class

然后,从您的表单中,只需声明日期访问逻辑的实例:

Dim dataAccess = New DataAccess();

并称之为:

lblResultadoTotal.Text = dataAccess.MostrarResultadoTotal()

适用于您的标签颜色

更改标签颜色是UI逻辑,从您的示例中,它也可以重复使用。因此,您应该考虑创建一个新类来管理常见的UI逻辑:

Friend Class CommonUILogic

    Function GetLabelColor(value As Integer) As Color
        If value <= 3.9 Then
            Return Color.Green
        ElseIf (value >= 4) And (value <= 6.9) Then
            Return Color.Orange
        ElseIf value >= 7 Then
            Return Color.Red
        End If
    End Function

End Class

然后你可以这样称呼它:

Dim uiLogic = New CommonUILogic()

Dim labelColor = uiLogic.GetLabelColor(Integer.Parse(lblResultadoTotal.Text))

lblResultadoTotal.ForeColor = labelColor
lblGB.ForeColor = labelColor