.net 继承的控制权赢得';更改视觉属性后,无法刷新
我有一个继承标签的MLable。它的底色是红色的。我可以在红色背景的表单上使用它。但当我想将背景从MLabel customcontrol更改为黑色时,已经添加的标签并没有效果。只有新MLabel的背景是黑色,其他的是红色。什么事 我应该一个一个地换吗 示例代码: MLabel.cs.net 继承的控制权赢得';更改视觉属性后,无法刷新,.net,c#-4.0,.net,C# 4.0,我有一个继承标签的MLable。它的底色是红色的。我可以在红色背景的表单上使用它。但当我想将背景从MLabel customcontrol更改为黑色时,已经添加的标签并没有效果。只有新MLabel的背景是黑色,其他的是红色。什么事 我应该一个一个地换吗 示例代码: MLabel.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using Syste
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);
}
}
}
表格1.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;
}
}
名称空间TestCControl
{
部分类Form1
{
///
///必需的设计器变量。
///
private System.ComponentModel.IContainer components=null;
///
///清理所有正在使用的资源。
///
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
///
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
///
私有void InitializeComponent()
{
this.customControl11=新的TestCControl.CustomControl1();
this.customControl12=新的TestCControl.CustomControl1();
这个.SuspendLayout();
//
//客户控制11
//
this.customControl11.AutoSize=true;
this.customControl11.BackColor=System.Drawing.Color.Black;
this.customControl11.Location=新系统.图纸.点(50,23);
this.customControl11.Name=“customControl11”;
this.customControl11.Size=新系统.图纸.尺寸(86,13);
this.customControl11.TabIndex=0;
this.customControl11.Text=“customControl11”;
//
//客户控制12
//
this.customControl12.AutoSize=true;
this.customControl12.BackColor=System.Drawing.Color.Maroon;
this.customControl12.Location=新系统.图纸.点(140,74);
this.customControl12.Name=“customControl12”;
this.customControl12.Size=新系统.图纸.尺寸(86,13);
this.customControl12.TabIndex=1;
this.customControl12.Text=“customControl12”;
//
//表格1
//
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=新系统.Drawing.Size(284262);
this.Controls.Add(this.customControl12);
this.Controls.Add(this.customControl11);
this.Name=“Form1”;
this.Text=“Form1”;
此选项为.resume布局(false);
这个。执行布局();
}
#端区
私人CustomControl1 customControl11;
私人CustomControl1 customControl12;
}
}
如您所见,其中一个自定义控件的背景颜色为黑色,另一个为栗色。对MLabel的更改不会影响以前在winform上添加的控件。这里有几点需要检查 你所有的MLabel都指向同一个版本吗?(可能) 如何设置MLabel控件的背景,是否更改为默认背景色 如何更改现有的MLabel,是在代码中手动设置它们,还是依赖默认的背景色 此外,请检查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;
请提供一些更详细的信息和示例代码,说明您已经做了什么和正在尝试做什么。创建一个自定义控件并从Label继承,将其命名为MLabel。将其背景色从Designer更改为红色。在WinForm上使用它。然后返回MLabel.cs并将其背景色更改为红色。编译。添加到winform。您将看到第一个MLabel背景为红色,另一个为黑色。我知道错了吗?或者所有的标签背景都必须变为黑色?如果你想改变所有标签的背景颜色,那么它们都必须使用相同的颜色变量,而不是相同的值…它们都指向相同的版本。从Visual Studio designer更改背景色。如果我从代码中修改它,一切正常。当我检查forms.Designer.cs文件时,以前添加的MLabel背景色仍然保持红色。更改为黑色后不会生效。同样,我正在更改Visual Studio Designer的背景色。我刚刚在检查列表中添加了一个“也”,但没有看到任何听起来可能是您的问题的代码。在设计器中更改背景色时,它会在设计器代码中设置背景色。此显式集覆盖控件的任何默认颜色。没有额外代码。我在做设计师设计的东西。我唯一能写的可能是:public分部类MLabel:LabelYes,但当您从设计器中更改内容时,这些更改会反映在生成的代码中。我认为一些示例代码在这一点上会非常有用。我添加了示例代码。如您所见。customControl11.BackColor=System.Drawing.Color.Black;而this.customControl12.BackColor=System.Drawing.Color.Maroon;customControl11首先添加了B