c# - Changing the BackColor of a Windows Form -


Why can not I change the backcolor of my form in this form?

  MainForm BackColor = System.Drawing.Color.Black;   

In this console I get:

  requires an object reference to non-static field, method, or property 'System.Windows. forms.Control.BackColor.get '(CS0120)    

are classes that are not Instantiated Static classes are static methods or static properties (or both) when you use a line like this:

  The mainform. Backside = System Drawing Collar Black; // & lt; Class name & gt; & Lt; Property & gt;   

What the first C # compiler does is for the local class variable called

MainForm . Since there was no one, it was seen outside of your local area and got its own code named "Windows.Form class MainForm .

Then it was seen to see that there is a fixed asset in class MainForm which is called BackColor . Compiler said, "Oh look, BackColor is a property called , but its not static ", which is when the compiler complains to you and threw you the error.

To change this, this.BackColor , the compiler knew that you wanted to set the background color of this direction of the mainform, which is this Or, by default, form1 :

  this.BackColor = System.Drawing.Color.Black; // & lt; This example & gt; & Lt; Property & gt;   

And as a side note, the keyword this is not required except "This object" is assumed that you can do the same:

  backallore = system. Drawing Collar Black; // & lt; This example & gt; & Lt; Property & gt;   

Hope that makes more sense!

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -