我有一系列测试,当用户完成时,分数存储在变量中并写入文件。在我的程序中是每个用户的进度屏幕,它应该显示标签中的所有测试分数。
我已使用此代码在标签中显示分数:lblTopic1Score.Text = Topic1Score
但是,当我运行系统并进入进度表单时,标签为空白。有人可以帮忙吗?
进度表格代码:
Private Sub StProgress_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lblTopic1Score.Text = Topic1Score
lblTotalScore.Text = TotalScore`
lblStName.Text = namest
LblStSurname.Text = surnamest
End Sub
创建模块以存储公共变量Public Topic1Score As String
这是测试本身的代码摘录
If answers(i) = questions(i, 4) And FileOpenStatusTS = False Then
Topic1Score += 1
TotalScore += 1
End If
If yearst = "12" And classst = "A" Then
FileOpen(1, FileName12A1, OpenMode.Append)
FileOpenStatus12A1 = True
'Once all the details have been entered and checked, then they are written to the Teacher accounts text file'
WriteLine(1, Username, Topic1Score, TotalScore)
FileClose(1)
End If
答案 0 :(得分:0)
显然,当您的表单加载Topic1Score
为null或为空时。
在Debug.Assert(Not String.IsNullOrWhiteSpace(Topic1Score))
事件处理程序的第一行添加StProgress_Load
:
Private Sub StProgress_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Debug.Assert(Not String.IsNullOrWhiteSpace(Topic1Score))
lblTopic1Score.Text = Topic1Score
lblTotalScore.Text = TotalScore
lblStName.Text = namest
LblStSurname.Text = surnamest
End Sub
只要值为null或仅包含空白,这将暂停执行,这样您就可以确保在调试时它永远不会低于雷达。
请注意,Debug.Assert
不会在发布版本中编译,因此您不必担心性能影响。可以把它想象成一个可以帮助您调试的简单工具。
此外,此变量确实不应该是String
类型,而是Integer
。如果你做需要它作为某个操作的字符串,你可以Topic1Score.ToString()
。