(VB) help with drawing changing text directly to form

Discussion in 'Mixed Languages' started by stevemk14ebr, Jul 23, 2011.

  1. stevemk14ebr

    stevemk14ebr MDL Senior Member

    Jun 23, 2010
    267
    48
    10
    I need to draw a string to the form directly on it that has a changing variable in it here is the string: "score:" & score
    what happens though is every time the score changes the event simply paints over the old value and leaves a jumbled mess of numbers 1 over the other i need help on how i would draw text directly to the form with the ability to have a changing variable in it heres my code:

    Public Sub DrawString()
    Dim formGraphics As System.Drawing.Graphics = Me.CreateGraphics()
    Dim drawString As String = String.Format("Score: {0}", score)
    Dim drawFont As New System.Drawing.Font("Arial", 16)
    Dim drawBrush As New _
    System.Drawing.SolidBrush(System.Drawing.Color.Black)
    Dim x As Single = 1.0
    Dim y As Single = 1.0
    Dim drawFormat As New System.Drawing.StringFormat
    formGraphics.DrawString(drawString, drawFont, drawBrush, _
    x, y, drawFormat)
    drawFont.dispose()
    drawBrush.Dispose()
    formGraphics.Dispose()


    End Sub
     
  2. stevemk14ebr

    stevemk14ebr MDL Senior Member

    Jun 23, 2010
    267
    48
    10
    any1 i need help pls
     
  3. stevemk14ebr

    stevemk14ebr MDL Senior Member

    Jun 23, 2010
    267
    48
    10
    ok i got this to work although its not pretty i use this method to draw the text onto the form:

    Protected Overrides Sub OnPaint( _
    ByVal e As System.Windows.Forms.PaintEventArgs)
    Dim g As Graphics = e.Graphics
    g.DrawString("Score:" & score, _
    New Font("Arial", 16), _
    Brushes.Black, 1, 1)
    MyBase.OnPaint(e)

    End Sub

    i then in the form designer tab created a rectangle(named inval) over the area where the text was drawn and then invalidated that area by do this every time the value of "score" went up:

    Dim bmpcontainter As RectangleF
    bmpcontainter = inval.Bounds
    Me.Invalidate(Rectangle.Round(bmpcontainter))

    thats it :D took me long enough :Lighten: