Reputation: 239
If i were to want to re-size a richtextbox
in vb6 so that its width is always equal to the length of its text how would i go about doing that? I'm not aware of any methods for measuring the proper length of a text string in the richtextbox
control. at some point i had relative success with the textwidth
property
Private Sub Form_Resize()
RTBarcode.Height = Barcode.Height - 1700
RTBarcode.Width = TextWidth(RTBarcode.Text)
End Sub
but it was too unreliable and i wasn't sure what it was measuring. any help would be greatly appreciated!
Upvotes: 0
Views: 1671
Reputation: 2951
you can set the font of your form to the font of your richtextbox, and then measure the width of your text as it would appear on your form
make sure though that you don't print anything on your form anywhere else in your code, as this code will change the font of that as well
Have a look at the following test project :
'1 form with :
' 1 richtextbox control: name=RichTextBox1
' 2 command buttons : name=Command1 name=Command2
Option Explicit
Private Sub Command1_Click()
Dim lngWidth As Long
With RichTextBox1
Font.Name = .Font.Name
Font.Size = .Font.Size
lngWidth = TextWidth(.Text)
End With
Caption = CStr(lngWidth)
End Sub
Private Sub Command2_Click()
With RichTextBox1
.Font.Size = 2 * .Font.Size
End With 'RichTextBox1
End Sub
Private Sub Command3_Click()
With RichTextBox1
.SelFontSize = 2 * .Font.Size
End With 'RichTextBox1
End Sub
Enter some text in the rtb and click on Command1 (the default text should give the value 1005 in the caption of the form
Then click on Command2 which will double the font size and click on Command1 again (the default text would give the value 2010 in the caption of the form
Then select a part of the text in the rtb and click on Command3 which will double the font size of your selection only, then click on Command1 again and see that the value in the caption of the form doesn't change
If you don't allow for different fonts inside your rtb then the code above might work, it will even select the longest line for you and measure that line's width
Upvotes: 1
Reputation: 316
In the RichTextBox's _Change() event, you use the TextWidth() function.
With RichTextBox
.Width = IIf(0 = Len(.Text), TextWidth("W"), TextWidth(.Text))
End With
I don't have VB6 available right now but give that a shot, it might need some changes. But if it's empty you definitely don't want Width to = 0 or they can't type anything, so default is 1 character width (the "W"
).
Upvotes: 1
Reputation: 239
i did figure it out using this code
Private Sub Form_Resize()
Dim char As Long
char = Len(RTBarcode.Text)
RTBarcode.Width = char * 550
RTBarcode.Height = Barcode.Height - 1700
End Sub
you can put this in wherever you need it to be excecuted, i have it in the resize sub and a button click sub.
Upvotes: 1