1 year ago

#127300

test-img

M.J

OnPaint event doesn't fire in c# custom control Updated

firstly I tried many solutions I found it here but it's still doesn't work!

I have a custom textbox control but the OnPaint event doesn't fire:

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace MyMainApp._MyTools
{
    public partial class Joul_TextBox : TextBox
    {
        //Constructor
        public Joul_TextBox()
        {
            InitializeComponent();
            //General Properties
            RightToLeft = RightToLeft.Yes;
            Size = new Size(250, 24);
            MaxLength = 50;

            //Colors
            BackColor = Color.WhiteSmoke;
            ForeColor = Color.FromArgb(64, 64, 64);

            //Borders
            BorderStyle = BorderStyle.FixedSingle;
            BorderColor = Color.Color3;
        }


        #region Fields

        bool _IsFocus;
        

        #endregion


        #region Event

        protected override void OnEnter(EventArgs e)
        {
            base.OnEnter(e);
            _IsFocus = true;
            this.Invalidate();
        }

        protected override void OnLeave(EventArgs e)
        {
            base.OnLeave(e);
            _IsFocus = false;
            this.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            if (_IsFocus)
            {
                this.BackColor = Color.FromArgb(60, 60, 60);
                this.ForeColor = Color.White;
                this.Font = new Font("Tahoma", 10F, FontStyle.Bold);
            }
        }

        #endregion
    }
}

I called invalidate but still doesn't work, can you help me, please. I say again that I searched here and applied all solutions but still doesn't work.

thanks

Update:

before applying SetStyle(ControlStyles.UserPaint, true); :

enter image description here

After:

enter image description here

c#

custom-controls

onpaint

0 Answers

Your Answer

Accepted video resources