|
-
Feb 27th, 2010, 10:13 AM
#1
Thread Starter
Member
[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
-
Feb 27th, 2010, 12:09 PM
#2
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
 Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Feb 27th, 2010, 12:28 PM
#3
Thread Starter
Member
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
-
Feb 27th, 2010, 12:45 PM
#4
Thread Starter
Member
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?
-
Feb 27th, 2010, 12:48 PM
#5
Thread Starter
Member
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
-
Feb 27th, 2010, 12:54 PM
#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
-
Feb 27th, 2010, 01:10 PM
#7
Thread Starter
Member
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
-
Feb 27th, 2010, 02:38 PM
#8
Thread Starter
Member
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.
-
Feb 27th, 2010, 02:39 PM
#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.
-
Feb 27th, 2010, 03:28 PM
#10
Junior Member
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...
-
Feb 27th, 2010, 03:36 PM
#11
Thread Starter
Member
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
-
Feb 27th, 2010, 03:38 PM
#12
Junior Member
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
-
Feb 27th, 2010, 03:39 PM
#13
Junior Member
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?
-
Feb 27th, 2010, 03:40 PM
#14
Thread Starter
Member
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?
-
Feb 27th, 2010, 03:41 PM
#15
Thread Starter
Member
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
-
Feb 27th, 2010, 03:43 PM
#16
Thread Starter
Member
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.
-
Feb 27th, 2010, 03:47 PM
#17
Junior Member
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
-
Feb 27th, 2010, 03:50 PM
#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.
-
Feb 27th, 2010, 03:51 PM
#19
Junior Member
Re: Text Editor Program-when I open a .txt my program loads with no text in the textb
lol :P how? using regedit?
-
Feb 27th, 2010, 03:54 PM
#20
Thread Starter
Member
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.
-
Feb 27th, 2010, 03:55 PM
#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:
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"
-
Feb 27th, 2010, 04:40 PM
#22
Thread Starter
Member
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.
-
Feb 27th, 2010, 05:32 PM
#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.
-
Feb 27th, 2010, 06:17 PM
#24
Thread Starter
Member
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.
-
Feb 27th, 2010, 07:29 PM
#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.
-
Feb 27th, 2010, 08:19 PM
#26
Thread Starter
Member
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.
-
Feb 27th, 2010, 09:11 PM
#27
Thread Starter
Member
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
-
Feb 27th, 2010, 10:16 PM
#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:
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.
-
Feb 27th, 2010, 11:38 PM
#29
Thread Starter
Member
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
-
Feb 28th, 2010, 12:32 AM
#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:
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.
-
Feb 28th, 2010, 01:24 AM
#31
Thread Starter
Member
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?
-
Feb 28th, 2010, 01:59 AM
#32
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|