Solved

RichTextBox.LoadFile, How about for TextBox ?

Posted on 2006-11-17
9
699 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
  • 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 85

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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 85

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 85

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 85

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now