Display the whole caption into a Label
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Display the whole caption into a Label

  1. #1
    Join Date
    Dec 2012
    Posts
    38

    Display the whole caption into a Label

    Goodmorning

    I've this trouble: a Label must display a string of 40 characters and can fill at most a space of 4100x615 twips. But, if I set the AutoSize or WordWrap properties, the text exceeeds this space, and if I don't use them, Label won't show the whole text (because part of it will go over the control borders.

    So, I got an idea: try to change Font size until text is fully contained into the control. But neither this works

    Please, help me to found a solution.


  2. #2
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,845

    Re: Display the whole caption into a Label

    Well... looks like using the default font and your size label it holds 39 characters if those characters are all X or other character of that size on one line. Word wrap only works when there is a space so that does nothing for a continuous string of characters.

    If I turn on word wrap and use a string which has a bunch of Xs with a space every 10 characters I can get 80 characters in that space with room to spare.

    So it would seem that if you are using word wrap and an 8 pt font that your string must not have any spaces you could make this string wrap by adding a VBCRLF into the string at the point where you need it to wrap or by turning on word wrap and inserting a space where it needs to be wrapped.

    Try this... place a label named label1 on the form with the size you specified, leave the font at the default and turn on word wrap. Place a command button on the form with the default name of command 1 then add this code and try it

    Code:
    Private Sub Command1_Click()
    Label1.Caption = ""
    Dim x As Integer
    For x = 1 To 79
        If x Mod 10 = 0 Then
            Label1.Caption = Label1.Caption & " "
        Else
            Label1.Caption = Label1.Caption & "X"
        End If
    Next
    Label1.Caption = Label1.Caption & "W"
    
    End Sub
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Dec 2012
    Posts
    38

    Re: Display the whole caption into a Label

    Following your suggestion, I wrote the code below:
    Code:
    Private Sub Command1_Click()
        If InStr(1, Label1, " ") <> 0 Then Exit Sub
        Set Me.Font = Label1.Font
        Dim crlf As Integer
        crlf = Len(Label1.Caption)
        Do While Me.TextWidth(Mid(Label1.Caption, 1, crlf)) > Label1.Width And crlf >= 1
            crlf = crlf - 1
        Loop
        Label1 = Left(Label1, crlf) & " " & Right(Label1, Len(Label1) - crlf)
    End Sub
    Even though this is not the best solution, 'cause if there are other characters that exceeds the lower border, you won't see them. But, for now, it's a starting point where begin to work seriously.

    Thanks


    PS: any improvement is always wellcome!

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center