VB File opening issue

sparky30161

Member
Joined
Nov 8, 2006
Messages
8
Programming Experience
Beginner
Hi,

hope anyone can help.

i am a complete novice.

i have a form with a text box and 2 buttons

i need a user to enter the filename in the text box and press the button. this will then open the file. the file will be a simple .txt file. the other button will be to close the file.

i am not sure if i can have another text box on the form and for the contents of the file to open into this.


any help is greatly appreciated. really stuck.

look forward to any replies

cheers

Sparky
 
i bascially want a text box. a user enters the name of a text file into the box and press the button. then either this file opens up or the contents open into another text box?
 
Yep, check the forum post I referenced you to, it will link you to here:

http://msdn2.microsoft.com/en-gb/library/db5x7c0d.aspx

Which is an article about how to read from input files.

That article will give you much more, but the general solution is that you will be:

VB.NET:
Using sr [COLOR=blue]As[/COLOR] StreamReader = [COLOR=blue]New[/COLOR] StreamReader([COLOR=maroon]"TestFile.txt"[/COLOR])
[COLOR=blue]  Dim[/COLOR] line [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR]
    [COLOR=blue]Do[/COLOR]
      line = sr.ReadLine()
      Me.Textbox1.Text = Me.Textbox1.Text & line
[COLOR=blue]    Loop[/COLOR] [COLOR=blue]Until[/COLOR] line [COLOR=blue]Is[/COLOR] [COLOR=blue]Nothing[/COLOR]
[COLOR=blue]
[/COLOR]

edited in a hurry, hope that works. If not, get the original code from the MSDN link.
 
Make sure the textbox you are putting the file contents into has mutlines enabled, or you wont see too much past line1 :)
 
really appreciate the help so far folks.

it good to know i have resources like this.

right,

i now have the code entered. i changed one of the text boxes to
a richtextbox and so now i can see the contents of the whole file.

so if i run the program i just click on the button to find the file. but my problem is that i need to have the user physicially an extension where the file will be. so they will need to type F:\example.txt into text box one and then click on the retrieve button.

also if the file is not there i would like to display an error to the user.

again thanks for all the help folks.

no idea how much i appreciate it

Cheers

Sparky
 
Is there a reason your having the user type the file path and name, instead of using a more standard file selection method like the OpenFileDialog?

Here's the MSDN article on how to use the OFD, let us know if there is a reason you can't use this (WAY better than a user typed name, and less annoying for the user), or if you have trouble.

http://msdn2.microsoft.com/en-us/library/system.windows.forms.openfiledialog.aspx
 
Dim sr As New System.IO.StreamReader("YourFileNameHere")
Textbox1.Text = sr.ReadToEnd
sr.Close()


simple as that
 
ok,

i have decided to take the OpenFileDialog. when i run this and click on the button it opens the f drive. i need to be able to click on a certain text file in this directory and have it displayed in the rich text box.

any ideas

thanks again for any help folks

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myStream As IO.Stream = Nothing
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory =
"f:\"
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory =
True
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
myStream = openFileDialog1.OpenFile()
If (myStream IsNot Nothing) Then

End If

Finally
If (myStream IsNot Nothing) Then
myStream.Close()
End If
End Try
End If
End Sub
End
Class
 
VB.NET:
Public Class Form1
  PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim ofd As New OpenFileDialog()
    With ofd
      .InitialDirectory = "f:\"
      .Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
      .DefaultExt = "txt"
      .FilterIndex = 2
      .RestoreDirectory = True
    End With
    If ofd.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
      Try
        Dim sr As New StreamReader(ofd.FileName)
        RichTextBox1.Text = sr.ReadToEnd()
        sr.Close()
      Catch ex As Exception
         MessageBox.Show("Error opening file" & ControlChars.NewLine & ex.Message, "File Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
      End Try
    End If
  End Sub
End Class
 
Alternatives

Two alternatives to using the StreamReader:

VB.NET:
RichTextBox1.LoadFile(ofd.FileName, RichTextBoxStreamType.PlainText)

RichTextBox1.Text = My.Computer.FileSystem.ReadAllText(ofd.FileName)
Note that the second example as well as many other useful examples can be written for you in Visual Studio by using the Insert Snippet functionality. To use Code Snippets: right click in the code window and click Insert Snippet...; you'll be presented with categories of snippets and after double clicking one you'll see the snippets in that category.
 
Back
Top