blinkor12-6317 avatar image
0 Votes"
blinkor12-6317 asked XingyuZhao-MSFT commented

VB.NET Break State when Changing Font

I created a custom control that has a label in it with VB.NET.

I dragged this control onto my designer and want to update its font (with a font file stored in the application) when the form loads. This works fine for a bit, but whenever I interact with the control (such as changing the text or clicking it), it throws a Parameter Not Valid exception and forces the application to enter a break state.

My code changes the font of a specific control on the form when it loads. It is shown below. Note that the font file is saved in \assets\fonts\roboto.ttf and this code is executed when the form loads (it doesn't work if I embed it in the custom control itself)

'Note that System.Drawing.Text is imported.

Private Sub mainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim pfc As New PrivateFontCollection()
    Dim fontCollectionBaseDIR As String = Application.StartupPath & "\assets\fonts\"
    pfc.AddFontFile(fontCollectionBaseDIR & "roboto.ttf")
    Me.TheCustomControl.Label1.Font = New Font(pfc.Families(0), 10.8, FontStyle.Bold)

End Sub

I have to set these fonts when the application is running from a local file because I cannot be sure that the font is installed on the user's system.


  Message=Parameter is not valid.
   at System.Drawing.FontFamily.GetName(Int32 language)
   at System.Drawing.FontFamily.get_Name()
   at System.Windows.Forms.Internal.WindowsFont.FromFont(Font font, WindowsFontQuality fontQuality)
   at System.Windows.Forms.Internal.WindowsGraphicsCacheManager.GetWindowsFont(Font font, WindowsFontQuality fontQuality)
   at System.Windows.Forms.TextRenderer.DrawText(IDeviceContext dc, String text, Font font, Rectangle bounds, Color foreColor, TextFormatFlags flags)
   at System.Windows.Forms.Label.OnPaint(PaintEventArgs e)
   at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
   at System.Windows.Forms.Control.WmPaint(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.Label.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

This error usually occurs when I try to modify the control's element (such as changing something else) or clicking it. When the form first loads, the custom control's font changes correctly. However, when the control is interacted with, it forces the application into a break state.

· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I cannot reproduce this, with a minimal User Control with just a Label inside and no code (just code to create the Label)
(tested with Roboto-BoldItalic.ttf font I downloaded from Google)
(VS 2019, .NET 4.7.2, Windows 10 1909)
(the font can also be embedded as resource)

0 Votes 0 ·

Does it work in this case?

 Private pfc As PrivateFontCollection
 Private Sub mainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
     pfc = New PrivateFontCollection()
     . . .
 End Sub

0 Votes 0 ·

Hi @blinkor12-6317 ,
I make a test based on your code.
After setting Font property of the custom control to a font which has been disposed, I can reproduce the problem:
Please check your Dispose method, and make sure that the font which has been disposed is not used.
If I have any misunderstanding, please show more details of your error code.
We are waiting for your update.

0 Votes 0 ·
1.png (177.4 KiB)

Hi @blinkor12-6317 ,
Do you still meet this issue? Please let me know if you need further help.

0 Votes 0 ·

I’ve been commenting out the disposed line and have been testing it for the past several days. I’ll let you know in a few hours. @XingyuZhao-MSFT

0 Votes 0 ·

Hi @blinkor12-6317 ,
How's the question going? Has the problem been solved?

0 Votes 0 ·

0 Answers