How to diplay text from an external .txt file in a text box

djgray88
10-23-2004, 08:31 AM
can anyone tell me how to diplay text form a .txt file in a textbox in visual basic. I know how to open external file i just can't work out how to display the text in a textbox.

RoySchreiber
10-23-2004, 09:03 AM
Dim Line1 as String
Open TextFileLocation for input as #1
Text1.text = ""
Do Until EOF(1)
If Text1.text <> "" then Text1.text = Text1.text & vbNewLine
Line Input #1, Line1
Text1.text = Text1.text & Line1
Loop
Close #1

Diurnal
10-23-2004, 09:23 AM
You might want to look at the File I/O (http://www.xtremevbtalk.com/showthread.php?threadid=123814) tutorial for some more information on this. You can also use the Input$ and LOF functions to get the text in one operation:

Open "C:\TestFile.txt" For Input As #1
'Calculate all the bytes in the file and return these bytes.
Text1.Text = Input$(LOF(#1), 1)
Close #1

RoySchreiber
10-23-2004, 09:41 AM
hmm... ya, that too. didn't use non binary files for a long time so i forgot that...

northwolves
10-23-2004, 10:09 AM
Dim TextLine as String
Open "d:\test.txt" For Binary As #1
TextLine = Space(LOF(1))
Get #1, , TextLine
Close #1
Text1.Text=Textline

Rashka
10-23-2004, 02:57 PM
My suggestion, on top of what everyone else is saying is to not open the File as #1. Rather, open the file as FreeFile. A simple search of freefile should tell you why.

RoySchreiber
10-23-2004, 03:05 PM
oh, come on... if you know your program you shouldn't need to use that.

MikeJ
10-23-2004, 03:38 PM
True, but then again, it's always good practice to do what's right the first time. It's like the End/Unload Me discussion, while End works, it's not the best, nor does it make the most sense to have to learn/unlearn it.

It's better to get it right the first time.

RoySchreiber
10-23-2004, 03:56 PM
"good practice" yeah, that's the constant answer. you know what, i'm sick and tierd of the "good practice" stuff. a good programer knows when to use each command and when each command fits/doesn't fit his code.

reboot
10-23-2004, 04:06 PM
Only if you make silly little pretend programs. Most of us don't fall into this category. Not using FreeFile when doing File I/O leaves you wide open to lots of nasty little bugs. I cannot understand for the life of me why there are so many people lately desperately trying to recommend the wrong way to do things. Why exactly does it bother you so bad that we try to teach the correct way to do things?

Rashka
10-23-2004, 11:32 PM
Thank you MikeJ and Reboot. You two put my thoughts into words beautifully! :D I am not the greatest with words, and sometimes I can't express my reasoning just right and it comes out all wrong, but you two did wonderful. Quite simply put, Thanks.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum