有没有办法在运行程序时将按钮更改为另一个按钮,即当用户单击一个名为“display”的按钮时,它会计算结果,然后“display”变成“Restart?”如果用户点击它重新启动程序?我想将btnDisplay更改为btnRestart:
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
' displays a student's grade
Double.TryParse(txtEarned.Text, dblEarned)
For Each minimum As Double In dblMinimumPoints
If dblEarned >= minimum Then
lblGrade.Text = strGrade(gradeIndex)
gradeIndex += 1
End If
Next
txtEarned.ReadOnly = False
btnDisplay.Enabled = False
End Sub
答案 0 :(得分:2)
我想有多种方法可以做到这一点。一种是有两个按钮btnDisplay
和btnRestart
,每个按钮都有所需的Text
,它们在表单设计器中相互叠加。单击它们时可以看到哪一个。
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
' displays a student's grade
Double.TryParse(txtEarned.Text, dblEarned)
For Each minimum As Double In dblMinimumPoints
If dblEarned >= minimum Then
lblGrade.Text = strGrade(gradeIndex)
gradeIndex += 1
End If
Next
txtEarned.ReadOnly = False
btnDisplay.Visible = False
btnReset.Visible = True
End Sub
我不确定你的意思是btnRestart
将“重启程序”,但可能在其点击事件中你同样会隐藏它并让btnDisplay
再次可见。
(另外,您可能希望在这些事件中添加一些Try...Finally
错误处理,这样即使出现问题,您也可以确保执行Visible
行。)
答案 1 :(得分:1)
在您的处理程序中单击并根据.text属性添加一个选择案例,然后如果文本显示更新它将重新启动,如果它是重新启动然后添加goto到您希望脚本在重新启动时从哪里开始
编辑,下面我已经将codetorun添加为一个函数,这可能是一个比goto更好的解决方案:
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
Select Case btnDisplay.Text
Case "Submit"
codeiwanttorun()
btnDisplay.Text = "Restart"
Case "Restart"
codeiwanttorun()
End Select
End Sub
Private Function codeiwanttorun()
Double.TryParse(txtEarned.Text, dblEarned)
For Each minimum As Double In dblMinimumPoints
If dblEarned >= minimum Then
lblGrade.Text = strGrade(gradeIndex)
gradeIndex += 1
End If
Next
End Function