zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    C/C++
    不使用判断语句求一组数中的奇数/偶数个数
    heap(堆)
    One-Hot Encoding(独热编码)
    泰坦尼克号生存预测
    LaTeX 使用笔记
    python学习 —— seaborn、matplotlib、pandas、numpy package的混合使用
    Python 读取本地*.txt文件 替换 内容 并保存
    MySQL基本命令脚本
    MySQL简介
  • 原文地址:https://www.cnblogs.com/dodui/p/1916313.html
Copyright © 2011-2022 走看看