1

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

M.J
  • 143
  • 9
  • 1
    According to the duplicate you need to call `SetStyle(ControlStyles.UserPaint, true);` in the constructor. – Palle Due Feb 02 '22 at 11:12
  • thanks, when I called that SetStyle(ControlStyles.UserPaint, true); some problem happened in the form style, please see the update in my post – M.J Feb 02 '22 at 11:37
  • @PalleDue please see the update, thanks – M.J Feb 02 '22 at 11:49
  • @MJ: Did you call it in the form's constructor? It should be the control's constructor. – Palle Due Feb 02 '22 at 11:49
  • @PalleDue firstly I called in the constructor of the custom control so I got the results that I mentioned in the update, but after your last reply I removed from the custom control and I called in the constructor of the form that contains the custom textbox (here the on paint doesn't fire!!) so what's the problem please? – M.J Feb 02 '22 at 11:58
  • TextBoxes and ListBoxes are notoriously hard to customise without using interops to handle the painting. You can confirm this by putting a break point in wherever you're handling the control's 'PaintEventArgs' as it never hits it. Calling "SetStyle(); handles the painting to the "OnPaint" event, but it doesn't behave like the textboxes we're used to such as flickering and other things. – Sylas Coker Feb 02 '22 at 12:00
  • @PalleDue now I called (SetStyle(ControlStyles.UserPaint, true);) in the constructor of both the custom control and the form that contains the custom control, is that correct and why? – M.J Feb 02 '22 at 12:01
  • 1
    @MJ You shouldn't call "SetStyle()" in the form that contains your control, just the custom control itself will do but you'll have to handle all the normal functions you're used to in TextBoxes if you're calling "SetStyle()". – Sylas Coker Feb 02 '22 at 12:04
  • @SylasCoker thank you, but can you please explain more with an example about that (handle all the normal functions) if you can apply it to my code I will be appreciated it. – M.J Feb 02 '22 at 12:06

0 Answers0