nikademous
Technical User
Hello, I have a bound textbox named txtItemText and I need to have this textbox expand to the right to fit the text that's displayed. Is this possible and if so how?
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
I cant because its a bound textbox...Andrzejek said:If you just want to 'display' the text, I would use a label instead.
I have to use a text box because its bound and I don't believe you can show data from a table in a label. I will not be typing data into this text box it only displays data that has previously been entered elsewhere.Andrzejek said:But if you do want to use a text box, are you going to allow changing the text / typing a new text in it?
I'm not typing into the textbox its going to display data already entered elsewhere so as the records change new data will be displayed and the text box will have to be expanded each time.Andrzejek said:If so, what about if you want to display 'ABC' in this text box, but you want to replace the 'ABC' with a lot longer text? Do you want to expand the text box while you type?
[blue]Private Sub Text0_Change()
Text0.Width = WidthFromFontWiz(Text0.Text, Text0.FontName, Text0.FontSize)
[COLOR=green]' We need to add the padding[/color]
Text0.Width = Text0.Width + Text0.LeftPadding + Text0.RightPadding
End Sub
Public Function WidthFromFontWiz(ByVal Caption As String, ByVal FontName As String, ByVal Size As Long, Optional ByVal Weight As Long = 400, Optional Italic As Boolean = False, Optional Underline As Boolean = False, Optional Cch As Long = 0, Optional MaxWidthCch As Long = 0) As Double
Dim dx As Long
Dim dy As Long
[COLOR=green]' Activate Access WizHook object[/color]
WizHook.Key = 51488399
WizHook.TwipsFromFont FontName, Size, Weight, Italic, Underline, Cch, Caption, MaxWidthCch, dx, dy
WidthFromFontWiz = dx + 15 [COLOR=green]'1 pixel short ...[/color]
End Function[/blue]
Public Sub AutoFit(ctl As Control)
Dim lngWidth As Long
lngWidth = GetTextLength(ctl, ctl.Value)
ctl.Width = lngWidth + 130 '40
End Sub
Public Function GetTextLength(pCtrl As Control, ByVal str As String, _
Optional ByVal Height As Boolean = False)
Dim lx As Long, ly As Long
' Initialize WizHook
WizHook.Key = 51488399
' Populate the variables lx and ly with the width and height of the
' string in twips, according to the font settings of the control
WizHook.TwipsFromFont pCtrl.FontName, pCtrl.FontSize, pCtrl.FontWeight, _
pCtrl.FontItalic, pCtrl.FontUnderline, 0, _
str, 0, lx, ly
If Not Height Then
GetTextLength = lx
Else
GetTextLength = ly
End If
End Function
Const TWIPSTOINCHES = 1440
'TWIPS times (number of inches for 1 character at 8 point Calibri)
Const TWIPSTOCHARWIDTH = TWIPSTOINCHES * 0.06
Me.txtFamilyTree.ColumnWidth = TWIPSTOCHARWIDTH * 5 'desired character width of 5 characters
Me.txtSearchTerm.ColumnWidth = TWIPSTOCHARWIDTH * 25