0

我需要绘制三个随机长度的均匀线,它们至少足够大,以便可以看到这些线。最大长度应该是一个按钮的大小。三条线应在按钮下方绘制。我必须使用循环。我有 1 行的基本代码,但是如何将其变成具有上述要求的循环?

4

1 回答 1

0

您需要确定间距,并为您的起始位置和长度设置一些变量,使用 Random.Next 方法在您选择的参数内设置长度。看看这样的东西是否适合你。

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


Private Sub drawLine()
    Dim g As Graphics = CreateGraphics()
    Dim spacing As Integer = 5
    Dim xStart As Integer = 50 ''Start of line
    Dim xEnd As Integer  'End of Line
    Dim minLength As Integer = 10
    Dim maxLength As Integer = 70
    Dim rndm As New Random
    Dim i As Integer
    Dim y As Integer = 50 'Top of first Line
    g.Clear(Me.BackColor) 'Reset Drawing

    Do Until (i = 3)
        xEnd = rndm.Next(minLength, maxLength)
        g.DrawLine(Pens.Red, New Point(xStart, y), New Point(xStart + xEnd, y))
        y += spacing
        i += 1
    Loop
    g.Dispose()
End Sub
于 2013-02-24T05:57:20.223 回答