Newbie TextBox question 
Author Message
 Newbie TextBox question

Hi All,
Running VB 3
I have a text file on disk.
I am having a tough time getting my textbox to display multiple lines of
text. I have set the TextBox property multiline to true but all I get with
the following code is the first line from my text file. I have tried lots of
other approaches and need a shove in the right direction.

Sub Command1_Click ()
 Dim myfile, mytext
myfile = "C:\mydatfil.txt"
Open myfile For Input As #1
Input #1, mytext
Text1 = mytext
End Sub

Thanks in advance,
mike



Mon, 18 Apr 2005 16:35:08 GMT  
 Newbie TextBox question
Hi Mike - here's the problem:

You're not reading the whole file - just the first line

You need to loop through the file adding each new line to the end of your
textbox until you hit the end of the file

Here's one way:

Sub Command1_Click ()
Dim myfile, mytext
myfile = "C:\mydatfil.txt"
Open myfile For Input As #1

Do While Not EOF(1)
  Input #1, mytext
  Text1 = Text1 & mytext & vbCrLf
Loop

'You should always close any files you have opened
'as soon as you have finished with them

Close #1

End Sub


Quote:
> Hi All,
> Running VB 3
> I have a text file on disk.
> I am having a tough time getting my textbox to display multiple lines of
> text. I have set the TextBox property multiline to true but all I get with
> the following code is the first line from my text file. I have tried lots
of
> other approaches and need a shove in the right direction.

> Sub Command1_Click ()
>  Dim myfile, mytext
> myfile = "C:\mydatfil.txt"
> Open myfile For Input As #1
> Input #1, mytext
> Text1 = mytext
> End Sub

> Thanks in advance,
> mike



Mon, 18 Apr 2005 17:54:28 GMT  
 Newbie TextBox question
It's because the Input command just gets the next data item from the file, using Carriage Return / Line Feeds and commas as delimiters. So your code is just getting the text up to the first of these delimiters. You would be better off using the Line Input function (rather than the Input function) because this uses just Carriage Return / Line feeds as delimiters. However, it woiuld still get just the first line of the file. In order to get the entire file you need to get all of the lines. I'm not sure what kind of loops VB3 allows you to use, but something like the following should work for you:

Dim myfile, mytext
myfile = "C:\netlog.txt"
Open myfile For Input As #1
While Not EOF(1)
Line Input #1, mytext
Text1.Text = Text1.Text + mytext
Wend

There are far more efficient ways of getting an entire text file into a Text Box, but I'm not sure what functions VB3 has. If VB3 allows you to use the Get function then you can get the entire file in one go. Have a look.

Mike

Quote:

> Hi All,
> Running VB 3
> I have a text file on disk.
> I am having a tough time getting my textbox to display multiple lines of
> text. I have set the TextBox property multiline to true but all I get with
> the following code is the first line from my text file. I have tried lots of
> other approaches and need a shove in the right direction.

> Sub Command1_Click ()
>  Dim myfile, mytext
> myfile = "C:\mydatfil.txt"
> Open myfile For Input As #1
> Input #1, mytext
> Text1 = mytext
> End Sub

> Thanks in advance,
> mike



Mon, 18 Apr 2005 23:15:25 GMT  
 Newbie TextBox question
Mike,

(dredging out the VB3 memories, but here goes...)

Avoid using the Input Statement if you can - it does lots
of "useful", if sometimes strange, things on your behalf.

Try the Line Input Statement if you want to read the file
line-by-line or, as in this case, grab the whole lot in one go,
using

Text1.Text = Input$( LOF( iFile ), #iFile )

HTH,
    Phill  W.


. . .

Quote:
> I am having a tough time getting my textbox to display multiple
> lines of text. I have set the TextBox property multiline to true
> but all I get with the following code is the first line from my text
> file.



Mon, 18 Apr 2005 19:48:06 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Newbie textbox question

2. newbie Textbox questions

3. Newbie TEXTBOX Question... Please Help???

4. Newbie question - textbox array

5. ..easy textbox question from a newbie

6. Newbie question: Textbox value assigning

7. Newbie - VB 5 textbox question

8. Really dumb textbox newbie question..

9. TextBox - entering and displaying question (Newbie)

10. newbie question - set textbox val

11. newbie question re. numeric input into textbox

12. Newbie Questions - 1: RTF-Control - 2: Textbox-Control

 

 
Powered by phpBB® Forum Software