VB6: label caption displays only to last visible space - need to compute text width
Our program displays text in a label's caption. The label has no wordwrap, is one line, fixed width.
I need to find out the width of the displayed text.
When the caption is longer than what fits the label, one of two things happen:
If there are no spaces in the text, the full width of the label contains contains letters. (In this case, the width of the displayed text is the label width)
If there are spaces, VB displays text up to the last visible space, and drops the rest.
Two Qs:
- Is it possible to make VB display as much of the caption as possible?
- [If not]: How can we determine the width of displayed text? When we write the text into an auto-adjust label, the label size is determined by the full text, not the text displayed up to the last space.
We could implement an iterative "keep cutting text after space" until the width is less than the label width, but this is tedious, inelegant, and possibly prone to errors.
Re: VB6: label caption displays only to last visible space - need to compute text wid
@Eduardo: the given label must be fixed width, so autosize is false. [First line of my post said that]
@Fafalone: me.width("txt") will be the same result as stuffing the text into another auto-sized label, which shows the width of the entire text. I need to find the width of the visible text
@chosk: Krool's Replacement Label control has a LOT there. Can you point me to the specific calls to find the width of displayed text, with the text after the last visible space discarded?
Re: VB6: label caption displays only to last visible space - need to compute text wid
Originally Posted by murspieg1
@chosk: Krool's Replacement Label control has a LOT there. Can you point me to the specific calls to find the width of displayed text, with the text after the last visible space discarded?
It is automatic. It will show what it can show including space within the width of the Label you set.
I should also link the OCX version. It is simpler to use and prefered:
Re: VB6: label caption displays only to last visible space - need to compute text wid
@Eduardo - good point, my original post wasn't totally clear. The label had to be fixed width bec of its use in the app's design.
Will look into Chosk's suggestions,and cycle back with any Qs. Gracious thanks to you all
Re: VB6: label caption displays only to last visible space - need to compute text wid
As a (in one of my past lives) software inspector/instructor, I am used to developers' 'lack of understanding' of end-users' desires, wishes and needs. On many occasions, I had to let the developers know that 'it doesn't make sense' they way the coded something (not only for functionality, but also, and very important, for logical displays).
Oh sure, it MAY make sense to keep a label only showing PART of a string, but is that what a user wants to see? Of what value would that be (not showing the whole string)? Yes, you probably have your reasons, but I would be telling a software developer to relook the interface and make it more 'user-friendly'.
So, hope the suggestions you received from others help you, but you may want to CONSIDER a slightly different design of your form. (Just a suggestion.)
Re: VB6: label caption displays only to last visible space - need to compute text wid
@Sam: Appreciate the comment. But there's other objects that we're displaying on this form. I know the user community very well - leaders of folkdance sessions - as I am one myself. For this application, the first, major portion of the string (the dance name) is enough, and the other objects that get displayed (an image of country where it's from) is important too.
But your design suggestion should always be considered, so thanks
Re: VB6: label caption displays only to last visible space - need to compute text wid
After you implement your design (to show only partial string), might I then suggest that a 'hover' feature be added so that a user can place the mouse over the label (like using the ToolTip property) and see, if desired, the entire string?
Re: VB6: label caption displays only to last visible space - need to compute text wid
@Sam: Appreciate the suggestion. I do use tooltips elsewhere. In this particular case, this form displays on a 2nd monitor. It's used as a dance hall display, and is usually several feet away from the user's laptop. It would be difficult to navigate the mouse to that exact spot. Actually, the labels are "dances previously done" and are just a reminder for the dancers of what played a cpl minutes previously. They don't need the whole name for that reminder!
Thanks for the suggestion - other programmers in the same pickle may use the tooltip idea. Happy New Year to our entire community of helpers.
Re: VB6: label caption displays only to last visible space - need to compute text wid
It is possible to do it with a hidden multi-line TextBox.
Set the textbox size and font as the label.
Now TextBox has EM_LINEINDEX message that returns the character index at the first of a given line.
You need to check the index of the character at second line (index 1):
Code:
Const EM_LINEINDEX As Long = &HBB&
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Function GetFirstLine(lbl As Label) As String
Dim p As Long
Text1 = lbl
p = SendMessage(Text1.hwnd, EM_LINEINDEX, 1, 0)
If p = -1 Then
GetFirstLine = lbl
Else
GetFirstLine = Left(lbl, p)
End If
End Function