System.drawing link

I am trying to set the backColor of a textbox like this:

txtCompanyName.BackColor = Drawing.Color.WhiteSmoke;

      

It doesn't like it because it wants me to add System in front, for example:

txtCompanyName.BackColor = System.Drawing.Color.WhiteSmoke;

      

This now works, but it annoys me that I have to type System. I am referencing the system at the top of my code using System; Shouldn't that do the trick, so I don't need to type the system before drawing, not sure why I still need to type System, does anyone know?

+3


source to share


4 answers


In C #, you cannot specify a type name using a partial namespace. In C #, the name should be

  • Fully qualified name, including full namespace or namespace alias
  • Type name only

The part Drawing

Drawing.Color.WhiteSmoke

is an incomplete namespace and therefore illegal as a type name. You either need to add a prefix System

or add using System.Drawing

and change the type name toColor.WhiteSmoke



Alternatively, you can create an alias for the namespace System.Drawing

named Drawing

.

using Drawing = System.Drawing;

      

It is legal to use an alias as the start of a type name in C #.

+5


source


Easily fixed:



using Drawing = System.Drawing;

...

txtCompanyName.BackColor = Drawing.Color.WhiteSmoke;

      

+1


source


The using statement imports types that are in the specified namespace; this does not include child namespaces.

If you really wanted to, you could use the following line:

using Drawing = System.Drawing;

      

which will allow you to refer to the System.Drawing namespace as "Drawing". This is probably not the best solution. Indeed, you should simply use:

using System.Drawing;

      

your line will look like this:

txtCompanyName.BackColor = Color.WhiteSmoke;

      

if you need to disambiguate between System.Drawing.Color and some other Color class (e.g. Microsoft.Xna.Framework.Color) you can use strings like this:

using XNAColor = Microsoft.Xna.Framework.Color;
using WinColor = System.Drawing.Color;

      

then your line:

txtCompanyName.BackColor = WinColor.WhiteSmoke;

      

+1


source


You are using the system, but you are not using System.Drawing. Add using System.Drawing;

your operators to the rest.

0


source







All Articles