Reputation: 182
I have a project for fun in VB6, I'm using quite long labels and would like them to stretch the entire length of my monitor however they seem to be capped at 256 chars per line. It let's me set their caption to as long as I like but after the 256th character, the rest does not appear on the screen.
If I change it to multiline however, it will display the full text but again will automatically take a new line at the 256th character meaning it doesn't utilise the full width of my monitor.
I was wondering if anyone knows why this is, a way around it or what my options are?
Edit: After testing, using a text box and making it look like a label is an alright work around, as text boxes don't seem to have this same restriction.
Edit 2: Text boxes lack an autosize function which is crucial to my project so any further advice is appreciated.
Upvotes: 2
Views: 2121
Reputation: 8868
According to the MS Learn Caption Property's Remarks:
A Label control’s caption size is unlimited. For forms and all other controls that have captions, the limit is 255 characters.
However, as you have seen this statement may not be correct. It appears to apply to Label controls as well, and the limit is actually 256 characters in my experimentation.
I think your idea of a TextBox control should work for what you need. Since there is no AutoSize property, simply change the width of the control in the Form Resize event.
Private Sub Form_Resize()
Text1.Width = Me.ScaleWidth
End Sub
Upvotes: 3
Reputation: 6165
Adding onto Brian's answer, yes, you will need to use a TextBox if you want your "label" to be more than 255 characters. You can make a TextBox look and act like a label if you set a few things.
First, set the BorderStyle
property to vbBSNone
(or 0, if you prefer). Then, you don't want users entering text into and otherwise changing your "label." If you're not fussy, you can set the Locked
property to true. This isn't perfect, because setting the Locked
property still allows users to click on the text and move around in it.
If you're really not fussy, you can set Enabled
to false. This can confuse users, because everything gets greyed out and users are conditioned to understand that to mean that something is disabled. However, a disabled control can't be landed on or tabbed to, which is the behavior you want for a label.
If you want to get disabled behavior without altering the appearance, you need to use the API:
Private Const WS_DISABLED = &H8000000
Private Const GWL_STYLE = -16
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Sub Form_Load()
Dim theStyle As Long
theStyle = GetWindowLong(myTextBox.hwnd, GWL_STYLE) Or WS_DISABLED
Call SetWindowLong(myTextBox.hwnd, GWL_STYLE, theStyle)
End Sub
Pretty straightforward. GWL_STYLE
is the index for the window's style properties. It's a hex value that amounts to a series of flags. If you Or
the WS_DISABLED
hex value with it, the result is to set the disabled flag. Which gets set when you set the window with the new value for GWL_STYLE
.
Here are the different settings handled by GW_STYLE
.
Upvotes: 2