Textbox question

RobbertH

Active Member
Joined
Apr 29, 2008
Messages
310
I have a textbox on an userform that will enable the user to write some comments on date entered in the userform. I set the size of the textbox so that it will match other control sizes in the form. However, the user might have to enter more text than would fit in the user form.
How do I set the textbox so that
1. the width will remain as preset
2. the height will increase when enter is pressed
3. All text remains visible at all times

Hope this is possible. I get it to work separately but not together.

Thanks for any help!
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
1. Ensure that the TextBox's Multiline and EnterKeyBehavior properties are set to "True."
2. Make it's _change event code look thus:
Code:
Private Sub TextBox1_Change()
Dim key_pressed As Integer

key_pressed = Asc(Right(Me.TextBox1.Text, 1))

    With Me.TextBox1
        If key_pressed = 10 Then .Height = .Height + 10
    End With
End Sub
This tests each time a character's entered, and if it's the return key, expands the box's height by 10. If you need more or less change of height (depending upon your font size etc) just change the "10" to something more suitable.

It's probably not the best solution, but it should suit your purpose.

If you're not sure where to post the code, post back and we'll help.

Let us know how you get on.
 
Upvote 0
Thanks sykes that was actually what I was looking for. One thing it won't do however. When i type data in the textbox it automatically resizes the width so that it would fit the character. When i continue typing the width would increase accordingly.
How do prevent from resizing in the first place, and how do I set a maximum to the width?

Thanks for your help!
 
Upvote 0
Hi Robert

Mine's not doing that, so it must me a property that you've specified for your textbox.

Can you just confirm that it is an ActiveX textbox you're using, and not a forms one? I'm sure it is, otherwise you wouldn't be able to put the code in where I said it needed to go.......

Also - check that the AutoSize property of the box is set to false (right click the TB, select properties, then go to AutoSize).

Finally - ensure that the WordWrap property is set to True.

Im sure it'll be one (or a combination) of these property settings.........

Hope this helps.
 
Upvote 0
Sykes, Im afraid that wont do the trick. When I change to the setting you propose it would indeed stay at its original width but also increase in height everytime I enter one character. What i do wrong here?
 
Upvote 0
Don't textboxes on a userform have scrollbars?

Or a property you can set to show them.
 
Upvote 0
They do indeed but i'm looking for a possibility to increase the height of the textbox so that I even after pressing enter can still read the whole text without using the scroll bar.
If this is not possible its not possible but I'm sure there should be a way to accomplish this, I just dont know how...:confused:
 
Upvote 0
There might be away to do this but there's a lot of factors, eg font/control/form width/height, screen resolution etc, to take into consideration.

To get it right and make it consistent would be pretty complicated.:)
 
Upvote 0
I almost got it to work with the following code.
Code:
Private Sub Comment_tb1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
With comment_tb1
    If KeyCode = 13 Then 'KeyCode 13 = Enter
        .Height = .Height + 10
    End If
    If Len(comment_tb1.Text) = 39 Then
        'something to enter
    End If
    If KeyCode = 46 Then 'KeyCode 46 = DEL
        .Height = .Height - 10
    End If
End With
End Sub
The only thing I am after now is how to make the code 'automatically' press enter. Pressing enter will extent the height of the textbox and the user textbox should be starting a new line after entering 39 characters.

Anybody some ideas?
 
Upvote 0
You could monitor the no of characters entered using the change event, adding a carriage return (vbCrLf) after every 39.
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,777
Members
452,942
Latest member
VijayNewtoExcel

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