Loading ALL of the text

pokeglobe

New member
Joined
Aug 1, 2009
Messages
3
Programming Experience
1-3
Hello, I am new here and just started visual basic 2008.

Seems pretty easy. :D
But I am at the point where I have to ask for help.

OK Here is my problem.

I want to load a BIG file to edit the text. It is 24MB So whenever I load it in the rich textbox it shows : �7@
:/ Regular text files load fine. Here is the code:
VB.NET:
CommonDialog1Open.Filter = ("EXE [*.exe*]|*.exe|All Files [*.*]|*.*")
        CommonDialog1Open.ShowDialog()
        rt1.Text = (My.Computer.FileSystem.ReadAllText(CommonDialog1Open.FileName))

I know I am trying to load .exe! It will load if I save it through the program but will only show �7@

Please help thanks alot :)
If I posted this in the wrong section could a moderator move it XD
 
First of all, loading a large text file usually will freeze the program.
May I suggest, to stop freezing the program, do this:
Stick this anywhere in your code, outside of a sub or function.
VB.NET:
Sub ReadFile()
 rt1.Text = (My.Computer.FileSystem.ReadAllText(CommonDialog1Open.FileName))
End Sub
Then, whatever button you use to have it read the text file, put this in instead of the rt1.Text = (blabla)...
VB.NET:
rt1.Text = "Loading file..."
        Dim t As System.Threading.Thread
        t = New System.Threading.Thread(AddressOf Me.ReadFile)
        t.Start()
What this will do is load the file into the text box in a new process thread, and while it is loading the file, it will say in the text box 'Loading file...'. This will stop the application from freezing.

Now, you want to load a compiled executable file into a text box... very odd. May I ask, why? Anyways, have you tried loading it in to a normal text box instead of a rich text box? Probably wont make a difference, but it is worth a try.
 
pokeglobe, exe files are binary files, not text files. You have to handle these as byte values. Binary file editors use a pane to display the byte values, often as hex string values (usually called a 'Hex editor'), and a pane that display character representation for byte values that fall within this range, but most byte values in a binary file has no logical string representation.

Untamed, your code sample result in InvalidOperationException: Cross-thread operation not valid. If you need to do multithreading you should read How to: Make Thread-Safe Calls to Windows Forms Controls Also look into BackgroundWorker which makes this even simpler.
 
Untamed, your code sample result in InvalidOperationException: Cross-thread operation not valid. If you need to do multithreading you should read How to: Make Thread-Safe Calls to Windows Forms Controls Also look into BackgroundWorker which makes this even simpler.

I don't know about that, I've used code like this:
VB.NET:
        Dim t As System.Threading.Thread
        t = New System.Threading.Thread(AddressOf Me.ReadFile)
        t.Start()
Except changed ReadFile to whatever sub I wanted to load in a new thread.

This code works perfectly for me, and is only 3 lines. Not sure how to make it a whole lot simpler, or why you'd need/want to. Also no clue why you got the error... once again... I get 0 errors/warnings, and this works perfectly for me.
 
I don't know about that, I've used code like this:
VB.NET:
        Dim t As System.Threading.Thread
        t = New System.Threading.Thread(AddressOf Me.ReadFile)
        t.Start()
Except changed ReadFile to whatever sub I wanted to load in a new thread.

This code works perfectly for me, and is only 3 lines. Not sure how to make it a whole lot simpler, or why you'd need/want to. Also no clue why you got the error... once again... I get 0 errors/warnings, and this works perfectly for me.
Have you considered using a BackgroundWorker? It has a report progress event that you can use to update a control in the UI thread from the BG thread.
 
This code works perfectly for me, and is only 3 lines. Not sure how to make it a whole lot simpler, or why you'd need/want to. Also no clue why you got the error... once again... I get 0 errors/warnings, and this works perfectly for me.
The problem is doing "rt1.Text =" from a thread that did not create the rt1 control, this is an invalid operation.
 
The rt1 was just a renamed rich text box.

Also I got this error when I tried it.
Cross-thread operation not valid: Control 'rt1' accessed from a thread other than the thread it was created on.

Maybe you are right... What should I change it to?

Also.. I am trying to make this edit strings. I know a hex editor does this but I have an idea with it.
 
Last edited:
Back
Top