Search code examples
c#controls

Is any way to change control's background color without `BackColor` property?


There are some properties for Controls, that needs System.Drawing.dll to be used, i.e.:

control.Size =  new System.Drawing.Size(10, 20);

however, that goal can be achieved without with other workaround (not needed System.Drawing), i.e.:

control.Width = 10;
control.Height= 20;

My question is, can we change control's background color ( .BackColor, which requires System.Drawing) with some workarounds like that? (so, not needed System.Drawing)?


Solution

  • No - The dependency on System.Drawing is for the Color value itself and there are no shortcut properties to, for example, set the component values like you can with Size.

    If you don't want to include System.Drawing inline just add a using directive:

    using System.Drawing;
    
    ...
    
        control.BackColor = Color.Blue;  // Color will be found in System.Drawing via the "using"