My VB is cursed! Simple text file loading!

VB won't even perform simple things D:

Page 1 of 1

10 Replies - 2812 Views - Last Post: 26 December 2010 - 08:08 AM Rate Topic: -----

#1 ssharma286  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 93
  • Joined: 04-December 10

My VB is cursed! Simple text file loading!

Posted 25 December 2010 - 04:58 PM

Ok I am seriously getting a vibe that my VB is cursed. All I want to do is load a textfile and have the text appear in TextBox1 But noooo. I have looked at 3 different tutorials and I have tried copying their code word to word but still! gah please tell me where I am going wrong

 Public Class Form1

    Private Sub ImportToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImportToolStripMenuItem.Click
        Dim OpenFile As New OpenFileDialog
        OpenFile.FileName = ""
        OpenFile.Filter = "Text Files (*.txt)|*.txt"
        OpenFile.Title = "Open"
        OpenFile.ShowDialog()

        Dim daText As New System.IO.StreamReader(OpenFile.FileName­)
        TextBox1.Text = daText.Readtoend
        daText.Close()

    End Sub
End Class
)


And it gives me the following errors:
Error	1	Character is not valid.	C:\Users\Saurabh Sharma\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb	10	67	WindowsApplication1
Error	2	'daText' is not declared. It may be inaccessible due to its protection level.	C:\Users\Saurabh Sharma\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb	11	25	WindowsApplication1
Error	3	'daText' is not declared. It may be inaccessible due to its protection level.	C:\Users\Saurabh Sharma\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb	12	9	WindowsApplication1


This post has been edited by ssharma286: 25 December 2010 - 04:59 PM


Is This A Good Question/Topic? 0
  • +

Replies To: My VB is cursed! Simple text file loading!

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10488
  • View blog
  • Posts: 38,865
  • Joined: 27-December 08

Re: My VB is cursed! Simple text file loading!

Posted 25 December 2010 - 05:00 PM

So why are you posting this in C++? Moved to VB.NET.
Was This Post Helpful? 0
  • +
  • -

#3 Zekorov  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 226
  • Joined: 16-May 10

Re: My VB is cursed! Simple text file loading!

Posted 25 December 2010 - 05:05 PM

hmmm idk if dim daText is possibly not reading because its protection level, have you tried putting "Public Sub" instead of "Private Sub"?

This post has been edited by Zekorov: 25 December 2010 - 05:06 PM

Was This Post Helpful? 0
  • +
  • -

#4 ssharma286  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 93
  • Joined: 04-December 10

Re: My VB is cursed! Simple text file loading!

Posted 25 December 2010 - 05:08 PM

View PostZekorov, on 25 December 2010 - 04:05 PM, said:

hmmm idk if dim daText is possibly not reading because its protection level, have you tried putting "Public Sub" instead of "Private Sub"?


tried didn't work D:
Was This Post Helpful? 0
  • +
  • -

#5 Zekorov  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 226
  • Joined: 16-May 10

Re: My VB is cursed! Simple text file loading!

Posted 25 December 2010 - 05:13 PM

well dang.... :( sorry bro. and you copied that right out of a tutorial?

Oh by the way, after
     End Sub
End Class
)


there should be no ) sign.
it should just be
     End Sub
End Class


This post has been edited by Zekorov: 25 December 2010 - 05:16 PM

Was This Post Helpful? 0
  • +
  • -

#6 ssharma286  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 93
  • Joined: 04-December 10

Re: My VB is cursed! Simple text file loading!

Posted 25 December 2010 - 05:14 PM

Yes except I changed the text boxes to my own D: i don't know what is wrong. This isn't the first time it has done this. I have quitted on so many applications because of this stupid error gah
Was This Post Helpful? 0
  • +
  • -

#7 Zekorov  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 226
  • Joined: 16-May 10

Re: My VB is cursed! Simple text file loading!

Posted 25 December 2010 - 05:18 PM

View Postssharma286, on 25 December 2010 - 07:14 PM, said:

Yes except I changed the text boxes to my own D: i don't know what is wrong. This isn't the first time it has done this. I have quitted on so many applications because of this stupid error gah

Hmm... maybe just try using their text boxes to see if it works first.
Was This Post Helpful? 0
  • +
  • -

#8 ssharma286  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 93
  • Joined: 04-December 10

Re: My VB is cursed! Simple text file loading!

Posted 25 December 2010 - 05:25 PM

View PostZekorov, on 25 December 2010 - 04:18 PM, said:

View Postssharma286, on 25 December 2010 - 07:14 PM, said:

Yes except I changed the text boxes to my own D: i don't know what is wrong. This isn't the first time it has done this. I have quitted on so many applications because of this stupid error gah

Hmm... maybe just try using their text boxes to see if it works first.

OMG it worked! But does this mean I have to stick to their style? I wanted to load a text into a textbox using a dialogbox...but now i have to copy and paste the filepath into a textbox for it to load... D: anyway to make it work using a dialog box??
Was This Post Helpful? 0
  • +
  • -

#9 Zekorov  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 226
  • Joined: 16-May 10

Re: My VB is cursed! Simple text file loading!

Posted 25 December 2010 - 05:34 PM

Glad it worked. and I'm not sure. I haven't done very much with VB.Net. :\ But i recommend that you search around on here for tutorials using Dialog Boxes until you understand them inside and out, and then try out your idea. if you can't find things on here specifically about Dialog Boxes, try google. :)
Although, if you are loading a text file, it seems to make sense that you would use a Text box and not a Dialog Box. just saying :)
Was This Post Helpful? 0
  • +
  • -

#10 biggerB  Icon User is offline

  • If at first you donít succeed, call it version 1.0
  • member icon

Reputation: 80
  • View blog
  • Posts: 760
  • Joined: 11-June 10

Re: My VB is cursed! Simple text file loading!

Posted 26 December 2010 - 02:48 AM

if you just want to read all the text in the file there is an easier way to do it than using a streamReader...

Try This

Dim SomeString As String = System.IO.File.ReadAllText(FilePath)

the System.IO.File.ReadAllText Reads all text in a file and then closes it.. and returns that text as a simple string, Then you could put that string in the Textbox1.Text Property to show it..
Was This Post Helpful? 0
  • +
  • -

#11 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 109
  • View blog
  • Posts: 603
  • Joined: 01-October 08

Re: My VB is cursed! Simple text file loading!

Posted 26 December 2010 - 08:08 AM

This worked for me

        Dim OpenFile As New OpenFileDialog
        OpenFile.FileName = ""
        OpenFile.Filter = "Text Files (*.txt)|*.txt"
        OpenFile.Title = "Open"

        Dim dr As DialogResult = OpenFile.ShowDialog()

        If dr = vbOK Then
            Dim daText As System.IO.StreamReader
            daText = New IO.StreamReader(OpenFile.FileName)
            'a RichTextBox might be a better choice
            TextBox1.Text = daText.ReadToEnd
        End If


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1