Results 1 to 15 of 15

Thread: VB6: label caption displays only to last visible space - need to compute text width

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2023
    Posts
    14

    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.

    Tnx

  2. #2
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,653

    Re: VB6: label caption displays only to last visible space - need to compute text wid

    width = Me.TextWidth("text")

  3. #3
    Hyperactive Member
    Join Date
    Feb 2014
    Posts
    294

    Re: VB6: label caption displays only to last visible space - need to compute text wid

    Krool's Common Control Replacement Label control solve the problem. It will show caption text up to the width of the label.
    https://www.vbforums.com/showthread....mmon-controls)

    MS Label control wrap the caption text at the space even if WordWrap is set to False. Increase the height of the Label to see this wrap effect.

  4. #4
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,667

    Re: VB6: label caption displays only to last visible space - need to compute text wid

    Label's AutoSize property = True?

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2023
    Posts
    14

    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?

    Thx you all for such rapid replies!

  6. #6
    Hyperactive Member
    Join Date
    Feb 2014
    Posts
    294

    Re: VB6: label caption displays only to last visible space - need to compute text wid

    Quote Originally Posted by murspieg1 View Post
    @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:

    https://www.vbforums.com/showthread....55#post5129155

  7. #7
    Hyperactive Member
    Join Date
    Feb 2014
    Posts
    294

    Re: VB6: label caption displays only to last visible space - need to compute text wid

    If there are spaces, VB displays text up to the last visible space, and drops the rest.

    - Is it possible to make VB display as much of the caption as possible?
    Example:
    Attached Images Attached Images  
    Last edited by chosk; Dec 29th, 2023 at 10:00 AM.

  8. #8
    PowerPoster
    Join Date
    Feb 2017
    Posts
    5,667

    Re: VB6: label caption displays only to last visible space - need to compute text wid

    Quote Originally Posted by murspieg1 View Post
    @Eduardo: the given label must be fixed width, so autosize is false. [First line of my post said that]
    It says that it has a fixed width, not that it must be fixed (nor gives a reason).
    Good luck.

  9. #9

    Thread Starter
    New Member
    Join Date
    Jul 2023
    Posts
    14

    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

  10. #10
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    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.)
    Sam I am (as well as Confused at times).

  11. #11

    Thread Starter
    New Member
    Join Date
    Jul 2023
    Posts
    14

    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

  12. #12
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    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?
    Sam I am (as well as Confused at times).

  13. #13

    Thread Starter
    New Member
    Join Date
    Jul 2023
    Posts
    14

    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.

  14. #14
    Hyperactive Member
    Join Date
    Jul 2021
    Posts
    267

    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

  15. #15
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,622

    Re: VB6: label caption displays only to last visible space - need to compute text wid

    @OP...ah, now I understand your project. Sounds like you can implement those other suggestions and get exactly what you want.

    And, to you as well, Happy New Year!

    Sam

    outa here
    Sam I am (as well as Confused at times).

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width