在协同列表框中存储坐标点?

时间:2014-02-28 06:24:09

标签: vb.net listbox coordinates

我一直试图在list box中存储几个坐标点。

当我点击表单上的任意点时,XY坐标应存储在listbox中。我需要至少10个这样的积分。我一次只能做一个。有一次,我点击下一个点,它用新坐标替换第一个坐标。

我还想为每个点添加一些属性,比如,坐标XY的成本为10美元。但我的主要挑战是首先解决所有这些问题。

请告知如何解决这个问题!

4 个答案:

答案 0 :(得分:0)

您可以使用Windows窗体的onClick事件获取单击鼠标的点的坐标。您可以将此点添加到集合(最好是数组,因为大小是固定的,只是存储10个坐标)。然后,您可以将坐标添加到特定索引处的数组中。

当索引从0-9递增时,如果索引达到9,则将其更改为0并继续增量循环,以便在数组中存储N个坐标点,并用新的坐标点替换旧坐标点。

要将属性附加到每个坐标,可以使用

数组
KeyValuePair(Of Point, Double)(10)

在这里,您可以将坐标点存储为Key,将其成本存储为值。

答案 1 :(得分:0)

创建一个可以保存所需数据的自定义类。然后使用BindingList(Of T)作为ListBox的数据源。

以下是一个例子:

Public Class Form1

    Public Sub New()
        Me.InitializeComponent()
        Me.list = New BindingList(Of ClickInfo)
        Me.ListBox1.DataSource = Me.list
        Me.ListBox1.DisplayMember = "Location"
    End Sub

    Private Sub _MybaseClick(sender As System.Object, e As System.EventArgs) Handles MyBase.Click
        Me.list.Insert(0, New ClickInfo() With {.Location = Me.PointToClient(Control.MousePosition), .Cost = 1234})
    End Sub

    Private Class ClickInfo
        Public Property Location() As Point
        Public Property Cost() As Decimal
    End Class

    Private ReadOnly list As BindingList(Of ClickInfo)

End Class

答案 2 :(得分:0)

代码和示例

Private Sub Form1_Click () Handles Mybase.Click   
      Me.Cursor = New Cursor(Cursor.Current.Handle)
      Listbox1.Items.Add (Cursor.Position.X)
      Listbox2.Items.Add (Cursor.Position.Y)
End Sub

或者,

Public Declare Auto Function GetCursorPos Lib "User32.dll" (ByRef lpPoint As Point) As Integer
Dim mousepos As Point
' This stores the cordinates of the mouse cursors location

Private Sub Form1_Click () Handles Me.Click
    Dim R As Long = GetCursorPos(mousepos) ' You'll get your location in mousepos
    ListBox1.Items.Add(mousepos.X & "x" & mousepos.Y)
End Sub

解释

实际上这不需要解释!它将在两个单独的列表框中添加。第二个将只在一个列表框中为您完成。

根据您的问题,如果您只想保存10个坐标,可以尝试以下方法:

Private Sub Form1_Click () Handles Me.Click

    ' This is to be added
    If ListBox1.Items.Count = 10 Then
        ListBox1.Items.Clear()
    End If
    'till here

    Me.Cursor = New Cursor(Cursor.Current.Handle)
    ListBox1.Items.Add(Cursor.Position.X & "x" & Cursor.Position.Y)
End Sub

希望它完美无缺!

答案 3 :(得分:0)

这是我最终做的:

导入System.ComponentModel

Public Class Form1

'  Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
' Me.Cursor = New Cursor(Cursor.Current.Handle)

' Listbox1.Items.Add(Cursor.Position.X)
' Listbox2.Items.Add(Cursor.Position.Y)
' This is to be added
' If ListBox1.Items.Count = 10 Then
'ListBox1.Items.Clear()
' End If
'till here

'   Me.Cursor = New Cursor(Cursor.Current.Handle)
' ListBox1.Items.Add(Cursor.Position.X & "x" & Cursor.Position.Y)
'End Sub



Public Function RandomNumber() As Integer
    'initialize random number generator
    Dim r As New Random(System.DateTime.Now.Millisecond)

    Return r.Next(1, 100)
End Function



Public Sub New()
    Me.InitializeComponent()
    Me.list = New BindingList(Of ClickInfo)
    Me.list2 = New BindingList(Of ClickInfo)
    Me.ListBox1.DataSource = Me.list
    Me.ListBox2.DataSource = Me.list2
    Me.ListBox1.DisplayMember = "Location"
    Me.ListBox2.DisplayMember = "Cost"
End Sub

Private Sub _MybaseClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Click
    Me.list.Insert(0, New ClickInfo() With {.Location = Me.PointToClient(Control.MousePosition), .Cost = 1234})
    Me.list2.Insert(0, New ClickInfo() With {.Cost = RandomNumber()})
End Sub

Private Class ClickInfo
    Public Property Location() As Point
    Public Property Cost() As Decimal
End Class

Private ReadOnly list As BindingList(Of ClickInfo)
Private ReadOnly list2 As BindingList(Of ClickInfo)

结束班