【问题标题】:How to create array in plotting points如何在绘图点中创建数组
【发布时间】:2015-10-25 11:42:40
【问题描述】:

我想在pb1(图片框)中绘制椭圆,但椭圆的数量取决于查询将给出什么。通常,它大于 1。我下面已经有一个代码,但它只画了一个椭圆。

我想为此使用数组,但我不知道该怎么做。任何帮助/协助将不胜感激。谢谢

Sub getdots()
    Try
        Call MyConnection()
        Sql = "select l.lot_no, l.lot_x, l.lot_y FROM lot_details AS l, area as a, type as t where l.type_no=t.type_no and t.AREA_NO=a.AREA_NO and a.AREA_NO=@Anum AND l.LOT_STATE='available'"
        Dim cmd As New MySqlCommand(Sql, Con)
        With cmd
            .CommandText = Sql
            .Connection = Con
            .Parameters.AddWithValue("@Anum", frmvacantlots.cboareano.Text)
            .ExecuteNonQuery()
        End With
        Dim reader As MySqlDataReader
        reader = cmd.ExecuteReader
        If reader.Read Then
            Try

                x = reader.GetString(1)
                y = reader.GetString(2)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
    Con.Close()
End Sub


Private Sub pb1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pb1.Paint
    e.Graphics.FillEllipse(Brushes.Red, x - 5, y - 5, 10, 10)
    getdots()
End Sub

【问题讨论】:

  • getdots 的结果存储在Point 对象列表中。然后在 Paint 中对列表中的每个点循环执行FillEllipse
  • @Saragis 我不熟悉点对象列表,我不知道当我尝试搜索它时如何。你能帮助我吗?非常感谢
  • 我在答案中添加了一个示例,请查看

标签: arrays vb.net


【解决方案1】:

创建一个列表来存储所有值:

Private points As New List(Of Point)

从您的查询中读取所有点:

While reader.Read
    Try
        x = reader.GetInt32(1)
        y = reader.GetInt32(2)
        points.Add(New Point(x, y))
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End While

对于 points 中的每个值,像你已经为一个值所做的那样绘制:

Private Sub pb1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pb1.Paint
    points.ForEach(Sub(p) e.Graphics.FillEllipse(Brushes.Red, p.X - 5, p.Y - 5, 10, 10))
    getdots()
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多