-
[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
-
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
-
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
-
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?
-
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
-
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
-
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
-
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.
-
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.
-
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:
RichTextBox1.LoadFile("C:\PATH", RichTextBoxStreamType.PlainText)
And make a RichTextBox instead of a TextBox ;)
To make UNDO button simply do:
And so on...
-
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
-
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
-
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?
-
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?
-
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
-
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.
-
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 :)
-
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.
-
Re: Text Editor Program-when I open a .txt my program loads with no text in the textb
lol :P how? using regedit?
-
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.
-
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:
Namespace My
Partial Friend Class MyApplication
Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
If e.CommandLine.Count > 0 Then Form1.File = e.CommandLine(0).ToString
End Sub
End Class
End Namespace
In Form1, put this:
vb.net Code:
Public Class Form1
Private FilePath As String = String.Empty
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If FilePath <> String.Empty Then
'Load the file
End If
End Sub
Public Property File() As String
Get
Return FilePath
End Get
Set(ByVal value As String)
FilePath = value
End Set
End Property
End Class
EDIT: This is assuming your argument is like so:
Code:
C:\MyNotePad.exe "C:\example\thisisatextfile.text"
-
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.
-
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.
-
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.
-
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.
-
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.
-
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
-
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:
Namespace My
Partial Friend Class MyApplication
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
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:
If m_FileToLoad <> String.Empty Then
'Load the file
'once loaded, reset the variable to nothing.
m_FileToLoad = String.Empty
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.
-
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
-
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:
Public Module Module1
Public m_FileToLoad As String = String.Empty
End Module
Then remove the line:
vb.net Code:
Dim m_FileToLoad As String
from your Form1 code. It should work now.
-
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?
-
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.