我有一个继承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上添加的控件。
请提供一些更多的细节和示例代码,显示您已完成并正在尝试执行的操作。 – 2012-02-24 14:59:21
创建自定义控件并从Label继承,将其命名为MLabel。将其从设计器更改为红色。在WinForm上使用它。返回到MLabel.cs并将它的背景颜色更改为红色。编译。添加到winform。你会看到第一个MLabel背景红色,另外一个是黑色。我知道错误或所有的MLabels背景必须变成黑色吗? – Murat 2012-02-24 15:07:13
如果你想让所有的标签背景颜色都改变,那么它们都必须使用相同的颜色变量,而不是相同的值... – 2012-02-24 15:07:55