2

我对编程和 vb.net 非常陌生,尝试更多地自学以作为一种爱好,因为我有一个我认为有用的程序的想法,但我无法解决这个问题,我相信它是与计时器有关。

我有一个 size.(600,600) 的表单,带有一个 size.(450,150) 的按钮,在表单上设置了 location(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,它应该禁用计时器并停止按钮移动,但它没有这样做。

提前致谢。

4

1 回答 1

2

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

Static Dim intTimerTick As Integer
于 2012-11-08T04:46:47.487 回答