2012-11-08 66 views
2

我對編程和vb.net非常陌生,試圖自我教導更多以作爲一種愛好,因爲我對一個我覺得有用的程序有個想法,但是我遇到了麻煩過去這個問題,我相信這是與計時器。Timer_tick不停止麻煩

我有一個大小爲(600,600)的表單,其大小爲一個按鈕(450,150),它是表單上設置的位置(100,50)。點擊時,我想向下移動它自己的高度,然後在它的位置添加一個新按鈕。下面的代碼適用於前兩次點擊所需的操作,但在第三次點擊時按鈕保持移動狀態,並且自動滾動條延伸。我最初認爲這是自動滾動功能或位置屬性,但意識到隨着按鈕不斷移動,定時器還沒有停止。我意識到代碼在實現結果方面可能非常笨重,並且編譯器目前會跳過一些行/變量(這些是從較早的嘗試中找出的)。

我環顧四周,無法找到問題的原因。任何幫助將不勝感激。道歉,如果代碼塊看起來凌亂 - 首先去。

Public Class frmOpenScreen 
Dim intWButtons, intCreateButtonY, intCreateButtonX 'intTimerTick As Integer 
Dim arrWNames() As String 
Dim ctrlWButtons As Control 
Dim blnAddingW As Boolean 

Private Sub btnCreateW_Click(sender As System.Object, e As System.EventArgs) Handles btnCreateW.Click 
    'Creates new Button details including handler 
    Dim strWName, strWShort As String 
    Dim intCreateButtonY2 As Integer 
    Static intNumW As Integer 
    Dim B As New Button 

    strWName = InputBox("Please enter the name name of the button you are creating. Please ensure the spelling is correct.", "Create W") 
    If strWName = "" Then 
     MsgBox("Nothing Entered.") 
     Exit Sub 
    End If 
    strWShort = strWName.Replace(" ", "") 
    B.Text = strWName 
    B.Width = 400 
    B.Height = 150 
    B.Font = New System.Drawing.Font("Arial Narrow", 21.75) 
    B.AutoSizeMode = Windows.Forms.AutoSizeMode.GrowAndShrink 
    B.Anchor = AnchorStyles.Top 
    B.Margin = New Windows.Forms.Padding(0, 0, 0, 0) 
    'Updates Crucial Data (w name array, number of w buttons inc Create New) 
    If intNumW = 0 Then 
     ReDim arrWNames(0) 
    Else 
     intNumW = UBound(arrWNames) + 1 
     ReDim Preserve arrWNames(intNumW) 
    End If 
    arrWNames(intNumW) = strWShort 
    intNumW = intNumW + 1 
    intWButtons = WButtonCount(intWButtons) + 1 
    'updates form with new button and rearranges existing buttons 
    intCreateButtonY = btnCreateW.Location.Y 
    intCreateButtonX = btnCreateW.Location.X 
    ‘intTimerTick = 0 
    tmrButtonMove.Enabled = True 
    ‘Do While intTimerTick < 16 
    ‘ 'blank to do nothing 
    ‘Loop 
    'btnCreateW.Location = New Point(intCreateButtonX, intCreateButtonY + 150) 
    B.Location = New Point(intCreateButtonX, intCreateButtonY) 
    Me.Controls.Add(B) 
    B.Name = "btn" & strWShort 
    intCreateButtonY2 = btnCreateW.Location.Y 
    If intCreateButtonY2 > Me.Location.Y Then 
     Me.AutoScroll = False 
     Me.AutoScroll = True 
    Else 
     Me.AutoScroll = False 
    End If 
    'MsgBox(intCreateButtonY) 
End Sub 

Function WButtonCount(ByRef buttoncount As Integer) As Integer 
    buttoncount = intWButtons 
    If buttoncount = 0 Then 
     Return 1 
    End If 
    Return buttoncount 
End Function 

Public Sub tmrButtonMove_Tick(sender As System.Object, e As System.EventArgs) Handles tmrButtonMove.Tick 
Dim intTimerTick As Integer 
    If intTimerTick > 14 Then 
     intTimerTick = 0 
    End If 
    If btnCreateW.Location.Y <= intCreateButtonY + 150 Then 
     btnCreateW.Top = btnCreateW.Top + 10 
    End If 
    intTimerTick += 1 
If intTimerTick = 15 Then 
     tmrButtonMove.Enabled = False 
    End If 
End Sub 

End Class 

所以我目前的理解是,Tick事件處理程序應該是每一個觸發時增加timertick變量,而一旦它擊中15應該魔鬼定時器和停止鍵移動,但它不是這樣做。

在此先感謝。

回答

2

IntTimerTick在每個Tick事件開始時初始化爲0。這不會發生,如果你聲明它是靜態的:

Static Dim intTimerTick As Integer 
+0

謝謝你,這個伎倆。我並沒有想到變量每次都會重新初始化。 – Lindsay