Enabled设置为False时,前景色和背景色也不改变的TextBox 并居中

1.公共方法

 

    public class MyTextBox : TextBox
    {
        
protected override void OnEnabledChanged(EventArgs e)
        {
            
base.OnEnabledChanged(e);
            
//设置Enabled为False时
            if (this.Enabled)
            {
                
this.SetStyle(ControlStyles.UserPaint, false);
               
            }
            
else
                
this.SetStyle(ControlStyles.UserPaint, true);
            
//再描绘
            this.Invalidate();
        }

        
//描绘TextBox
        protected override void OnPaint(PaintEventArgs e)
        {
            
base.OnPaint(e);
            System.Drawing.Brush b 
=
                
new System.Drawing.SolidBrush(this.ForeColor);

            StringFormat sf 
= new StringFormat();
            sf.LineAlignment 
= StringAlignment.Center;
            sf.Alignment 
= StringAlignment.Center;
            
//描绘字符串
            
//e.Graphics.DrawString(this.Text, this.Font, b, -1, 1);
            e.Graphics.DrawString(this.Text, this.Font, b, this.ClientRectangle, sf);

            b.Dispose();
        }
    }

 

 

2.页面 designer.cs修改为 MyTextBox

posted @ 2010-12-24 19:40  DODUI  阅读(690)  评论(0编辑  收藏  举报