我对编程和 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,它应该禁用计时器并停止按钮移动,但它没有这样做。
提前致谢。
最佳答案
IntTimerTick 在每个 Tick 事件开始时初始化为 0。如果您将其声明为静态,则不会发生这种情况:
Static Dim intTimerTick As Integer
关于vb.net - Timer_tick 无法停止的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13281691/