如何使inputBox正常并按[Enter]键使其再次弹出循环

时间:2014-04-22 22:57:27

标签: vb.net

主要问题是:当用户点击“确定”或按键盘上的[Enter]时,如何使InputBox保持弹出状态?此程序将每个值存储在顺序访问文件中。

Option Explicit On
Option Strict On
Option Infer Off

Public Class frmMain

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click


End Sub

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    Dim outFile As IO.StreamWriter
    Dim strInput As String
    Dim dblInput As Double

    'Do 
    strInput = InputBox("Enter A Number", "Test Scores Project")
    Double.TryParse(strInput, dblInput)

    outFile = IO.File.CreateText("Scores.txt")

    outFile.WriteLine(dblInput)
    'While user presses the enter button or ok keep asking for more numbers

    outFile.Close()

    Me.Close()
End Sub

Private Sub btnCount_Click(sender As Object, e As EventArgs) Handles btnCount.Click
    Dim inFile As IO.StreamReader
    Dim strMessage As String
    Dim intFiles As Integer

    If IO.File.Exists("Scores.txt") Then
        inFile = IO.File.OpenText("Scores.txt")
    Else
        MessageBox.Show("No such file exists", "Text Scores Project")
    End If

    Do Until inFile.Peek = -1
        strMessage = inFile.ReadLine
        intFiles += 1
    Loop

    lblNumber.Text = intFiles.ToString

End Sub

结束班

2 个答案:

答案 0 :(得分:1)

尝试在声明中初始化outfile:

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    Using outFile As New IO.StreamWriter("Scores.txt")
        Dim strInput As String
        Dim dblInput As Double

        Do
        strInput = InputBox("Enter A Number(done when finished)", "Test Scores Project")
        If Double.TryParse(strInput, dblInput) Then                
            outFile.WriteLine(dblInput)
        End If
        While strInput <> "done"

    End Using

        Me.Close()
End Sub

简单的while循环可以在这里工作。

注意:使用Using块。这将处理流的所有清理。

答案 1 :(得分:1)

您收到有关inFile的警告的原因是编译器识别出代码路径可以通过Else的{​​{1}}分支,而无需为{If IO.File.Exists("Scores.txt")分配值1}}。

尝试执行此操作:inFile