OnPaint TextBox Проблема - - PullRequest
       4

OnPaint TextBox Проблема -

0 голосов
/ 20 января 2020

У меня возникла проблема при использовании события OnPaint в TextBox. Всякий раз, когда я щелкаю мышью по текстовой области или начинаю печатать, фон текстового поля снова становится белым. Есть идеи, что может вызвать это?

enter image description here

Вот мой код:

    protected override void OnCreateControl() {
        base.OnCreateControl();
        SetStyle(ControlStyles.OptimizedDoubleBuffer
            | ControlStyles.DoubleBuffer 
            | ControlStyles.AllPaintingInWmPaint
            | ControlStyles.ResizeRedraw
            | ControlStyles.UserPaint, true);
    }

    protected override void OnPaintBackground(PaintEventArgs e) {
        SolidBrush bBrush = new SolidBrush(Enabled ? BackColor : Color.LightGray);
        e.Graphics.FillRectangle(bBrush, ClientRectangle);
        bBrush.Dispose();
    }

    protected override void OnPaint(PaintEventArgs e) {
        base.OnPaint(e);
        using (Brush aBrush = new SolidBrush(ForeColor)) {
            StringFormat sf = new StringFormat();
            switch (TextAlign) {
                case HorizontalAlignment.Center:
                    sf.Alignment = StringAlignment.Center;
                    break;
                case HorizontalAlignment.Left:
                    sf.Alignment = StringAlignment.Near;
                    break;
                case HorizontalAlignment.Right:
                    sf.Alignment = StringAlignment.Far;
                    break;
            }
            string text = UseSystemPasswordChar && !Enabled
                ? new StringBuilder(Text.Length).Insert(0, PasswordChar.ToString(), Text.Length).ToString()
                : Text;
            e.Graphics.DrawString(text, (Font)Font.Clone(), aBrush, ClientRectangle, sf);
            aBrush.Dispose();
        }
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...