简体   繁体   English

C#:尝试更改用户控件文本框和背景色

[英]C# : Trying to change a usercontrol textbox and background color

My problem is simple. 我的问题很简单。 I want to click a panel in Form1, this will cause label1 in a userControl1 which is placed upon form2 to change to "Text". 我想单击Form1中的面板,这将导致放置在form2上的userControl1中的label1更改为“文本”。

Clicking this panel would also change the background color of said userControl1. 单击此面板还将更改所述userControl1的背景颜色。 I receive the error "'TileInterFaceTest.Usercontrol1.label1' due to its protection level" which frankly baffles me. 我收到错误“'TileInterFaceTest.Usercontrol1.label1',由于其保护级别”,这使我感到困惑。

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 in general. 需要明确的是,对于C#和一般编程,我还是一个新手。 I've been working with Visual Basic until now so the concept of classes, methods and objects are slightly confusing to me. 到目前为止,我一直在使用Visual Basic,因此类,方法和对象的概念对我来说有些混乱。

Here is my 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 TileInterFaceTest
{
    public partial class Form1 : Form
    {
        public UserControl1 userControl1 = new UserControl1();
        public Form2 form2 = new Form2();

        public Form1()
        {
            InitializeComponent();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void panel1_DoubleClick(object sender, EventArgs e)
        {

        }



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


    }
}

First off, your code as shown doesn't even show the usercontrol on the first form! 首先,您显示的代码甚至不会在第一种形式上显示用户控件! Thats probably why your color change code didn't do as you expected. 这可能就是为什么您的颜色更改代码未按预期执行的原因。 Simply writing: 简单地写:

public UserControl1 userControl1 = new UserControl1();

Just declares a public field (bad style!) and sets it to a new instance of your user control. 只需声明一个公共字段(错误的样式!)并将其设置为用户控件的新实例即可。 It doesn't put it on the rendered UI . 它不会将其放在呈现的UI上

To fix that part, you need to add it to your form somewhere. 要修复该部分,您需要将其添加到表单中的某个位置。 Say... in your constructor or the Loaded event: 在构造函数或Loaded事件中说...:

Controls.Add(userControl1);

Note also that this puts it on Form1 . 还要注意,这会将它放在Form1 If you want it on Form2 , then that form needs to create the control and add it to its Controls collection, and expose it as a property (see below). 如果要在Form2它,则窗体需要创建控件并将其添加到其Controls集合中,并将其公开为属性(请参见下文)。 The other problem is here: 另一个问题在这里:

userControl1.label1 = "Text";

Presumably label1 is a label control on that user control. 大概label1是该用户控件上的标签控件。 First off, controls are private members of the user control, you can't just access them from somewhere else without exposing them first! 首先,控件是用户控件的私有成员,您不能只从其他地方访问它们而不先暴露它们! You need to add something like this to your user control: 您需要在用户控件中添加以下内容:

public Label Label1 { get { return label1; } }

Note the use of a property (correct style!). 注意使用属性(正确的样式!)。 Now you can write this: 现在,您可以编写以下代码:

userControl1.Label1.Text = "Text";

Note I fixed the final mistake in that line, as you can't set a Label object to a string, it just doesn't make sense. 注意,我修复了该行中的最后一个错误,因为您不能将Label对象设置为字符串,但这没有任何意义。 You need to modify its Text property instead. 您需要改为修改其Text属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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