The property or indexer "Color.Name" can not be assigned because it is read-only

2

Here is the code where I define SegundaTela :

public partial class Form1 : Form
{
    SegundaTela telaSecundaria;
}

Code:

telaSecundaria = new SegundaTela();
telaSecundaria.label_segunda_tela.BackColor.Name = cor_fundo;

I get error:

  

CS0200 The property or indexer "Color.Name" can not be   because it is read-only

Any solution?

    
asked by anonymous 05.12.2017 / 03:54

2 answers

4

The error is self descriptive, can not rename the color. Why do you want to change her name? This does not make sense.

If you want to change the color, just change the color, do not change its name. The solution is to not do this.

Documentation .

Then you can do:

telaSecundaria.label_segunda_tela.BackColor = cor_fundo;

I'm assuming that cor_fundo is a variable of type Color ". Otherwise this would give error because the property BackColor expect this. If it is a string with the name you can use the

05.12.2017 / 04:14
2

If you want to change the color of label . The property you need to work on is ForeColor and if you want to change the background color, you are not assigning the Name you will get, just assign the value of the struct Color the BackColor property:

Here's how it would look:

telaSecundaria = new SegundaTela();
telaSecundaria.label_segunda_tela.BackColor = Color.Red;
telaSecundaria.label_segunda_tela.ForeColor = Color.White;
    
05.12.2017 / 11:21