Results 1 to 32 of 32

Thread: [RESOLVED] Text Editor Program-when I open a .txt my program loads with no text in the textbox

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    44

    Resolved [RESOLVED] Text Editor Program-when I open a .txt my program loads with no text in the textbox

    I believe I need the code for Load Event. I have tried many ideas. I have searched the threads here and also google of course. No luck as of yet.

    Better description: I have a file named hello.txt. The contents of this text file are "hello". I use my program as the default for .txt files. I dbl click hello.txt. My program loads and the contents are blank and it should have "hello" in it.



    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub



    Thank you for the assistance

  2. #2
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Text Editor Program-when I open a .txt my program loads with no text in the textb

    It doesn't work that way. Just because your app is the default for opening those files, doesn't mean it will read it. You have to make the functionality.

    Look into StreamReaders
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    44

    Re: Text Editor Program-when I open a .txt my program loads with no text in the textb

    I currently am using streamreaders on my program

    I just don't know how to point the streamreader to my opened file. I assume I will also have to use a streamwriter or maybe I will need to put in
    textbox1.text = mystreamreader.readtoend()

    This is what I have put in so far.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim Open As New OpenFileDialog()
    Dim sr As StreamReader
    Dim sw As StreamWriter
    sr = System.IO.File.OpenText(Open.FileName)
    textBox1.Text = sr.ReadToEnd()
    sr.Close()
    End Sub

  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    44

    Re: Text Editor Program-when I open a .txt my program loads with no text in the textb

    Should I be using File.ReadAllText? If so how would I use it?

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    44

    Re: Text Editor Program-when I open a .txt my program loads with no text in the textb

    Maybe? The below doesn't work I just want to know if i am getting close.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    textBox1.Text = System.IO.File.WriteAllText()
    End Sub

  6. #6

    Re: Text Editor Program-when I open a .txt my program loads with no text in the textb

    That's not...that's the opposite. First off, here's what I'd do in Psuedo/vb code:
    Code:
    Dim o As New OpenFileDialog
    If o.ShowDialog = Ok Then
    Dim sr as New StreamReader(o.Filepath)
    textbox1.text = sr.ReadToEnd()
    sr.close()
    End If

  7. #7

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    44

    Re: Text Editor Program-when I open a .txt my program loads with no text in the textb

    I put this in and it says Ok is not declared. Do I need to use Imports to get that to work.

    The second thing it said- Dim sr As New StreamReader(o.Filepath) 'Filepath' is not a member of 'System.Windows.Forms.OpenFileDialog'.
    Should I use o.Filename?

    Dim o As New OpenFileDialog
    If o.ShowDialog = Ok Then
    Dim sr As New StreamReader(o.Filepath)
    textBox1.Text = sr.ReadToEnd()
    sr.Close()
    End If

  8. #8

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    44

    Re: Text Editor Program-when I open a .txt my program loads with no text in the textb

    I think I understand what you meant by o.Filepath.

    Since I never know what the filepath will be I am not sure what to put after o.

  9. #9

    Re: Text Editor Program-when I open a .txt my program loads with no text in the textb

    I think the proper term is Filename, not Filepath.

  10. #10
    Junior Member
    Join Date
    Jan 2010
    Posts
    16

    Re: Text Editor Program-when I open a .txt my program loads with no text in the textb

    Here is the answer!
    make sure the streamtype is PlainText
    vb Code:
    1. RichTextBox1.LoadFile("C:\PATH", RichTextBoxStreamType.PlainText)

    And make a RichTextBox instead of a TextBox

    To make UNDO button simply do:
    vb Code:
    1. RichTextBox1.Undo

    And so on...

  11. #11

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    44

    Re: Text Editor Program-when I open a .txt my program loads with no text in the textb

    I think I am getting closer. Only problem is it is popping up the open file dialog box. I don't want to do that. I want to dbl click on a text file and it to just load the file i clicked on in my program of course.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim o As New OpenFileDialog
    o.ShowDialog(Me)
    If Windows.Forms.DialogResult.OK Then
    Dim sr As New StreamReader(o.FileName)
    textBox1.Text = sr.ReadToEnd()
    Me.Text = String.Format("{0} - My MC Notepad", IO.Path.GetFileNameWithoutExtension(o.FileName))
    End If
    End Sub

  12. #12
    Junior Member
    Join Date
    Jan 2010
    Posts
    16

    Re: Text Editor Program-when I open a .txt my program loads with no text in the textb

    You should use Button1.Click instead of Mybase.Load then create a button Called Open... or something :P

  13. #13
    Junior Member
    Join Date
    Jan 2010
    Posts
    16

    Re: Text Editor Program-when I open a .txt my program loads with no text in the textb

    Why do you use TextBox? Why not RichTextBox?

  14. #14

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    44

    Re: Text Editor Program-when I open a .txt my program loads with no text in the textb

    Wouldn't I need to use the streamReader to read the file I am opening and then use the streamWriter to write in my textbox1 the file It has read?

  15. #15

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    44

    Re: Text Editor Program-when I open a .txt my program loads with no text in the textb

    No reason Netorb. I can switch that. That is easy. This program is not terribly long, yet

  16. #16

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    44

    Re: Text Editor Program-when I open a .txt my program loads with no text in the textb

    I do have an open butotn within the program Netorb. What I am trying to do is dbl click on a .txt file before my program is loaded. Once I click on the .txt it opens my program with the text from the .txt file I dbl clicked on.

  17. #17
    Junior Member
    Join Date
    Jan 2010
    Posts
    16

    Re: Text Editor Program-when I open a .txt my program loads with no text in the textb

    ohhh, lol thought you where simply making a text editor :P

    There might be a way to make the program use the text file location then load it into a richtextbox using the code i wrote

  18. #18

    Re: Text Editor Program-when I open a .txt my program loads with no text in the textb

    You'd have to use arguments to pass in the file.

    So you would handle most of the stuff in Application Events. An example argument would be:
    C:\path.exe "<filepath>". You just have to tell Windows Explorer that's how you want to open the file.

  19. #19
    Junior Member
    Join Date
    Jan 2010
    Posts
    16

    Re: Text Editor Program-when I open a .txt my program loads with no text in the textb

    lol :P how? using regedit?

  20. #20

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    44

    Re: Text Editor Program-when I open a .txt my program loads with no text in the textb

    I was just trying to get this to operate like windows notepad. you know when you open a .txt file it loads notepad (if that is the default program). When it loads the notepad it has the text within that file loaded.

  21. #21

    Re: Text Editor Program-when I open a .txt my program loads with no text in the textb

    Probably. I haven't done much research. You can check it out yourself. But here's how you would parse such an argument example:

    In ApplicationEvents.vb, put this:
    vb.net Code:
    1. Namespace My
    2.     Partial Friend Class MyApplication
    3.         Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
    4.             If e.CommandLine.Count > 0 Then Form1.File = e.CommandLine(0).ToString
    5.         End Sub
    6.     End Class
    7. End Namespace

    In Form1, put this:
    vb.net Code:
    1. Public Class Form1
    2.     Private FilePath As String = String.Empty
    3.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    4.         If FilePath <> String.Empty Then
    5.             'Load the file
    6.         End If
    7.     End Sub
    8.     Public Property File() As String
    9.         Get
    10.             Return FilePath
    11.         End Get
    12.         Set(ByVal value As String)
    13.             FilePath = value
    14.         End Set
    15.     End Property
    16. End Class

    EDIT: This is assuming your argument is like so:
    Code:
    C:\MyNotePad.exe "C:\example\thisisatextfile.text"

  22. #22

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    44

    Re: Text Editor Program-when I open a .txt my program loads with no text in the textb

    I received this error sir:

    An error occurred creating the form. See Exception.InnerException for details. The error is: Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "MyMCNotepad.Form1.resources" was correctly embedded or linked into assembly "MyMCNotepad" at compile time, or that all the satellite assemblies required are loadable and fully signed.

  23. #23

    Re: Text Editor Program-when I open a .txt my program loads with no text in the textb

    I probably used a bad example. I didn't test it. Try creating a global variable and setting it to the value, then do a check. If you need me to show you what I mean, tell me.

  24. #24

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    44

    Re: Text Editor Program-when I open a .txt my program loads with no text in the textb

    Does that mean a public class? Sorry I am very new. I understand it's meaning. A variable that can be seen throughout the whole program. btw, I saw your thread, the hangman on. I downloaded your little hangman game. That was pretty cool.

  25. #25

    Re: Text Editor Program-when I open a .txt my program loads with no text in the textb

    Thanks for the comment on the hangman game.
    As for placing the variable, place it in a module, declaring the variable public.

  26. #26

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    44

    Re: Text Editor Program-when I open a .txt my program loads with no text in the textb

    Great, I will try that. I will post later with an update.

  27. #27

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    44

    Re: Text Editor Program-when I open a .txt my program loads with no text in the textb

    Hello again.

    I have created a module. I have attempted many different things. I am way lost. This is the final big issue my program has. Once I resolve this there will just be minor tweaks to get it perfect. I guess all good projects are a work in progress though.

    Anyway, I have added a module to this program. I am not sure what to put in there and how to tie it to my Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub.

    I appreciate your help Formlesstree4

  28. #28

    Re: Text Editor Program-when I open a .txt my program loads with no text in the textb

    Let's say you have a variable named m_FileToLoad in your module, and it's a string. In your ApplicationSettings file, I had you, in the Startup Sub, check for an argument. Let's make it assign to that variable.

    vb.net Code:
    1. Namespace My
    2.     Partial Friend Class MyApplication
    3.         Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
    4.             If e.CommandLine.Count > 0 Then m_FileToLoad = e.CommandLine(0).ToString
    5.         End Sub
    6.     End Class
    7. End Namespace

    Basically, I see if we have any Commands that are at the end of the exe, and if so, take the first one (which is generally the file) and assign it to the m_FileToload variable.

    Now, in your Form1_Load Sub, the code should be as follows:
    vb.net Code:
    1. If m_FileToLoad <> String.Empty Then
    2.             'Load the file
    3.             'once loaded, reset the variable to nothing.
    4.             m_FileToLoad = String.Empty
    5.         End If

    This is a rough idea of what I'd suggest you do. You can try it out to see if it works, and I think it should.

    If you have any questions, feel free to ask.

  29. #29

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    44

    Re: Text Editor Program-when I open a .txt my program loads with no text in the textb

    I have an Application Events:

    Namespace My
    Partial Friend Class MyApplication
    Dim m_FileToLoad As String
    Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
    If e.CommandLine.Count > 0 Then m_FileToLoad = e.CommandLine(0).ToString
    End Sub
    End Class
    End Namespace


    I had to put Dim m_FileToLoad As String, if I didn't it said m_FileToLoad not declared.

    My Form1Load is:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim m_FileToLoad As String
    If m_FileToLoad <> String.Empty Then
    'Load the file
    'once loaded, reset the variable to nothing.
    m_FileToLoad = String.Empty
    End If
    End Sub

    Again I had to put Dim m_FileToLoad As String it had m_FileToLoad as undeclared.

    I did create a module. I have never done that I put in:

    Module Module1
    Dim m_fileToLoad As String
    End Module


    The other question I had:

    Do I need this following code as you suggested in my Public Class Form1

    Private FilePath As String = String.Empty
    Public Property File() As String
    Get
    Return FilePath
    End Get
    Set(ByVal value As String)
    FilePath = value
    End Set
    End Property


    What are your thoughts formlesstree4?
    Aside from the obvious, "OMG this guy is green" hehe

  30. #30

    Re: Text Editor Program-when I open a .txt my program loads with no text in the textb

    Don't keep that property. Change your module code to this:
    vb.net Code:
    1. Public Module Module1
    2.     Public m_FileToLoad As String = String.Empty
    3. End Module

    Then remove the line:
    vb.net Code:
    1. Dim m_FileToLoad As String
    from your Form1 code. It should work now.

  31. #31

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    44

    Re: Text Editor Program-when I open a .txt my program loads with no text in the textb

    Ok I have everything in place.I did test it. My file hi.txt has "hello" written within it. My program loaded when I dbl clicked hi.txt and no text was in there.

    Any thoughts?

  32. #32

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    44

    Re: Text Editor Program-when I open a .txt my program loads with no text in the textb

    I just found an easier solution.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim myStreamReader As IO.StreamReader
    Dim cla As String() = Environment.GetCommandLineArgs()
    If cla.Length > 1 Then
    myStreamReader = System.IO.File.OpenText(cla(1))
    rtb.Text = myStreamReader.ReadToEnd
    myStreamReader.Dispose()
    End If
    End Sub


    This was all I needed. Just wanted to share this with you. Thank you for your time and effort formlesstree4. I am going to mark this as resolved.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width