Create a new Windows Application Add a New form Add a textbox to the form; use the default name for the textbox. Make sure to change the Multiline Property of the textbox to True. After changing the Multiline Property of the textbox to True, make the textbox larger in order to fill the form, but leave enough room at the top to add a button. Add a button to the form above the textbox. Rename the button to pbSelectFile. Add an OpenFileDialog to the form. Rename the OpenFileDialog fDiag1. Add this “Text Files|*.txt” to the Filter Property of fDiag1.Copy and paste the code below
private void pbSelectFile_Click(object sender, EventArgs e)
{
if (fDiag1.ShowDialog() != DialogResult.Cancel)
{
//Set the Text to an empty string in order to reset the text if you
//have previously opened a file.
textBox1.Text = "";
String sLine = "";
try
{
//Pass the file you selected with the OpenFileDialog control to
//the StreamReader Constructor.
StreamReader FileStream = new StreamReader(fDiag1.FileName);
//Set the string variable to the first line of text in the text file.
sLine = FileStream.ReadLine();
while (sLine != null)
{
//Add the line of text from the text file to the TextBox,
//and add a carriage return (\r) and line feed (\n) to the end of the
//line of text.
textBox1.Text = textBox1.Text + sLine + "\r\n";
//Read the next line of the text file.
sLine = FileStream.ReadLine();
}
//Close the selected text file.
FileStream.Close();
}
catch (Exception err)
{
//Display any errors in a Message Box.
System.Windows.Forms.MessageBox.Show("Error: " + err.Message, "Program Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
_____________________
Make sure to add pbSelectFile_Click to the Click Event of the pbSelectFile button.
_____________________
Visual Basic Equivalent
Private Sub pbSelectFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbSelectFile.Click
Dim FileStream As System.IO.StreamReader
If fDiag1.ShowDialog() <> Windows.Forms.DialogResult.Cancel Then
'Set the Text to an empty string in order to reset the text if you
'have previously opened a file.
textBox1.Text = ""
Dim sLine As String = ""
Try
'Pass the file you selected with the OpenFileDialog control to
'the StreamReader Constructor.
FileStream = New System.IO.StreamReader(fDiag1.FileName)
'Set the string variable to the first line of text in the text file.
sLine = FileStream.ReadLine
While sLine <> Nothing
'Add the line of text from the text file to the TextBox,
'and add a carriage return (\r) and line feed (\n) to the end of the
'line of text.
textBox1.Text = sLine
'Read the next line of the text file.
sLine = FileStream.ReadLine()
End While
'Close the selected text file.
FileStream.Close()
Catch Err As Exception
'Display any errors in a Message Box.
System.Windows.Forms.MessageBox.Show("Error: " + Err.Message, "Program Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End Sub
End Sub
This free website was made using Yola.
No HTML skills required. Build your website in minutes.
Go to www.yola.com and sign up today!
Make a free website with Yola