简体   繁体   中英

C# Change usercontrol labeltext and background color

My problem is simple. I want to click a panel in Form1 that will cause label1 in a userControl1 , which will be placed upon form2 to change to "Text".

Clicking this panel would also change the background color of said userControl1 . I receive the error "'TileInterFaceTest.Usercontrol1.label1' due to its protection level" which frankly baffles me.

Even running the color change code separately it simply doesn't achieve the desired result.

To be clear, I'm quite a novice when it comes to C# and programming. I've been working with Visual Basic until now so the concept of classes, methods and objects are slightly confusing to me.

Here is my code for Form1 :

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {


        public Form1()
        {
            InitializeComponent();
        }

        private void panel1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            UserControl1 userControl1 = new UserControl1();
            form2.Show();
            userControl1.BackColor = System.Drawing.Color.Red;
            userControl1.LabelText = "Text";

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

Code for UserControl1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class UserControl1 : UserControl
    {
        public String LabelText
        {
            get
            {
                return label1.Text;
            }
            set
            {
                label1.Text = value;
            }
        }

        public UserControl1()
        {

            InitializeComponent();
        }

        private void UserControl1_Load(object sender, EventArgs e)
        {

        }
    }
}

label1 is a private field, which means you cannot access it outside of the class UserControl1 .

What you could do is add a public property in the definition of the class UserControl1 :

public String LabelText {
    get {
        return label1.Text;
    }
    set {
        label1.Text = value;
    }
}

Then use this property to modify the value of the Text of label1 :

private void panel1_Click(object sender, EventArgs e)
{
    form2.Show();
    userControl1.BackColor = System.Drawing.Color.Red;
    userControl1.LabelText = "Text";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM