CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2000
    Posts
    57

    How can we calculate the width of the control from the length of a string

    How can we calculate the width of the control from the length of a string. I can get the length of a string using len(strvar), now I need to reset the width of the control (label or command button) so that the strvar is completely displayed as the caption of the control.

    thanks

    Ali.


  2. #2
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: How can we calculate the width of the control from the length of a string

    I made a quick example, wich uses the printer object (yes, the printer object)

    ' create a form, add a textbox and a commandbutton
    private Sub Text1_Change()

    Dim W as Single
    Dim oldFont as Variant

    ' get printer settings
    set oldFont = Printer.Font

    ' set font to the font of the control you want to resize
    set Printer.Font = Command1.Font

    ' het width of text
    W = Printer.TextWidth(Text1.Text)

    ' set width and caption
    Command1.Width = W + 360
    Command1.Caption = Text1.Text

    ' restore original font to printer
    set Printer.Font = oldFont

    End Sub



    When you type something in the textbox, you will see that the button automatically resizes

    Tom Cannaerts
    [email protected]

    The best way to escape a problem, is to solve it.
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  3. #3
    Join Date
    Feb 2001
    Location
    PA
    Posts
    163

    Re: How can we calculate the width of the control from the length of a string

    If you set the label's Autosize = True then the label will automatically expand with the length of the caption.


  4. #4
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: How can we calculate the width of the control from the length of a string

    If it is a label. If not?
    I enjoied cakkie solution.
    I used to use an Api to get the width of default fonts (=the font of form), but it did not work properly...


    Special thanks to Lothar "the Great" Haensler. Come back soon, you Guru.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  5. #5
    Join Date
    Apr 2001
    Posts
    8

    Re: How can we calculate the width of the control from the length of a string

    The Form object also has a "TextWidth" method. It may be more correct to use that, because if the computer your application is running on does not have a printer installed, the code will crash! Simply substitute the "Printer" object for the name of your form (or the "Me" keyword).

    You may also have unexpected behaviour if the default printer is a text-only printer.

    One last thing: don't forget to restore the "Font" property of the form to its original value or you could have some strange behaviour with labels.


  6. #6
    Join Date
    Mar 1999
    Location
    Nepal
    Posts
    540

    Re: How can we calculate the width of the control from the length of a string

    If it's not a label, still you can use a dummy label on your form to compute the height and width of the string.

    lbldummy.Font = ....
    ...
    lblDummy.AutoSize = True
    lblDummy.Caption = yourString

    text1.width = lblDummy.width + Some_margin
    text1.height = lblDummy.height + Some_margin



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

Featured