Link to home
Start Free TrialLog in
Avatar of COOMET
COOMET

asked on

RichTextBox.LoadFile, How about for TextBox ?


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 ?

Avatar of Julian_K
Julian_K
Flag of Bulgaria image

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.
Avatar of COOMET
COOMET

ASKER


Can you show an example from the above two files (file1.txt and file2.txt) ?
Avatar of Mike Tomlinson
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...
Avatar of COOMET

ASKER


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.
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
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
Avatar of COOMET

ASKER


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
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of COOMET

ASKER

Thanks Idle_Mind !