2016-03-04 65 views
1
Public Sub changePoints(ByVal Name As String, StartPoint As Boolean, Point As Integer) 

    Locations(Point).Name = Name 

    If StartPoint = True Then 
     For i = 0 To 19 
      Locations(i).StartPoint = False 
     Next 
    End If 
    Locations(Point).StartPoint = StartPoint 
    DrawGraph() 
End Sub 

Private Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click 

    GraphicalPlot.changePoints(PointNameBox.Text, MakeStart.Checked, LoadedPointValue) 

End Sub 

所以我有2種形式。第一種形式,當點擊一個選定的點時,在可編輯的文本框中顯示第二個形式的點的詳細信息。當您單擊第二個窗體上的保存按鈕以保存細節時,上面的第二個子例程會運行,並在保存全局變量的第一個窗體中調用子例程。全局數組不保存它所做的更改

的信息傳遞到在正確的第一形式的子程序。當我改變它後立即檢查全局數組的值(緊跟在Locations(Point).Name = Name之後),它表示該值已被正確更改。

然而,子程序結束後,在全局陣列返回到它有子程序被調用之前,並沒有i的輸入的信息發送到第二形式跡的值。

如果有幫助,全球陣列是一個結構:

Structure EnteredPoint 
    Dim Xcoord As Integer 'Stores X co-ordinate of point 
    Dim Ycoord As Integer 'Store Y co-ordinate of point 
    Dim Name As String  'Stores name of point, used to check if point is unused 
    Dim StartPoint As Boolean 'Checks if the point is the start point 
    Dim Selected As Boolean  'Checks if the point is currently being hovered over the mouse 
    Dim nextPoint As Integer 'Used to implement a linked list, making deleting and recreating points easier 
End Structure 

名稱變回那是什麼應該是不可能的;我的程序在初始化之外改變名稱的唯一時間(將其設置爲「未使用」,或已創建的點數)在這裏。

我GOOGLE了它,把它拿給我的老師和朋友,我們不能任憑它玩了幾個小時找到任何東西。任何幫助將不勝感激,因爲這是我的Comp4課程!

編輯:增加了對vbnet3d

Sub DrawGraph() 
    'Used to draw the current state. 
    G = Me.CreateGraphics 
    G.Clear(Color.White) 'Sets entire background to white 
    Dim placeholder As Integer = 0 'Used to store the current point being checked. 
    If UsedLocations > 0 Then 'This part will only run if any points have been made 
     Do Until Locations(placeholder).nextPoint = 0 'Loops until all points have been drawn 

      If Locations(placeholder).StartPoint = True Then 'will only draw this if it is the starting point 
       G.FillEllipse(Brushes.LightBlue, Locations(placeholder).Xcoord - 3, Locations(placeholder).Ycoord - 3, 16, 16) 
      End If 
      If Locations(placeholder).Selected = True Then  'Will only draw this if it is the currently selected point 
       G.FillEllipse(Brushes.LightGreen, Locations(placeholder).Xcoord - 3, Locations(placeholder).Ycoord - 3, 16, 16) 
      End If 
      'Draws the actual Point 
      G.FillEllipse(Brushes.Black, Locations(placeholder).Xcoord, Locations(placeholder).Ycoord, 10, 10) 
      If UsedLocations <= 20 Then 
       placeholder = Locations(placeholder).nextPoint 'Gets the next point to be checked. 
      End If 


     Loop 
     If UsedLocations = 20 Then 
      If Locations(placeholder).Selected = True Then  'Will only draw this if it is the currently selected point 
       G.FillEllipse(Brushes.LightGreen, Locations(placeholder).Xcoord - 3, Locations(placeholder).Ycoord - 3, 16, 16) 
      End If 
      'Draws the actual Point 
      G.FillEllipse(Brushes.Black, Locations(placeholder).Xcoord, Locations(placeholder).Ycoord, 10, 10) 
     End If 
    End If 

End Sub 
+0

哪裏位置的數組聲明? – vbnet3d

+1

可能與默認表單引用有關。什麼是表單名稱? – Plutonix

+0

位置數組聲明在公共類的頂部,全局,第一行圖形區域 – Hellfire

回答

0

您的問題是由默認的形式引用造成的。 (請參閱@Plutonix的評論)這意味着您有一個名爲「GraphicalPlot」的基類實例,但您也有一個活動的「GraphicalPlot」實例,它是一個單獨的實例。這兩個類在形式上是相同的,但不共享內存數據(一個是另一個的模型)。當你進行下面的調用,它引用了模型的形式,而不是活性形​​式:

Private Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click 

    GraphicalPlot.changePoints(PointNameBox.Text, MakeStart.Checked, LoadedPointValue) 

End Sub 

您需要創建爲活性形式(GraphicalPlot)的引用,並將它傳遞給GraphicsMenu使用作爲當前實例參考。

GraphicsMenu.Load(Me, PointName, PointNum) 

而且在GraphicsMenu:沿東西線

Dim GP As GraphicalPlot 
Public Sub Load(ByVal PlotForm As GraphicalPlot, ByVal Name As String, pointnumber As Integer) 

    GP = PlotForm 

End Sub 
1

的問題是,您使用的數組中的值類型(結構)。當你訪問數組元素時,你會得到該項目的副本,所以你的更改只能在副本上進行。

你將不得不從陣列得到EnteredPoint,根據需要更改屬性,然後重新分配點回陣列:

Public Sub changePoints(ByVal Name As String, StartPoint As Boolean, Point As Integer) 

    Dim pointToChange As EnteredPoint = Locations(Point) 

    pointToChange.Name = Name 

    If StartPoint = True Then 
     For i = 0 To 19 
      Dim tmp As EnteredPoint = Locations(i) 
      tmp.StartPoint = False 
      Locations(i) = tmp 
     Next 
    End If 

    pointToChange.StartPoint = StartPoint 
    Locations(Point) = pointToChange 

    DrawGraph() 
End Sub 

如果您想保留原來的語法,則可以更改已將EnteredPoint添加到班級,並且可以按照您的預期工作。

+0

剛剛嘗試複製/粘貼你建議的編輯過的子程序,仍然沒有骰子:/在檢查了正在使用的內容後,加載到PointToChange中的位置(點)似乎爲空。 – Hellfire

+0

@Hellfire - 如果Locations是一個結構體EnteredPoint的數組,它不能爲null,因爲值類型不能爲null。如果整數'Point'指向數組中的有效索引,則它不能爲空。如果整數無效,您將得到一個'IndexOutOfRangeException'。 –

相關問題