Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with string length for textbox

Status
Not open for further replies.

markvan

Technical User
Sep 12, 2001
37
0
0
AU
I am successfully displaying the content of an external text file in a textbox txtWelcomeMessage. The problem is, it only displays the first 120 or so characters from the text file. I upped the MaxLength property on the textbox but it makes no difference, I think it has to do with the variable being declared as a string. Any suggestion would be helpful, as I am a newbie at VB.

Here's my code:

Dim strFile As String = "WelcomeMessage.txt"
'=========================================
Private Sub btnSaveChanges_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveChanges.Click
' The output file is written in the same file
Dim tFile As StreamWriter = File.CreateText(strFile)
tFile.WriteLine(txtWelcomeMessage.Text)
tFile.Close()
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Populate textbox1 on program load
Dim tFile As StreamReader = File.OpenText(strFile)
txtWelcomeMessage.Text = tFile.ReadLine()
tFile.Close()
End Sub

Mark Van
EZ-PC Help
 
Nope your is only reading one line of the textfile hence readline

go for something like this

Code:
dim lines() as string
dim tr as io.textreader
dim inttemp as integer

Tr = System.IO.File.OpenText(strfile)
Lines = Split(Tr.ReadToEnd(), vbCrLf)
Tr.Close()
for inttemp = 0 to lines.getupperbound(0)-1
  txtwelcomemessage.text &= lines(inttemp) & vbcrlf
next

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Thanks Christiaan

You solved my problem, works perfectly.

This is the working code in case anyone else needs it:

Dim strFile As String = "WelcomeMessage.txt"
Dim lines() As String
Dim tr As IO.TextReader
Dim inttemp As Integer


'=========================================
Private Sub btnSaveChanges_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveChanges.Click
' The output file is written in the same file
Dim tFile As StreamWriter = File.CreateText(strFile)
tFile.WriteLine(txtWelcomeMessage.Text)
tFile.Close()
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Populate textbox1 on program load
Dim tFile As StreamReader = File.OpenText(strFile)
txtWelcomeMessage.Text = tFile.ReadLine()
tFile.Close()
'=========================================
tr = System.IO.File.OpenText(strFile)
lines = Split(tr.ReadToEnd(), vbCrLf)
tr.Close()
For inttemp = 0 To lines.GetUpperBound(0) - 1
txtWelcomeMessage.Text &= lines(inttemp) & vbCrLf
Next
'=========================================
End Sub


Mark Van
EZ-PC Help
 
Sorry guys one mistook in that code, it works but duplicates the first line in the output.

All you need to do is comment out this line then it will work perfectly:

'txtWelcomeMessage.Text = tFile.ReadLine()

Cheers,


Mark Van
EZ-PC Help
 
LOL, sorry again guys.

Guess what? now it is working and I have checked it thoroughly.

Here it is:

Dim strFile As String = "WelcomeMessage.txt"
Dim lines() As String
Dim tr As IO.TextReader
Dim inttemp As Integer

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
tr = System.IO.File.OpenText(strFile)
lines = Split(tr.ReadToEnd(), vbCrLf)
tr.Close()
For inttemp = 0 To lines.GetUpperBound(0)
txtWelcomeMessage.Text &= lines(inttemp) & vbCrLf
Next
End Sub

Mark Van
EZ-PC Help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top