Coloured text using DrawString

Pete-

New member
Joined
Jan 10, 2004
Messages
2
Location
East Midlands , UK
Hi all

Im trying to add coloured text at X, Y coords on the form. I have 4 text string and each string will be a differant colour.

I want it to display the text together in one line as abcedfghijklmnopqrstuvwx.

The problem Im having is when the text is displayed there are spaces or overlapping between each string abcedf ghijkl mnopstuvwx and I dont know how to solve it.

Any help or if anyone got a better idea on how to do it would be nice :)

Thanks

Code:
Sample Code..

 Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

        Dim myFontBold As New Font("Verdana", 15, FontStyle.Bold)
        Dim Ycoord As Integer = 40
        Dim Xcoord As Integer = 0
       
        Dim StringSizeA As Double
        Dim StringSizeB As Double
        Dim StringSizeC As Double
        Dim StringSizeD As Double

        Dim TextA As String = "acdebf"
        Dim TextB As String = "ghijkl"
        Dim TextC As String = "mnopqr"
        Dim TextD As String = "stuvwx"

        StringSizeA = Me.CreateGraphics.MeasureString(TextA, myFontBold).Width
        StringSizeB = Me.CreateGraphics.MeasureString(TextB, myFontBold).Width
        StringSizeC = Me.CreateGraphics.MeasureString(TextB, myFontBold).Width
        StringSizeD = Me.CreateGraphics.MeasureString(TextD, myFontBold).Width

        Me.CreateGraphics.DrawString(TextA, myFontBold, System.Drawing.Brushes.Blue, Xcoord, Ycoord)
        Me.CreateGraphics.DrawString(TextB, myFontBold, System.Drawing.Brushes.Red, Xcoord + StringSizeA, Ycoord)
        Me.CreateGraphics.DrawString(TextC, myFontBold, System.Drawing.Brushes.Green, Xcoord + StringSizeA + StringSizeB, Ycoord)
        Me.CreateGraphics.DrawString(TextD, myFontBold, System.Drawing.Brushes.Yellow, Xcoord + StringSizeA + StringSizeB + StringSizeC, Ycoord)
        Me.CreateGraphics.Dispose()

    End Sub
 
You should use e.Graphics instead of calling me.CreateGraphics all the time.

Also no need for Me.CreateGraphics.Dispose(), because you create a graphics object, you do nothing with it and then you dispose it.
 
Hi,
How did you sort your problem with the overlapping strings? Im having a similar problem whilst trying to draw a string character by character. My string then appears with big spaces.
My code looks like this...

Code:
For i = 0 To str.Length - 1
        g.DrawString(str.Chars(i), myFont, b, myPoint)
        myPoint.X += g.MeasureString(str.Chars(i), myFont).ToPointF.X
        Next i
 
Back
Top