2

私はプログラミングとvb.netに非常に慣れておらず、趣味としてもっと独学しようとしていますが、役立つプログラムのアイデアがあるので、この問題を乗り越えるのに苦労しています.タイマーを操作します。

サイズ (600,600) のフォームと、サイズ (450,150) のボタンが 1 つあり、フォームの場所 (100,50) が設定されています。クリックすると、それ自体の高さを下に移動し、その場所に新しいボタンを追加します。以下に含まれるコードは、最初の 2 回のクリックでは目的どおりに機能しますが、3 回目のクリックではボタンが動き続け、自動スクロール バーが伸びます。最初はオートスクロール機能か位置プロパティかと思ったのですが、ボタンが動き続けているのにタイマーが止まらないことに気付きました。結果を達成するという点でコードがおそらく非常に不格好であること、およびコンパイラによって現在スキップされている行/変数がいくつかあることを認識しています(これらはこれを理解しようとする古い試みによるものです)。

私は周りを見回しましたが、私の問題の原因を見つけることができません。どんな助けでも大歓迎です。コード ブロックが乱雑に見える場合はお詫び申し上げます。まず行ってください。

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 に答える