2014-02-28 62 views
1

我一直試圖在我的list box中存儲幾個座標點。將座標點存儲在列表框中?

當我點擊表格上的任意一點時,XY座標應存儲在listbox中。我需要至少10個這樣的點。我一次只能做一個。一次,我單擊下一個點,它將用新座標替換第一個座標。

我也想附加一些屬性到每個點,比如說,座標X,Y的成本是10美元。但我的主要挑戰是首先將所有這些協調統一起來。

請告訴如何去做!

+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 
0

您可以使用Windows窗體的onClick事件來獲取鼠標單擊點的座標。您可以將此點添加到集合(最好是一個數組,因爲大小是固定的,僅用於存儲10個座標)。然後,您可以將您的座標添加到特定索引處的數組中。

當從0-9增加索引時,如果索引達到9,則將其更改爲0,然後繼續增量循環以在陣列中存儲N個座標點,並用新的座標點替換舊座標點。

對於附連到每個屬性座標可以使用的

KeyValuePair(Of Point, Double)(10) 

陣列您可以在這裏的座標點存儲爲鍵和其作爲值成本。

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 

說明

其實這不需要解釋!它將添加在單獨的列表框中。第二個只會在1個列表框中爲你做。

如果你想節省僅有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 

希望它完美的作品!

+1

請問爲什麼要導入'GetCursorPos'?只需使用Control.MousePosition。 –

+0

@Bjørn-RogerKringsjå,是的,我知道。更好地使用簡單的一個。我給了OP關於這個話題的所有知識。現在,他/她希望選擇任何他感到滿意的。 –

0

這就是我終於做到的:

進口系統。ComponentModel

公共類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) 

末級

+0

你不需要兩個列表。刪除'list2',並設置像這樣的cost:'Me.list.Insert(0,New ClickInfo()With {.Location = Me.PointToClient(Control.MousePosition),.Cost = RandomNumber()})'' –

+0

順便說一句,如果我的答案解決了你的問題,請點擊大的複選框接受它作爲答案,因爲其他人可能會覺得它有用;) –