只有两个文件而不是100个

时间:2015-04-27 14:13:23

标签: vb.net file system.io.file

基本上我有这个文件创建程序,它帮助我制作一堆文件,出于某种原因,它只会指定2个而不是100个文件。 代码:

Imports System.IO
Public Class Main

    Dim CURRENT_NUM As Integer
    Dim CONTENT_STRING As String
    Dim PATH_STRING As String
    Dim FROM_NUM As Integer
    Dim TO_NUM As Integer

    Public Sub SetupVar()
        Try
            CONTENT_STRING = tbContent.Text
            PATH_STRING = tbPath.Text
            FROM_NUM = Integer.Parse(tbFrom.Text)
            TO_NUM = Integer.Parse(tbTo.Text)
        Catch ex As Exception
        End Try
    End Sub

    Private Sub Make()
        tmrMake.Enabled = True
    End Sub

    Private Sub btnMake_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMake.Click
        SetupVar()
        Make()
    End Sub

    Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click
        Me.Close()
    End Sub

    Private Sub tmrMake_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrMake.Tick
        'disable quit
        'and control box
        Me.ControlBox = False
        btnQuit.Enabled = False
        CURRENT_NUM = FROM_NUM
        CURRENT_NUM = CURRENT_NUM + 1
        Using sw As New StreamWriter(PATH_STRING & CURRENT_NUM & ".txt")
            sw.Write(CONTENT_STRING)
        End Using
        If CURRENT_NUM = TO_NUM Then
            btnQuit.Enabled = True
            Me.ControlBox = True
            tmrMake.Enabled = False
        End If
    End Sub
End Class

结果是文件夹中的c1和c2。而不是1-100正如我在两个tb。

中指定的那样

1 个答案:

答案 0 :(得分:1)

你需要移动一些代码然后你应该好。请参阅下面的代码......

Imports System.IO
Public Class Main

    Dim CURRENT_NUM As Integer
    Dim CONTENT_STRING As String
    Dim PATH_STRING As String
    Dim FROM_NUM As Integer
    Dim TO_NUM As Integer

    Public Sub SetupVar()
        Try
            CONTENT_STRING = tbContent.Text
            PATH_STRING = tbPath.Text
            FROM_NUM = Integer.Parse(tbFrom.Text)
            TO_NUM = Integer.Parse(tbTo.Text)

            ' Moved code from tmrMake_Tick to here
            'disable quit
            'and control box
            Me.ControlBox = False
            btnQuit.Enabled = False
            CURRENT_NUM = FROM_NUM
        Catch ex As Exception
        End Try
    End Sub

    Private Sub Make()
        tmrMake.Enabled = True
    End Sub

    Private Sub btnMake_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMake.Click
        SetupVar()
        Make()
    End Sub

    Private Sub btnQuit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.Click
        Me.Close()
    End Sub

    Private Sub tmrMake_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrMake.Tick
        Using sw As New StreamWriter(PATH_STRING & CURRENT_NUM & ".txt")
            sw.Write(CONTENT_STRING)
        End Using

        ' Increment your CURRENT_NUM after you've written the file
        CURRENT_NUM = CURRENT_NUM + 1
        If CURRENT_NUM = TO_NUM Then
            btnQuit.Enabled = True
            Me.ControlBox = True
            tmrMake.Enabled = False
        End If
    End Sub
End Class