Adding Text to a TextBox via code

cs1ctp

Board Regular
Joined
Oct 7, 2005
Messages
122
Hi,

What is the code for a new line of text to a text box attached to a userform, for example:

Code:
 UserForm1.Text.Add = “/nl”, ArrayContract(i)

Where ArrayContract(i) is a line of text eg: Microsoft Excel

Basically this code is within a loop that searches an array for text values that meet a condition. The chances are the search will find up to three text lines that it will add to the textbox, I am just not sure of the code to add the text to the text box.

Thanks is advance for your help,

Chris
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Chris

Something like this perhaps?
Code:
For i = LBound(ArrayContract) To UBound(ArrayContract)

     UserForm1.Text = UserForm1.Text & ArrayContract(i) & vbCrLf
Next i
 
Upvote 0
Hi Norie,

I managed to update the textbox using this code:

Code:
ContractInfo.TextBox1.Text = ArrayContract(i)

This prints the text to the textbox exactly as I need, however, it constantly gets overwritten each time this is called. Is there anyway of simply adding a new line so when text is added to the textbox it is on a new line and does not replace the previous text that was already written.

Thanks,

Chris
 
Upvote 0
Chris

Did you see the code I posted?
 
Upvote 0
Sorry, yes I looked at your code, but it seems to run an endless loop printing the whole array over and over again. Perhaps if I provide you a little more detail:

Code:
Open "U:/contracts.txt" For Input As #1
    k = 0
    On Error Resume Next
    Do While Not EOF(1)
        
        ReDim Preserve ArrayContract(k + 1) As Variant   'resize the array to add another place
        ReDim Preserve ArrayMyTop(k + 1) As Variant      'resize the array to add another place
        ReDim Preserve ArrayMyLeft(k + 1) As Variant     'resize the array to add another place
        
        Input #1, ArrayContract(k), ArrayMyTop(k), ArrayMyLeft(k)
        
        k = k + 1
    Loop
    Close #1

For i = LBound(ArrayMyTop) To UBound(ArrayMyTop)
        If Top = ArrayMyTop(i) Then
            For j = LBound(ArrayMyLeft) To UBound(ArrayMyLeft)
                If Left = ArrayMyLeft(i) Then
                    ContractInfo.TextBox1.Text = ArrayContract(i)
                End If
            Next j
        End If
    Next i

So I am first reading a text file and assigning the values to three different arrays. I am then searching the arrays for specific values ... once the first value I am looking for is found in the first array, it then searches another array to see if the second value is found in the same position. Once this match has been made I then wish to print the text information found in that position to the textbox. This works, but when another record is found matching the same constraints, it is again printed, but replaces the previous entry.

I tried to add the & vbCrLf code, but it simply prints the newline symbol to the text box, but does not move down a line and print!!

Basically I am just after a list to be printed in the textbox eg:

Software1
Software5
Software6

Hope this makes sense, thanks for taking the time to look at this.

Regards,

Chris
 
Upvote 0
Chris

Did you try my code?

It doesn't run an endless loop, it loops through each element of the array, adds it to the existing text of the textbox, adds a carriage return and so on.

By the way if you want >1 line in your textbox you need to set it's MultiLine Property.

Also are Top and Left variables? If they are you should consider renaming them, it's not a good idea having variables with the same name as VBA properties/methods/functions etc.
Code:
Open "U:/contracts.txt" For Input As #1
    k = 0
    On Error Resume Next
    Do While Not EOF(1)
        
        ReDim Preserve ArrayContract(k + 1) As Variant   'resize the array to add another place
        ReDim Preserve ArrayMyTop(k + 1) As Variant      'resize the array to add another place
        ReDim Preserve ArrayMyLeft(k + 1) As Variant     'resize the array to add another place
        
        Input #1, ArrayContract(k), ArrayMyTop(k), ArrayMyLeft(k)
        
        k = k + 1
    Loop
Close #1

For i = LBound(ArrayMyTop) To UBound(ArrayMyTop)
    If Top = ArrayMyTop(i) Then
        For j = LBound(ArrayMyLeft) To UBound(ArrayMyLeft)
            If Left = ArrayMyLeft(i) Then
                ContractInfo.TextBox1.Text = ContractInfo.TextBox1.Text & ArrayContract(i) & vbCrLf
            End If
        Next j
    End If
Next i
 
Upvote 0
Perfect ... sorry, I was getting confused with the array names and the for loops, which is why I couldn't get it to work. I see what you mean about the variables, I will definitely change them!!

Your help has been much appreciated with this query, thanks for bearing with me.

Chris
 
Upvote 0

Forum statistics

Threads
1,203,464
Messages
6,055,573
Members
444,799
Latest member
CraigCrowhurst

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top