使用窗体(VB)中的单击事件调用函数 [英] Invoking a function with a click event in form (VB)
问题描述
我正在构建一个表格来将数字分数转换为字母等级,并且需要调用一个当我点击btnCalc按钮时调用的函数。表单上的输入字段标题为txtScore,输出字段为txtGrade。
下面的代码不起作用,我也得到这个错误:
'txtScore'是不明确的,因为在类中存在具有该名称的多种成员
我不确定可能是错的,已经经历了很多教程和文档,但似乎无法解决。有人可以帮忙吗?
公共课程成绩
$ p $在这个例子中,你不需要传递参数,只需在GetLetterGrade中需要它时从txtScore中取出值即可(请参阅参考资料)。 )。您可能根本不需要一个类级别的变量 - 因为您可能不需要坚持结果?
Dim txtScore = Math.Round(txtScore )
Public Sub btnCalc_Click(sender As Object,e As EventArgs)Handle 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然后
txtGrade.Text =B
ElseIf txtScore< = 100 Then
txtGrade.Text =A
End If
End Function
Public Class GradeForma
Public Sub btnCalc_Click(sender As Object,e As EventArgs)Handle btnCalc.Click
GetLetterGrade()
End Sub
Public Function GetLetterGrade()作为字符串
Dim dblResult as Double = Math.Round(txtScore)
如果dblResult< = 59那么
txtGrade.Text =F
ElseIf dblResult< = 69 Then
txtGrade.Text =D
ElseIf dblResult< = 79然后
txtGrade.Text =C
ElseIf dblResult < = 89然后
txtGrade.Text =B
ElseIf dblResult< = 100 Then
txtGrade.Text =A
End If
结束功能
I am building a form to convert a number score into a letter grade and need to invoke a function that is called when I click on the "btnCalc" button. The input field on the form is titled "txtScore" and the output field is "txtGrade".
The code below isn't working and I'm also getting this error: " 'txtScore' is ambiguous because multiple kinds of members with this name exist in class"
I'm not sure what could be wrong, I've gone through a lot tutorials and documentation but can't seem to resolve. Would someone be able to help?
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
解决方案You don't need to pass parameters around for this example, just pull out the value from txtScore when you need it in GetLetterGrade(). You likely don't need a class level variable at all - as you maybe don't need to hold onto the result?
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
这篇关于使用窗体(VB)中的单击事件调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!