错误:从VB中的不同子调用子(例程)

时间:2016-02-06 18:02:08

标签: vb.net visual-studio subroutine

我试图让程序多次执行(而不是只在表单加载时执行一次);我试图通过用户单击按钮或在“勾选”后再次执行程序的计时器来执行此操作。

我已将错误注释掉了代码:

Public Class Forml

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        Randomize()
        Dim i As Integer
        Dim z As Integer
        z = Rnd() * 25
        For i = 0 To 2
            Dim intx As Integer = Rnd() * 420
            Dim inty As Integer = Rnd() * 420
            Dim tempBrush As Brush = New SolidBrush(Color.FromArgb(Rnd() * 255, Rnd() * 255, Rnd() * 255))
            e.Graphics.FillEllipse(tempBrush, intx, inty, 2, 2)
        Next
    End Sub

    'Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    '   Call Form1_Paint()
    'End Sub

    'Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
    '   Call Form1_Paint()
    'End Sub

End Class

1 个答案:

答案 0 :(得分:0)

在这种情况下看似简单的尝试实际上只需要一些背景知识就可以了。

  • 只要表单需要重新绘制,就会引发Form1.Paint事件,例如,如果表单已被其他表单覆盖。对于本练习,您可能不会关注比您最初看起来产生的代码更多的点。我不会在这里解决这个问题。

  • 如果您想要保留图片,e.Graphics对象不是您想要的,而是想要使用从Control.CreateGraphics获得的对象。

  • 建议使用.NET框架Random class而不是VB的Rnd()

  • 有些类使用的资源需要具体disposed,因此他们使用.Dispose()方法。图形对象往往是这样的(也是数据库和文件操作)。您可以使用Using块来处理为您调用Dispose。

非常重要的一点:

  • 使用Option Strict On。它将通过指出它可以检测到的任何错误来帮助您编写有效的代码。将其设置为新项目的默认值。

我应该提到的另外一件事是,在命名变量时应该小心一点,并且它使调试更容易将一些事情分离到他们自己的变量中,而不是编写一长串代码。

对于像计时器这样的东西,可以将计时器拖到绘图表面上,我觉得最好在代码中创建它。这样你就不必在设计器和代码窗口之间来回切换来改变参数,你也不知道“Timer1”之类的东西来自何处。

毕竟,我提出了(不一定非常好)的代码:

Option Infer On
Option Strict On

Public Class Form1

    Dim rand As New Random
    Dim tim As Timer

    Sub DrawDots(sender As Object)
        ' This sub could have been called with anything as sender, so check that
        ' it is actually a Control
        Dim self As Control = TryCast(sender, Control)
        If self Is Nothing Then
            Exit Sub
        End If

        Using g = self.CreateGraphics()

            Dim nDots = rand.Next(1, 26)
            Dim circleRadius = 20

            For i = 1 To nDots
                Dim xLoc = rand.Next(0, 420)
                Dim yLoc = rand.Next(0, 420)
                Dim col = Color.FromArgb(rand.Next(0, 256), rand.Next(0, 256), rand.Next(0, 256))

                Using br As New SolidBrush(col)
                    g.FillEllipse(br, xLoc, yLoc, circleRadius, circleRadius)
                End Using

            Next
        End Using

    End Sub

    Sub DrawDotsOnTimerTick(sender As Object, e As EventArgs)
        DrawDots(Me)

    End Sub

    Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
        DrawDots(sender)

    End Sub

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

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        tim = New Timer
        tim.Interval = 2000
        AddHandler tim.Tick, AddressOf DrawDotsOnTimerTick
        tim.Start()

    End Sub

End Class