J
jzheng22
Hi all,
I am new to VB .NET. I am currently wring a simple GUI, a TextBox
(multilined),
a button and an OpenFileDialog. When user click the button, the
OpenFileDialog
fired and after user select a file, the content of the text file would
be loaded
(appended) into the TextBox.
Note: I am not using RichTextBox.Loadfile() method.
My code is working, however, when i tested it by loading large .exe
file into the
textbox, the program freezes. I wondered if anyone could help me with
these:
1) check if the selected file is a text file
2) if user choose a very large file, no matter what format it is, the
code should
report an exception and keep running.
Many thanks.
P.S. My current error handlers using "Try" are not working, code
attached below:
Try
If OpenFileDialog1.ShowDialog() =
System.Windows.Forms.DialogResult.OK _
And (OpenFileDialog1.FileName.Length) > 0 Then
Dim objReader As New StreamReader(OpenFileDialog1.FileName)
Dim sLine As String = ""
Do
sLine = objReader.ReadLine()
If Not sLine Is Nothing Then
TextBox1.Text = TextBox1.Text & "; " &
sLine
End If
End If
Loop Until sLine Is Nothing
objReader.Close()
End If
Catch ex As Exception
MessageBox.Show("Error when open files." & vbCrLf &
ex.Message)
TextBox1.Clear()
TextBox1.Focus()
Exit Sub
End Try
I am new to VB .NET. I am currently wring a simple GUI, a TextBox
(multilined),
a button and an OpenFileDialog. When user click the button, the
OpenFileDialog
fired and after user select a file, the content of the text file would
be loaded
(appended) into the TextBox.
Note: I am not using RichTextBox.Loadfile() method.
My code is working, however, when i tested it by loading large .exe
file into the
textbox, the program freezes. I wondered if anyone could help me with
these:
1) check if the selected file is a text file
2) if user choose a very large file, no matter what format it is, the
code should
report an exception and keep running.
Many thanks.
P.S. My current error handlers using "Try" are not working, code
attached below:
Try
If OpenFileDialog1.ShowDialog() =
System.Windows.Forms.DialogResult.OK _
And (OpenFileDialog1.FileName.Length) > 0 Then
Dim objReader As New StreamReader(OpenFileDialog1.FileName)
Dim sLine As String = ""
Do
sLine = objReader.ReadLine()
If Not sLine Is Nothing Then
TextBox1.Text = TextBox1.Text & "; " &
sLine
End If
End If
Loop Until sLine Is Nothing
objReader.Close()
End If
Catch ex As Exception
MessageBox.Show("Error when open files." & vbCrLf &
ex.Message)
TextBox1.Clear()
TextBox1.Focus()
Exit Sub
End Try