Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

.Drawstring trimming text problem.

Status
Not open for further replies.

Sorwen

Technical User
Nov 30, 2002
1,641
US
I'm drawing text to a control in the OnPaint event. It works fine except that it is trimming the string. I need the string to be a certain length.

Code:
        Dim rect As New System.Drawing.RectangleF(Me.ClientRectangle.X, Me.ClientRectangle.Y, Me.ClientRectangle.Width, Me.ClientRectangle.Height)
        Dim strFmt As New System.Drawing.StringFormat

        strFmt.LineAlignment = Drawing.StringAlignment.Near 'Far = Top, Near = Bottom
        strFmt.Alignment = Drawing.StringAlignment.Center
        strFmt.Trimming = Drawing.StringTrimming.None

        sender.DrawString(text.PadLeft(9), Me.Font, New System.Drawing.SolidBrush(Me.FontColor), rect, strFmt)
Any ideas?

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Trimming the string? I assume your string is wider than your drawing surface area. What do you want to occur in that case?
 
Basically it is for the progress bar I created quite awhile ago. The string reads as " 28/1000", but is drawn as "28/1000". As the digits are added the text adjusts fine. For example for "100/1000" the text re-centers correctly. I just prefer the text to be positioned correctly in the first place. I could pad with zeros, but I prefer not to. String.Format isn't an option either as it will only force correctly if the string is preceded by a character rather than a space. So it seems that DrawString is trimming even though I set it not to.

The text auto-sizes based off the size of the control. If it is small enough that the text is too long then the progress bar is so small that the text would be illegible any way so that isn't a concern.


-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Are you sure you're not trimming the string elsewhere? I just ran this example on a test form. As I increase the number of leading spaces, the text appears less centered on the form (which is what you want).

Code:
        Dim Text As String = "               hello world 12345 6789 10 11 12 13 14 15 16 17 18 19 20"
        Dim rect As New System.Drawing.RectangleF(Me.ClientRectangle.X, Me.ClientRectangle.Y, Me.ClientRectangle.Width, Me.ClientRectangle.Height)
        Dim strFmt As New System.Drawing.StringFormat

        strFmt.LineAlignment = Drawing.StringAlignment.Near 'Far = Top, Near = Bottom
        strFmt.Alignment = Drawing.StringAlignment.Center
        strFmt.Trimming = Drawing.StringTrimming.None

        e.Graphics.DrawString(Text.PadLeft(9), Me.Font, New System.Drawing.SolidBrush(Me.FontColor), rect, strFmt)
 
I'm sure. When I break on the DrawString line and check it, it shows as correct. I tried again and it does work correctly on static text, just not on variable text.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
It seems likely the problem is with the font. The white space is half the size of the text and it will not add double character for padding. So, it seems like it is padding just not enough. I'm going to play around and try calculating the string so that it will do two white spaces for every actual character missing from the string.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Works but doesn't. The white space is a strange size so either the string is to long or it is to short. I think the only way I can do it then is to actually offset the whole rectangle for the missing characters.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
So here is how I had to fix it.

Code:
        Dim ActualSizeF As System.Drawing.SizeF
        Dim NeededSizeF As System.Drawing.SizeF
        Dim strFmt As New System.Drawing.StringFormat

        strFmt.LineAlignment = Drawing.StringAlignment.Near 'Far = Top, Near = Bottom
        strFmt.Alignment = Drawing.StringAlignment.Center
        strFmt.Trimming = Drawing.StringTrimming.None

        ActualSizeF = Me.CreateGraphics.MeasureString(text, Me.Font, New System.Drawing.SizeF(Me.ClientSize), strFmt)
        NeededSizeF = Me.CreateGraphics.MeasureString(text.PadLeft(padNum, "0"), Me.Font, New System.Drawing.SizeF(Me.ClientSize), strFmt)

        Dim rect As New System.Drawing.RectangleF((NeededSizeF.ToSize.Width - ActualSizeF.ToSize.Width), Me.ClientRectangle.Y, Me.ClientRectangle.Width - (NeededSizeF.ToSize.Width - ActualSizeF.ToSize.Width), Me.ClientRectangle.Height)

        sender.DrawString(text.PadLeft(9), Me.Font, New System.Drawing.SolidBrush(Me.FontColor), rect, strFmt)

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Oops, changed text.PadLeft(9) to just text.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top