skip to Main Content

I have the following error Font is a namespace but is used like a type error
when running this code:

namespace Font
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void fontFamily_MS_CheckedChanged(object sender, EventArgs e)
        {
            sampleText.Font = new Font("Microsoft Sans Serif");  
        }
    }
}

I believe it’s because it’s due the namespace as the top is named Font.
But I’m not sure how to fix it?

It’s my first-time programming in C# using Visual Studio, so I’m not really sure on how to go about fixing it.

2

Answers


  1. You must use "using System.Drawing;"

    Login or Signup to reply.
  2. It appears that your default namespace is Font, which indicates that you named your project "Font", which is a really bad name. Your project is not a font, so don’t name it "Font". Use descriptive names for everything, including your projects, and you’re less likely to have name clashes like this.

    Now that that’s already done, unless you want to create a new project, you can change the default namespace in the project properties and then change the namespace in all your code files. VS should prompt you to change them I think, so you can accept that and let VS do it for you. I’m not 100% sure about that though, because I use ReSharper and it might be just that doing it for me. Regardless, you can do a Replace in Files to find "namespace Font" and replace it with the new namespace name.

    If you don’t want to do that and leave all your current naming in place, you need to qualify the Font type:

    sampleText.Font = new System.Drawing.Font("Microsoft Sans Serif");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search