Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#更改用户控件标签文本和背景颜色_C# - Fatal编程技术网

C#更改用户控件标签文本和背景颜色

C#更改用户控件标签文本和背景颜色,c#,C#,我的问题很简单。我想单击Form1中的一个面板,该面板将导致userControl1中的label1更改为“文本” 单击此面板还将更改所述userControl1的背景色。我收到了错误“TileInterFaceTest.Usercontrol1.label1”,因为它的保护级别“,这让我很困惑 即使单独运行颜色更改代码,也无法达到预期的效果 说清楚了,我在C#和编程方面是个新手。到目前为止,我一直在使用VisualBasic,因此类、方法和对象的概念对我来说有点混淆 这是我的表格1的代码: u

我的问题很简单。我想单击
Form1
中的一个面板,该面板将导致
userControl1
中的label1更改为“文本”

单击此面板还将更改所述
userControl1
的背景色。我收到了错误“TileInterFaceTest.Usercontrol1.label1”,因为它的保护级别“,这让我很困惑

即使单独运行颜色更改代码,也无法达到预期的效果

说清楚了,我在C#和编程方面是个新手。到目前为止,我一直在使用VisualBasic,因此类、方法和对象的概念对我来说有点混淆

这是我的
表格1的代码:

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)
        {

        }
    }
}
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
是一个字段,这意味着您不能在类
UserControl1
之外访问它

您可以在类的定义中添加公共属性
UserControl1

public String LabelText {
    get {
        return label1.Text;
    }
    set {
        label1.Text = value;
    }
}
private void panel1_Click(object sender, EventArgs e)
{
    form2.Show();
    userControl1.BackColor = System.Drawing.Color.Red;
    userControl1.LabelText = "Text";
}
然后使用此属性修改
标签1
文本的值:

public String LabelText {
    get {
        return label1.Text;
    }
    set {
        label1.Text = value;
    }
}
private void panel1_Click(object sender, EventArgs e)
{
    form2.Show();
    userControl1.BackColor = System.Drawing.Color.Red;
    userControl1.LabelText = "Text";
}

谢谢你的回复。但这是我第二次使用完全相同的解决方案,我似乎没有得到任何结果。在类或对象定义方面有什么我可能做得不对的吗?编辑你的问题并发布你正在使用的代码。如果不查看代码,我们将无法帮助您。添加了Form1和UserControl1的代码。我肯定我错过了一些显而易见的东西。我很感激你的耐心。你也应该根据生成的默认值重命名你的变量。我只是想要一些可以工作的东西。在弄清楚如何实际使程序执行期望的结果后,我将适当地重命名变量。但现在似乎有点不必要,我不同意。如果您只是使用生成的代码,当然可以。一旦你开始编写“代码隐藏”,比如事件处理程序,那么是时候使用专有名称了。在你的程序运行后适当地重新命名变量就像在你完成切割后磨刀一样。