0

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!

4

1 回答 1

0

将字符串移出到类级别,以便您可以更改它:

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
于 2013-11-08T17:03:38.673 回答