3

I'm trying to set the backColor of a text box like this:

txtCompanyName.BackColor = Drawing.Color.WhiteSmoke;

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

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

Now that works, but it irritates me that I have to type System. I'm referencing System at the top of my code with using System; Shouldn't that do the trick so I don't have to type System in front of drawing, not sure why I still have to type System, anybody know?

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
user1202606
  • 1,160
  • 9
  • 23
  • 37
  • possible duplicate: http://stackoverflow.com/questions/9433012/why-cant-i-write-io-directory-getfiles – nawfal Mar 13 '12 at 19:45

4 Answers4

5

In C# you can't specify a type name by means of a partial namespace. In C# the name must either be

  • A fully qualified name including the entire namespace or namespace alias
  • A type name only

The Drawing portion of Drawing.Color.WhiteSmoke is a non-fully qualified namespace and hence illegal as a type name. You either need to add the prefix System or add a using System.Drawing and change the type name to Color.WhiteSmoke

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

using Drawing = System.Drawing;

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

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
1

Easily fixed:

using Drawing = System.Drawing;

...

txtCompanyName.BackColor = Drawing.Color.WhiteSmoke;
Roman Starkov
  • 59,298
  • 38
  • 251
  • 324
1

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

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

using Drawing = System.Drawing;

which would allow you to refer to the System.Drawing namespace as 'Drawing'. This is probably not the best solution though. Really you should just use:

using System.Drawing;

your line then becomes:

txtCompanyName.BackColor = Color.WhiteSmoke;

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

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

then your line is:

txtCompanyName.BackColor = WinColor.WhiteSmoke;
Dave Cousineau
  • 12,154
  • 8
  • 64
  • 80
0

You're using system, but you aren't using System.Drawing. Add using System.Drawing; to the rest of your using statements.

yoozer8
  • 7,361
  • 7
  • 58
  • 93