I am learning the C # language, also using the VisualStudio tool, and I can not pass a variable (BtnColor in the code) from one class to another, and then perform an action from the value of that variable. This is the form I try to create in VisualStudio:
The idea is that when you click on the first button, change the color of the button to red, and where it says "None Selected" (label1) the word "red" appears. This is the Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace A___CambiarColorBotones
{
public partial class Form1 : Form
{
protected string BtnColor;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
button1.BackColor = Color.Red;
BtnColor = "red";
CambiarColorBoton colorbtn = new CambiarColorBoton();
colorbtn.CambiarColor(BtnColor);
}
}
public class CambiarColorBoton:Form1
{
string c;
public void CambiarColor(string BtnColor)
{
c = BtnColor;
}
public void BtnColorCambiar()
{
if (c == "red")
{
label1 = "Rojo";
}
}
}
With this code, the error that comes up is: CS0122 - 'Form.label1' is not accessible due to its level of protection.