I've downloaded several custom fonts. When the user presses a button I want the font of the text in a rich-textbox to change. But the fonts themselves don't change but the font size does. I also don't get any errors. When I install the font on my PC the font works perfectly. I can then also use it as the default font using the VS properties editor and can assign the font using both the codes below. But when I de-install the font it stops working again. It seems as if VS doesn't recognize the font even when using a direct directory link and just skips over/ignores the font part and just adjusts the size of the normal font. I have it working on labels but not on rich-textboxes.
I've looked at a lot of related questions on this forum and elsewhere but none of them worked. I would like to use custom fonts that the user doesn't have to install. I've also put the fonts in the resources using Resources.resx and set them to public.
Sorry if this is a stupid or easily solved question but I just can't get it to work on rich/textboxes.
What I have tried:
This is what I've tried so far:
PrivateFontCollection pfc = new PrivateFontCollection();
pfc.AddFontFile(@"C:\\Users\\MyName\\Documents\\Font\\6937\\HARRYP.ttf");
System.Drawing.Font font = new Font(pfc.Families[0], 12);
richTextBox_Translate.Font = font;
And any permutation of this:
richTextBox_Translate.Font = new Font(@"C:\\Users\\Myname\\Documents\\Font\\6937\\HARRY P", 12);
richTextBox_Translate.Font = new Font(@".\\Resources\\HARRY P", 12);
richTextBox_Translate.Font = new Font("HARRY P", 12);
The font does change when I use a standard font±
richTextBox_Translate.Font = new Font("Comic Sans MS", 12);
And I found (several) of these but they don't work either:
[DllImport("gdi32.dll")]
private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont, IntPtr pdv, [In] ref uint pcFonts);
PrivateFontCollection pFC = new PrivateFontCollection();
private void LoadFont()
{
Stream fontStream = new MemoryStream(Properties.Resources.DBSScouter);
System.IntPtr data = Marshal.AllocCoTaskMem((int)fontStream.Length);
Byte[] fontData = new Byte[fontStream.Length];
fontStream.Read(fontData, 0, (int)fontStream.Length);
Marshal.Copy(fontData, 0, data, (int)fontStream.Length);
uint cFonts = 0;
AddFontMemResourceEx(data, (uint)fontData.Length, IntPtr.Zero, ref cFonts);
pFC.AddMemoryFont(data, (int)fontStream.Length);
fontStream.Close();
Marshal.FreeCoTaskMem(data);
System.Drawing.Font font = new Font(pFC.Families[0], 12);
richTextBox_Translate.Font = font;
}