Solved

RichTextBox.LoadFile, How about for TextBox ?

Posted on 2006-11-17
9
708 Views
Last Modified: 2013-12-26

1) In RichText box, we have .LoadFile (filename) to load a file content into the richtextbox.  How about in TextBox ?  Is there a loadfile equivalent ?

2) How can we load two or three files in one TextBox (RichTextBox) ?

For instance, I have two files:

c:\file1.txt
c:\file2.txt

Then I have RichTextBox1 and TextBox1

How can we load the above two files in RichTextBox1 using the .LoadFile ?

How can we load the above two files in textBox1 using a kind of .loadfile equivalent ?

0
Comment
Question by:COOMET
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 5

Expert Comment

by:Julian_K
ID: 17964319
Hello.

1) In TextBox there is no equivalent. You have to assign a string to its .Text property and that is it. You have to load the file, either by native VB commands for file handling (Open, input, etc...), or using the FSO object.

2) On the textbox, you have to concatenate the data: like (Text1.Text = Text1.Text & newText). In RichTextBox you could do something simillar, using the TextRTF property of the control.

Regards,
Julian.
0
 

Author Comment

by:COOMET
ID: 17964612

Can you show an example from the above two files (file1.txt and file2.txt) ?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 17965721
Before proceeding, can you clarify what version VB you are working with?

Your comment, "Then I have RichTextBox1 and TextBox1" suggests a version of .Net rather than VB6...
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

Author Comment

by:COOMET
ID: 17966994

Idle_Mind,

Sorry for not mentioning.  I work on VB6 Pro

When I said that I have RichTextBox1 and TextBox1, I simply meant that I put the two controls on a form.  Depending on the performance of each, I will eventually choose one of the two controls.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 17967143
To append an RTF file to the end of an RTB:

Private Sub Command1_Click()
    AppendRtfFile RichTextBox1, "c:\someFile.rtf"
End Sub

Public Sub AppendRtfFile(ByVal rtb As RichTextBox, ByVal fileName As String)
    rtb.SelStart = Len(rtb.Text)
    Dim ff As Integer
    ff = FreeFile
    Open fileName For Binary Access Read As #ff
    rtb.SelRTF = StrConv(InputB(LOF(ff), ff), vbUnicode)
    Close #ff
End Sub
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 17967160
To append the contents of a text file to the end of a textbox:

Private Sub Command1_Click()
    AppendTextFile Text1, "c:\someFile.txt"
End Sub

Public Sub AppendTextFile(ByVal tb As TextBox, ByVal fileName As String)
    tb.SelStart = Len(tb.Text)
    tb.SelText = GetFileContents(fileName)
End Sub

Private Function GetFileContents(ByVal fileName As String) As String
    Dim ff As Integer
    Dim entireFile As String
   
    If Dir(fileName) <> "" Then
        ff = FreeFile
        Open fileName For Binary Access Read As #ff
        GetFileContents = Input(LOF(ff), ff)
        Close #ff
    Else
        MsgBox fileName, vbExclamation, "File Not Found"
    End If
End Function
0
 

Author Comment

by:COOMET
ID: 17967824

The append doesn't begin in a new line.  The last record of the first array and the first record of the second array are overlapping.  

fo instance:

2
2
2
23
3
3
3

Instead of:
2
2
2
2
3
3
3
3
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 25 total points
ID: 17967870
So add one in...  =)

(it really depends on whether there is a trailing CrLf in the file though)

This adds a carriage return BEFORE appending the file:

    Public Sub AppendTextFile(ByVal tb As TextBox, ByVal fileName As String)
        tb.SelStart = Len(tb.Text)
        tb.SelText = vbCrLf & GetFileContents(fileName)
    End Sub

This adds a carriage return AFTER appending the file:

    Public Sub AppendTextFile(ByVal tb As TextBox, ByVal fileName As String)
        tb.SelStart = Len(tb.Text)
        tb.SelText = GetFileContents(fileName) & vbCrLf
    End Sub

Do a similar thing for the RTB if that is what you are using...
0
 

Author Comment

by:COOMET
ID: 17976210
Thanks Idle_Mind !
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question