Visual Basic使用Drawline - 使用循环绘制3行

时间:2013-02-24 05:34:06

标签: vb.net

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

1 个答案:

答案 0 :(得分: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