[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

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?
0
GohdanTheMoblin
Asked:
GohdanTheMoblin
  • 2
1 Solution
 
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
 
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now