Label painting issue

I'm using custom painting to display a string in a label with the StringTrimming set to display shortened paths when the string is too long. For example, instead of seeing "C:\Windows\Syst" I would see "C:\...\System32".

The code I have is like this:

        Dim sf As New StringFormat(StringFormatFlags.LineLimit)
        sf.Trimming = StringTrimming.EllipsisPath

        With lblCurrentFolder
            g.FillRectangle(backBrush, e.ClipRectangle)
            g.DrawString(.Text, .Font, foreBrush, RectangleF.op_Implicit(e.ClipRectangle), sf)
        End With

This works fine, but I noticed something odd about the way the label is drawn. When the form is on top everything is fine. But if I drag another window around, the string is only drawn to the edge of the label that is visible.

This is most definitely because of the ClipRectangle being changed to reflect the visible area. I tried using the label's bounds instead, but then it wrapped the text across two lines instead of only drawing a single line. I've tried using StringFormatFlags.NoWrap in addition to LineLimit, but nothing seems to be forcing the text onto one line.  What am I doing wrong?
LVL 2
GohdanTheMoblinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RonaldBiemansCommented:
This seems to work

    Private Sub Label1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Label1.Paint
        Dim sf As New StringFormat(StringFormatFlags.LineLimit)
        sf.Trimming = StringTrimming.EllipsisPath
        Dim rectf As New Rectangle(0, 0, Label1.Width, Label1.Height)
        With Label1
            e.Graphics.FillRectangle(New SolidBrush(Label1.BackColor), rectf)
            e.Graphics.DrawString(.Text, .Font, New SolidBrush(Color.Black), RectangleF.op_Implicit(rectf), sf)
        End With

    End Sub
0
RonaldBiemansCommented:
sorry forgot the NoWrap

Private Sub Label1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Label1.Paint
        Dim sf As New StringFormat(StringFormatFlags.LineLimit)
        sf.Trimming = StringTrimming.EllipsisPath
        sf.FormatFlags = StringFormatFlags.NoWrap
        Dim rectf As New Rectangle(0, 0, Label1.Width, Label1.Height)
        With Label1
            e.Graphics.FillRectangle(New SolidBrush(Label1.BackColor), rectf)
            e.Graphics.DrawString(.Text, .Font, New SolidBrush(Color.Black), RectangleF.op_Implicit(rectf), sf)
        End With

    End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GohdanTheMoblinAuthor Commented:
Yes, that works perfectly.  I wonder why using the bounds didn't work.... the only difference I see in the bounds property is it has the location of the label set, I guess that throws DrawString off or something.  Thanks for the help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.