Timer_tick无法停止的问题

时间:2012-11-08 02:50:07

标签: vb.net .net-4.0

我是编程和vb.net的新手,尝试自学更多,所以作为一种爱好,因为我对一个我觉得有用的程序有一个想法,但是我遇到了这个问题并且我遇到了麻烦相信它与计时器有关。

我有一个大小的形式。(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,它应该调整计时器并停止按钮移动,但它没有这样做。 / p>

提前致谢。

1 个答案:

答案 0 :(得分:2)

IntTimerTick在每个Tick事件开始时初始化为0。如果您将其声明为静态,则不会发生这种情况:

Static Dim intTimerTick As Integer