【发布时间】:2021-03-25 22:08:15
【问题描述】:
我有一个从VB.NET 函数中的点列表到PictureBox 的函数。将两个点添加到列表中,并在列表中添加的所有点之间绘制一条线。但是,当向列表中添加一个新点时,我希望将所有先前绘制的线移到左侧。
例如,如果从列表中分别在(9, 0) (9, 4) 和(10, 0) (10, 3) 之间绘制了两条不同的线,并将第三行添加到列表(10, 0) (10, 2) 中,我想像这样移动前两行:
(9, 0), (9, 4) 到 (8, 0), (8, 4)
(10, 0), (10, 3) 到 (9, 0), (9, 3)
这是我的意思的演示。
绿线是第一条线,黄线是第二条线。在第二张图中,添加了一条深蓝色的线,并且前两条线,黄色和绿色,都向左移动。
我正在使用此代码来尝试绘制这些类型的线条。
Public Class Line
Public ReadOnly Property StartPoint As Point
Public ReadOnly Property EndPoint As Point
Public Sub New(startPoint As Point, endPoint As Point)
Me.StartPoint = startPoint
Me.EndPoint = endPoint
End Sub
End Class
Public Class Form1
Private lines As New List(Of Line)
Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
For Each line In lines
e.Graphics.DrawLine(Pens.Black, line.StartPoint, line.EndPoint)
Next
End Sub
Private Sub AddNewLine(length As Integer)
Dim pictureBoxRightXPoint As Integer = 300 'the right most side of the PictureBox is the x coordinate of 300 (PictureBox has the x size of 300).
For Each l As line In lines 'move all points of X in all previously drawn lines in the list to the left.
l.StartPoint.X = l.StartPoint.X - 1
l.EndPoint.X = l.EndPoint.X - 1
Next
lines.Add(New Line((pictureBoxRightXPoint, 0), (pictureBoxRightXPoint, length))
PictureBox1.Invalidate() 'Refresh the PictureBox to redraw the lines.
End Sub
End Class
添加后,我收到以下错误:
表达式是一个值,因此不能作为赋值的目标
当我尝试从 X 坐标中减去 1 以将列表中的所有点向左移动时。
在将所有先前绘制的点向左移动时,如何解决此问题或在PictureBox 中绘制一条线? (注意:列表中可能已经添加了多行)。
(仅在列表中绘制和添加行的代码来自:https://stackoverflow.com/a/66621165/14924603)。
【问题讨论】:
-
您确定要在图片框上画线吗?认为您可能会发现每次添加线条并重新绘制整个内容时都必须清除图片框。关于主题,您是否有理由将 StartPoint 和 Endpoint 属性设置为只读,值得注意的是 vb 命名不区分大小写 startPoint 和 StartPoint 是同一件事
-
是的。我的主要观点是只移动列表中已有的数字@Hursey
-
如果您的起点和终点不是只读的会怎样?