'WindowsForms C# non firing click event [closed]

I am creating my own button element. When the button is clicked, it works correctly. But if you press the button quickly, then some of the clicks are ignored. In the button class, which is inherited from the Control class, in the OnMouseDown method, I added information to the console about the button being pressed. I also added information about the button click in the click handling method in the main form class, which, of course, is inherited from the Form class. The method itself was automatically created from the constructor.

Why is part of the clicks in the main class ignored?

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sof
{
    public class Test : Control
    {
        private bool Clicked = false;

        public Test()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint |
                    ControlStyles.OptimizedDoubleBuffer |
                    ControlStyles.ResizeRedraw |
                    ControlStyles.SupportsTransparentBackColor |
                    ControlStyles.UserPaint, true);
            DoubleBuffered = true;

            Size = new Size(95, 45);
            BackColor = Color.Red;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Graphics graph = e.Graphics;

            graph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            graph.Clear(Parent.BackColor);

            Rectangle rect = new Rectangle(0, 0, Width-1, Height-1);

            graph.DrawRectangle(new Pen(BackColor), rect);

            if(Clicked)
            {
                graph.FillRectangle(new SolidBrush(Color.Blue), rect);
            }
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);

            Clicked = true;
            Invalidate();

            Debug.WriteLine("Requested: " + DateTime.Now.ToString("fff"));
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseDown(e);

            Clicked = false;
            Invalidate();
        }
    }
}



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

namespace sof
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void test1_Click(object sender, EventArgs e)
        {
            Debug.WriteLine("Accepted: " + DateTime.Now.ToString("fff"));
        }
    }
}


I have attached a screenshot of the log from the debugger console. Maybe it will help someone to solve this issue



Solution 1:[1]

It may be that the double-click event is being fired instead of two clicks.

You could try handling the DoubleClick event as well and check if it is fired.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ryan M