视觉属性更改后,不会刷新继承控件

时间:2012-02-24 14:56:48

标签: .net c#-4.0

我有一个继承Label的MLable。它的背景色是红色的。我可以在红色背景的表格上使用它。但是,当我想从MLabel customcontrol将背景更改为黑色时,已添加的labeles不起作用。只有新的MLabel背景为黑色,其他为红色。什么???

我是否必须逐一更改它们?

示例代码:

MLabel.cs

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

namespace TestCControl
{
    public partial class CustomControl1 : Label
    {
        public CustomControl1()
        {
            InitializeComponent();
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }
    }
}

Form1.cs的

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

namespace TestCControl
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    }
}

Form1.Designer.cs

namespace TestCControl
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.customControl11 = new TestCControl.CustomControl1();
            this.customControl12 = new TestCControl.CustomControl1();
            this.SuspendLayout();
            // 
            // customControl11
            // 
            this.customControl11.AutoSize = true;
            this.customControl11.BackColor = System.Drawing.Color.Black;
            this.customControl11.Location = new System.Drawing.Point(50, 23);
            this.customControl11.Name = "customControl11";
            this.customControl11.Size = new System.Drawing.Size(86, 13);
            this.customControl11.TabIndex = 0;
            this.customControl11.Text = "customControl11";
            // 
            // customControl12
            // 
            this.customControl12.AutoSize = true;
            this.customControl12.BackColor = System.Drawing.Color.Maroon;
            this.customControl12.Location = new System.Drawing.Point(140, 74);
            this.customControl12.Name = "customControl12";
            this.customControl12.Size = new System.Drawing.Size(86, 13);
            this.customControl12.TabIndex = 1;
            this.customControl12.Text = "customControl12";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.customControl12);
            this.Controls.Add(this.customControl11);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private CustomControl1 customControl11;
        private CustomControl1 customControl12;
    }
}

正如您所看到的,其中一个自定义控件具有黑色其他栗色背景色。对MLabel的更改不会影响以前在winform上添加的控件。

1 个答案:

答案 0 :(得分:2)

在这里查看几件事。

你的所有MLabels都指的是同一个版本吗? (可能)

如何设置MLabel控件的背景,是否更改为默认背景颜色?

您如何更改现有的MLabels,是在代码中手动设置它们还是依赖于默认的背景颜色?

另外,检查您的winform代码。如果设计器代码明确设置了MLabel背景颜色,那么这将覆盖控件可能具有的任何默认值。如果是这种情况,那么您将需要删除背景颜色设置以使其使用默认值,或者您必须手动更改每个设置。

如果上述所有内容看起来都是正确的,那么一些代码和/或更多信息在这里会很有用。

看到实际代码后更新了答案:

在CustomControl1中,您需要执行以下操作:

 System.Drawing.Color _backColor = System.
 protected override System.Drawing.Color BackColor 
 {
     get{return _backColor;} 
     set{_backColor = value;}
 }

在Form1.Designer.cs中删除设置BackColor的行。这些是明确设置背景颜色而不允许默认颜色

this.customControl11.BackColor = System.Drawing.Color.Black;