0
public class CustomFont
{
    public string ChangeFont(string font, string target)
    {
        PrivateFontCollection pfc = new PrivateFontCollection();
        int fontLength = Properties.Resources.font.Length;
        byte[] fontdata = Properties.Resources.font;
        System.IntPtr data = Marshal.AllocCoTaskMem(fontLength);
        Marshal.Copy(fontdata, 0, data, fontLength);
        pfc.AddMemoryFont(data, fontLength);
        target.Font = new Font(pfc.Families[0], target.Font.Size);
    }
}

I tried this but errors were shown that 'App.Properties.Resources' does not contain a definition for 'font'.

Okay so I edited the code following everybody's answers and it is almost working now. The problem is, what method should I use for byte[]?

public void ChangeFont(string font, TextBox target)
        {
            PrivateFontCollection pfc = new PrivateFontCollection();
            int fontLength = Properties.Resources.ResourceManager.GetString(font).Length;
            byte[] fontdata = Properties.Resources.font;
            System.IntPtr data = Marshal.AllocCoTaskMem(fontLength);
            Marshal.Copy(fontdata, 0, data, fontLength);
            pfc.AddMemoryFont(data, fontLength);
            target.Font = new Font(pfc.Families[0], target.Font.Size);
        }
  • 1
    If the error says so then I think there's no entry for font in resources. Can you show your resources file ? – mrogal.ski May 31 '17 at 11:32
  • 1
    View the properties window of your project and select the "Resources" section. If it says you have no resources then you need to add one and add the required settings. – Wheels73 May 31 '17 at 11:33
  • 2
    There are much more issues with this code. target is of type string and the string type I know has no Font property. You did not return a result although the method signature say you have to. – Sir Rufo May 31 '17 at 11:56

1 Answers1

0

You are trying to get a string from your resources by name.

You can't write it like

Properties.Resources.font;

You have to use the ResourceManager

string myString = Properties.Resources.ResourceManager.GetString(font);

And then you can do whatever you want with that string.

But your method has a lot of other things why you will get following problems; for example no return value.

Mighty Badaboom
  • 6,067
  • 5
  • 34
  • 51