using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;namespace CSharpWin
{ [ToolboxBitmap(typeof(TextBox))]
public class DataGridExtend : DataGridView
{
public DataGridExtend()
: base()
{
this.BackgroundColor = Color.Blue;
}
}
}上面是一个DataGridExtend 控件.当我把DataGridExtend(控件) 拖拽到新的form里面的时候,
会生成代码this.dataGridExtend1.BackgroundColor = System.Drawing.Color.Blue;
我的目的是不生成这段代码,因为如果我以后改变dataGridExtend的BackgroundColor,
可以直接从基类里面继承,希望大虾指教...
那就不要在构造函数里写this.BackgroundColor = Color.Blue;
# (军军)
大虾,如果不放在构造函数里面应该放在那里呢,希望指点
[DefaultValue(Color.Blue)]
public override BackgroundColor
{
get
{
return base.BackgroundColor;
}
set
{
base. BackgroundColor = value;
}
}
public override BackgroundColor
{
get
{
return base.BackgroundColor;
}
set
{
base. BackgroundColor = value;
}
}
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;namespace WindowsApplication4
{
/// <summary>
/// Summary description for CustomColorCheckBox.
/// </summary>
public class CustomColorCheckBox : CheckBox
{
// Fields
private Color checkColor; public CustomColorCheckBox()
{
this.checkColor = this.ForeColor;
this.Paint += new PaintEventHandler(this.PaintHandler);
} [Description("The color used to display the check painted in the CheckBox")]
public Color CheckColor
{
get
{
return checkColor;
}
set
{
checkColor = value;
this.Invalidate();
}
} private void PaintHandler (object sender, PaintEventArgs pe)
{
if (this.Checked)
{
Point pt = new Point(); if (this.CheckAlign == ContentAlignment.BottomCenter)
{
pt.X = (this.Width / 2) - 4;
pt.Y = this.Height - 11;
}
if (this.CheckAlign == ContentAlignment.BottomLeft)
{
pt.X = 3;
pt.Y = this.Height - 11;
}
if (this.CheckAlign == ContentAlignment.BottomRight)
{
pt.X = this.Width - 11;
pt.Y = this.Height - 11;
}
if (this.CheckAlign == ContentAlignment.MiddleCenter)
{
pt.X = (this.Width / 2) - 4;;
pt.Y = (this.Height / 2) - 4;
}
if (this.CheckAlign == ContentAlignment.MiddleLeft)
{
pt.X = 3;
pt.Y = (this.Height / 2) - 4;
}
if (this.CheckAlign == ContentAlignment.MiddleRight)
{
pt.X = this.Width - 11;
pt.Y = (this.Height / 2) - 4;
}
if (this.CheckAlign == ContentAlignment.TopCenter)
{
pt.X = (this.Width / 2) - 4;
pt.Y = 3;
}
if (this.CheckAlign == ContentAlignment.TopLeft)
{
pt.X = 3;
pt.Y = 3;
}
if (this.CheckAlign == ContentAlignment.TopRight)
{
pt.X = this.Width - 11;
pt.Y = 3;
} DrawCheck(pe.Graphics, this.checkColor,pt);
}
} public void DrawCheck(Graphics g, Color c, Point pt)
{
Pen pen = new Pen(this.checkColor);
g.DrawLine(pen, pt.X, pt.Y + 2, pt.X + 2, pt.Y + 4);
g.DrawLine(pen, pt.X, pt.Y + 3, pt.X + 2, pt.Y + 5);
g.DrawLine(pen, pt.X, pt.Y + 4, pt.X + 2, pt.Y + 6);
g.DrawLine(pen, pt.X + 3, pt.Y + 3, pt.X + 6, pt.Y);
g.DrawLine(pen, pt.X + 3, pt.Y + 4, pt.X + 6, pt.Y + 1);
g.DrawLine(pen, pt.X + 3, pt.Y + 5, pt.X + 6, pt.Y + 2);
}
}
}
这个特性并不能设置属性的默认值,而是当字段的值为Blue时不存储而已
而不是实例的控件,都有自己的backcolor,
从而我可以改变类的backcolor,达到改变所有实例的backcolor的目的
你不想让你的派生类继承基类的BackColor,而是想搞这个类的一个静态的属性BackColor,这样当你更改类的BackColor的时候,想让所有的派生类的对象响应此类的静态属性的Changed事件(暂且这样说),来达到让每个派生类对象都改变BackColor的目的,不知道我理解的对不对;
会生成代码
C# code
this.dataGridExtend1.BackgroundColor = System.Drawing.Color.Blue;我的目的是不生成这段代码,因为如果我以后改变dataGridExtend的BackgroundColor,
可以直接从基类里面继承,希望大虾指教... 在下对这段话不太理解。。
给派生类加一个静态的StaticBackColor属性,然后在代码里修改类的这个属性值,类中维护了所有的实例,然后去foreach遍历,理论上应该是可以的,就是实现起来太麻烦,个人感觉没有必要
不能像css一样去改变。。