我将如何返回到某一行 Visual Basic 2013?

How would I go back to a certain line Visual Basic 2013?

我目前正在使用 visual basic 创建一个项目,但卡在了某个点上... 一旦选定的操作完成,我试图回到某个点。 如果可能的话,我想重复 Button1_Click 子。 到目前为止,这是我的代码。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Button1.Text = ("Transfer")
    TextBox1.Text = ("This program only allows the use of one application at a time in order to prevent corrupt files etc.")
    CheckBox1.Show()
    CheckBox2.Show()
    CheckBox3.Show()
    CheckBox4.Show()
    If CheckBox1.Checked Then
        CheckBox2.Enabled = False
        CheckBox3.Enabled = False
        CheckBox4.Enabled = False
        TextBox2.Show()
        TextBox2.Text = ("Completed without any faults : If error occurs resort to readme in download file.")
        Button1.Text = ("Quit")
    End If
    If CheckBox2.Checked Then
        CheckBox1.Enabled = False
        CheckBox3.Enabled = False
        CheckBox4.Enabled = False
        TextBox2.Show()
        TextBox2.Text = ("Completed without any faults : If error occurs resort to readme in download file.")
        Button1.Text = ("Quit")
    End If
    If CheckBox3.Checked Then
        CheckBox1.Enabled = False
        CheckBox2.Enabled = False
        CheckBox4.Enabled = False
        TextBox2.Show()
        TextBox2.Text = ("Completed without any faults : If error occurs resort to readme in download file.")
        Button1.Text = ("Quit")
    End If
    If CheckBox4.Checked Then
        CheckBox2.Enabled = False
        CheckBox3.Enabled = False
        CheckBox1.Enabled = False
        TextBox2.Show()
        TextBox2.Text = ("Completed without any faults : If error occurs resort to readme in download file.")
        Button1.Text = ("Quit")
    End If
End Sub

不要。

您已经在该点击处理程序中实现了太多功能,就此打住!


提取方法

你需要在这里重构一点 - 为这部分提取一个方法:

TextBox2.Show()
TextBox2.Text = ("Completed without any faults : If error occurs resort to readme in download file.")
Button1.Text = ("Quit")

然后用方法调用替换该片段的所有副本。

然后为每个块提取另一个方法 - 并命名它们

也考虑重命名您的 controls/buttons - 稍后感谢您自己:)

完成后,您的点击处理程序读起来应该像是对正在发生的事情的高级总结,而不是像 1997 年编写的单一脚本 - 因为这就是您现在的执行流程。

为您希望代码完成的每个 一件事情 编写一个方法,然后调用这些方法。简单!


如果您的代码完成了它应该做的事情(即按预期工作),并且看起来凌乱、混乱且效率低下,请考虑下次在 Code Review 上提问!