数组是一个声明为字符串的9位数组,我有9个用户可以输入数据的文本框,每个文本框写入数组中的一个变量。
我正在尝试停止填充数组,并在用户填写所有9个文本框时移动打印,或者当他按下"写入文件时停止填写#34;写入文件&#34 ;按钮。从我的调试点来看,看起来我已经到了"对于"循环,但程序崩溃没有错误,我可以做出头或尾(不是语法或变量)...任何人都可以看到我错过了什么?
由于
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim FILE_NAME As String = "C:\Users\foo\test2.txt"
Dim i As Integer
Dim j As Integer
Dim aryText(9) As String
MessageBox.Show(j)
aryText(0) = "[" & TextBox1.Text & "]"
j = 0
MessageBox.Show(j)
If String.IsNullOrWhiteSpace(TextBox2.Text) Then
End
Else
aryText(1) = "*" & TextBox2.Text & "{label: " & "varchar, not null" & "}"
j = j + 1
MessageBox.Show(j)
End If
'If TextBox3.Text IsNot Nothing Then
If String.IsNullOrWhiteSpace(TextBox3.Text) Then
End
Else
aryText(2) = TextBox3.Text & " {label: " & "varchar, null" & "}"
j = j + 1
MessageBox.Show(j)
End If
If String.IsNullOrWhiteSpace(TextBox4.Text) Then
End
Else
aryText(3) = TextBox4.Text & " {label: " & "varchar, not null" & "}"
j = j + 1
MessageBox.Show(j)
End If
If String.IsNullOrWhiteSpace(TextBox5.Text) Then
End
Else
aryText(4) = TextBox5.Text & " {label: " & "varchar, not null" & "}"
j = j + 1
MessageBox.Show(j)
End If
If String.IsNullOrWhiteSpace(TextBox6.Text) Then
End
Else
aryText(5) = TextBox6.Text & " {label: " & "varchar, not null" & "}"
j = j + 1
MessageBox.Show(j)
End If
If String.IsNullOrWhiteSpace(TextBox7.Text) Then
End
Else
aryText(6) = TextBox7.Text & " {label: " & "varchar, not null" & "}"
j = j + 1
MessageBox.Show(j)
End If
If String.IsNullOrWhiteSpace(TextBox8.Text) Then
End
Else
aryText(7) = TextBox8.Text & " {label: " & "varchar, not null" & "}"
j = j + 1
MessageBox.Show(j)
End If
If String.IsNullOrWhiteSpace(TextBox9.Text) Then
End
Else
aryText(8) = TextBox9.Text & " {label: " & "varchar, not null" & "}"
j = j + 1
MessageBox.Show(j)
End If
Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)
MessageBox.Show(j)
For i = 0 To j
objWriter.WriteLine(aryText(j))
i = i + 1
Next
objWriter.Close()
MessageBox.Show("Text written to file")
End Sub
答案 0 :(得分:0)
为您的流编写器使用代码块可确保释放所有资源。
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim FILE_NAME As String = "C:\Users\foo\test2.txt"
Dim aryText(8) As String
Dim msg As String = "Please fill in all the boxes,"
If String.IsNullOrWhiteSpace(TextBox1.Text) Then
MessageBox.Show(msg)
Exit Sub
Else
aryText(0) = "[" & TextBox1.Text & "]"
End If
If String.IsNullOrWhiteSpace(TextBox2.Text) Then
MessageBox.Show(msg)
Exit Sub
Else
aryText(1) = "*" & TextBox2.Text & "{label: " & "varchar, not null" & "}"
End If
If String.IsNullOrWhiteSpace(TextBox3.Text) Then
MessageBox.Show(msg)
Exit Sub
Else
aryText(2) = TextBox3.Text & " {label: " & "varchar, null" & "}"
End If
If String.IsNullOrWhiteSpace(TextBox4.Text) Then
MessageBox.Show(msg)
Exit Sub
Else
aryText(3) = TextBox4.Text & " {label: " & "varchar, not null" & "}"
End If
If String.IsNullOrWhiteSpace(TextBox5.Text) Then
MessageBox.Show(msg)
Exit Sub
Else
aryText(4) = TextBox5.Text & " {label: " & "varchar, not null" & "}"
End If
If String.IsNullOrWhiteSpace(TextBox6.Text) Then
MessageBox.Show(msg)
Exit Sub
Else
aryText(5) = TextBox6.Text & " {label: " & "varchar, not null" & "}"
End If
If String.IsNullOrWhiteSpace(TextBox7.Text) Then
MessageBox.Show(msg)
Exit Sub
Else
aryText(6) = TextBox7.Text & " {label: " & "varchar, not null" & "}"
End If
If String.IsNullOrWhiteSpace(TextBox8.Text) Then
MessageBox.Show(msg)
Exit Sub
Else
aryText(7) = TextBox8.Text & " {label: " & "varchar, not null" & "}"
End If
If String.IsNullOrWhiteSpace(TextBox9.Text) Then
MessageBox.Show(msg)
Exit Sub
Else
aryText(8) = TextBox9.Text & " {label: " & "varchar, not null" & "}"
End If
Using objWriter As New System.IO.StreamWriter(FILE_NAME, True)
For Each s As String In aryText
objWriter.WriteLine(s)
Next
End Using
MessageBox.Show("Text written to file")
End Sub