-4

I am already able to change the back color of other controls like Label, text color etc during runtime. But when I try to change the color of the main UserControl (the control selected in the pic below), I get an error:

property or indexer control.DefaultBackColor cannot be assigned to -- it is read-only

UPDATE-1: In the Pic, I just want to show that I want to change BackColor. But when I try to use the code, InfoBox.BackColor = System.Drawing.Color.FromArgb(255, 214, 164, 143); then I get another error

an object reference is required to access non-static member

How can I change the color of it?

enter image description here

skm
  • 5,015
  • 8
  • 43
  • 104
  • 1
    use `BackColor` and not `DefaultBackColor` – Martin Verjans May 28 '18 at 12:30
  • You've entirely changed the question, but for the *new* question you can find helpful information here: https://stackoverflow.com/questions/498400/cs0120-an-object-reference-is-required-for-the-nonstatic-field-method-or-prop Basically you're mixing static and non-static code. What is `InfoBox` in that one line of code you're showing? Where is that one line of code executing? There isn't enough information in your question, but by Googling the error message you can find a lot of helpful information about the error. – David May 28 '18 at 12:42

2 Answers2

2

property or indexer control.DefaultBackColor cannot be assigned to -- it is read-only

Because you're trying to set the DefaultBackColor property, which is indeed read-only. In the property pane you're showing, the property is called BackColor. Set that one instead.

David
  • 208,112
  • 36
  • 198
  • 279
  • I tried to use `BackColor` but then I get an error, `an object reference is required to access non-static member`. But I do not get this error if I change the BackColor of other controls. – skm May 28 '18 at 12:34
  • @skm: Clearly you've made a mistake then. Somewhere you're attempting to statically reference an instance property, and you can't do that. So far you've asked how to set the `.BackColor` property on an object. You do that by setting the `.BackColor` property on an object (not a different property). If you're experiencing a different problem and have some code which demonstrates that problem, perhaps you could ask a Stack Overflow question about it... – David May 28 '18 at 12:36
1

There is a trivial solution to my problem.

this.BackColor= System.Drawing.Color.FromArgb(x,x,x,x);

skm
  • 5,015
  • 8
  • 43
  • 104