2012-02-24 101 views
1

我有一个继承Label的MLable。它的背景是红色的。我可以在红色背景的表单上使用它。但是当我想将背景从MLabel自定义控件更改为黑色时,已添加的贴图不起作用。只有新的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; 
    } 
} 

正如你所看到的一个自定义控件有黑色的其他mar背影。 MLabel上的更改不会影响以前在winform上添加的控件。

+0

请提供一些更多的细节和示例代码,显示您已完成并正在尝试执行的操作。 – 2012-02-24 14:59:21

+0

创建自定义控件并从Label继承,将其命名为MLabel。将其从设计器更改为红色。在WinForm上使用它。返回到MLabel.cs并将它的背景颜色更改为红色。编译。添加到winform。你会看到第一个MLabel背景红色,另外一个是黑色。我知道错误或所有的MLabels背景必须变成黑色吗? – Murat 2012-02-24 15:07:13

+0

如果你想让所有的标签背景颜色都改变,那么它们都必须使用相同的颜色变量,而不是相同的值... – 2012-02-24 15:07:55

回答

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删除设置背景色的线。这些都明确地设置背景颜色,并且不允许使用默认颜色

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

它们都引用相同的版本。 从Visual Studio设计器更改BackColor。如果我从代码中改变它,一切正常。 当我检查forms.Designer。cs文件,以前添加的MLabels BackColor仍然保持红色。改为黑色后不会生效。再次,我从Visual Studio Designer更改BackColor。 – Murat 2012-02-24 15:09:57

+0

我刚刚添加了一个也到我的检查列表,没有看到任何代码,听起来可能是你的问题。当您更改设计器的背景色时,它会将其设置在设计器代码中。该显式设置将覆盖控件的任何默认颜色。 – 2012-02-24 15:13:21

+0

没有额外的代码。我正在从设计师那里做所有这些东西。只有我可以这样写:public partial class MLabel:Label – Murat 2012-02-24 15:16:01