Solved

RichTextBox.LoadFile, How about for TextBox ?

Posted on 2006-11-17
9
690 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
Comment Utility
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
Comment Utility

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

Expert Comment

by:Mike Tomlinson
Comment Utility
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
Comment Utility

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
Comment Utility
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
Comment Utility

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
Comment Utility
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
Comment Utility
Thanks Idle_Mind !
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…

771 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

13 Experts available now in Live!

Get 1:1 Help Now