在窗体中调用具有单击事件的函数(VB)

时间:2017-10-30 23:02:01

标签: vb.net forms visual-studio

我正在构建一个表单,将数字分数转换为字母等级,并且需要调用当我单击“btnCalc”按钮时调用的函数。表单上的输入字段标题为“txtScore”,输出字段为“txtGrade”。

以下代码无效,我也收到此错误:“ 'txtScore'含糊不清,因为类“

中存在多种具有此名称的成员

我不确定会出现什么问题,我已经完成了很多教程和文档,但似乎无法解决。有人能帮忙吗?

Public Class GradeForma

Dim txtScore = Math.Round(txtScore)

Public Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
    GetLetterGrade(txtScore)
End Sub

Public Function GetLetterGrade(ByVal dblGrade As Double) As String
    If txtScore <= 59 Then
        txtGrade.Text = "F"
    ElseIf txtScore <= 69 Then
        txtGrade.Text = "D"
    ElseIf txtScore <= 79 Then
        txtGrade.Text = "C"
    ElseIf txtScore <= 89 Then
        txtGrade.Text = "B"
    ElseIf txtScore <= 100 Then
        txtGrade.Text = "A"
    End If

End Function

1 个答案:

答案 0 :(得分:1)

此示例不需要传递参数,只需在GetLetterGrade()中需要时从txtScore中提取值。您可能根本不需要类级变量 - 因为您可能不需要保留结果?

Public Class GradeForma

    Public Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click
        GetLetterGrade()
    End Sub

    Public Function GetLetterGrade() As String

        Dim dblResult as Double = Math.Round(txtScore)

        If dblResult <= 59 Then
            txtGrade.Text = "F"
        ElseIf dblResult <= 69 Then
            txtGrade.Text = "D"
        ElseIf dblResult <= 79 Then
            txtGrade.Text = "C"
        ElseIf dblResult <= 89 Then
            txtGrade.Text = "B"
        ElseIf dblResult <= 100 Then
            txtGrade.Text = "A"
        End If

    End Function