Label exactly that width of his text...

Posted on 2004-10-24
Last Modified: 2010-05-18

I have a problem. I several labels which I can move and they have a transparent background

Now I can edit the text of the label.

So my question: How can I make the label exact that width that it is showing the text and not more? Otherwise I have problems if I place two labels aside of each others...

The Problem is that 'i' takes a half a size then 'B' and so on...

Any solutions?

Thanks a lot

Question by:Svenrutten
    LVL 85

    Accepted Solution

    Something like this may work:

        Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
            Label2.Text = TextBox1.Text
            Dim g As Graphics = Label2.CreateGraphics
            Label2.Width = g.MeasureString(Label2.Text, Label2.Font).Width + 1 ' (a little flub)
        End Sub


    Author Comment

    That's great!! Didn't think that this would be possible!

    Thanks so much!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    This video Micro Tutorial is the second in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 ( But the ability to create custom scanning profiles a…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now