[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 726
  • Last Modified:

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 ?

0
COOMET
Asked:
COOMET
  • 4
  • 4
1 Solution
 
Julian_KCommented:
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
 
COOMETAuthor Commented:

Can you show an example from the above two files (file1.txt and file2.txt) ?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
COOMETAuthor Commented:

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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
COOMETAuthor Commented:

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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
COOMETAuthor Commented:
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!

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now