Question

Hey all i have the following code that works just fine when my form loads up:

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
    Dim custFont As New PrivateFontCollection()
    Dim solidBrush As New SolidBrush(Color.FromArgb(255, 0, 0, 255))
    Dim string2 As String = "AntiAlias"

    custFont.AddFontFile("C:\aFont.ttf")
    e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias
    e.Graphics.DrawString(string2, New Font(custFont.Families(0), 100, FontStyle.Regular, GraphicsUnit.Pixel), solidBrush, New PointF(10, 60))
End Sub

However, i need a way to update that font text whenever i push a button(s) on the form itself. I tried making a sub like so:

Public Sub changeText(ByVal e As System.Windows.Forms.PaintEventArgs, ByVal theText as string)
    Dim custFont As New PrivateFontCollection()
    Dim solidBrush As New SolidBrush(Color.FromArgb(255, 0, 0, 255))

    custFont.AddFontFile("C:\aFont.ttf")
    e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias
    e.Graphics.DrawString(theText, New Font(custFont.Families(0), 100, FontStyle.Regular, GraphicsUnit.Pixel), solidBrush, New PointF(10, 60))
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    changeText(Me.OnPaint, "just a test")
End Sub

But i end up having an error:

Overload resolution failed because no accessible 'OnPaint' accepts this number of arguments.

on line: changeText(Me.OnPaint, "just a test")

Any help would be great! Thanks!

Was it helpful?

Solution

Move the string out to class level so you can change it:

Imports System.Drawing.Text
Public Class Form1

    Private string2 As String = "AntiAlias"
    Private custFont As New PrivateFontCollection()

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        custFont.AddFontFile("C:\aFont.ttf")
    End Sub

    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        Using solidBrush As New SolidBrush(Color.FromArgb(255, 0, 0, 255))
            Using fnt As New Font(custFont.Families(0), 100, FontStyle.Regular, GraphicsUnit.Pixel)
                e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias
                e.Graphics.DrawString(string2, fnt, solidBrush, New PointF(10, 60))
            End Using
        End Using
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        string2 = "just a test"
        Me.Refresh()
    End Sub

End Class
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top