Search code examples
c#winformspicturebox

How to make 'click event' is a custom user control's?


I have a problem to handle mouse click event on dynamic object. I have 2 form, a main form called FormMain and another customized form called FormView, I create a component base on PictureBox class like this:

public class customPB : PictureBox
{
        public customPB()
        {
            InitializeComponent();
            this.Width = 195;
            this.Height = 114;
            this.Visible = true;
            this.Invalidate();
        }
        .
        .
        private void BtnLink_Click(object sender, MouseEventArgs e)
        {
            MessageBox.Show("Button Click Event");
        }
        .
        .
        private void InitializeComponent()
        {
            this.btnAmazon = new Button();
            ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
            this.SuspendLayout();
            // 
            // btnLink
            // 
            this.btnLink.BackColor = System.Drawing.Color.White;
            this.btnLink.BackgroundImage = Properties.Resources.LinkIcon;
            this.btnLink.Location = new System.Drawing.Point(9, 70);
            this.btnLink.Name = "btnLink";
            this.btnLink.Size = new System.Drawing.Size(34, 34);
            this.btnLink.TabIndex = 1;
            this.btnLink.MouseClick += new MouseEventHandler(BtnLink_Click);

            this.Controls.Add(this.btnLink);
            this.Size = new System.Drawing.Size(195, 114);
            ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
            this.ResumeLayout(false);

        }
}

In my project , I created "FormView" form like this:

    class FormView : Form
    {
        public FormView(Form windowParent, Point windowPosition ,Size windowSize )
        {
            _windowPosition = windowPosition;
            this.Owner=windowParent;
            InitializeComponent();
            windowParent.LocationChanged += FormView_LocationChanged;
            this.Location = windowParent.PointToScreen(windowPosition);
            this.ClientSize = windowSize;
            this.Show(windowParent);
        }

        private void FormView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                custPB = new customPB();
                this.Controls.Add(custPB);
            }
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            this.ClientSize = new System.Drawing.Size(284, 261);
            this.Name = "OverlayWindow";
            this.DoubleBuffered = true;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.ShowIcon = false;
            this.ControlBox = false;
            this.ShowInTaskbar = false;
            this.Paint += FormView_Paint;
            this.MouseDoubleClick += FormView_MouseDoubleClick;
            this.ResumeLayout(false);
        }
        protected override void OnActivated(EventArgs e)
        {
            // Always keep the owner activated instead
            this.BeginInvoke(new Action(() => this.Owner.Activate()));
        }
   }

In main form , I create new "FormView" like this:

        private FormView frm1 ;
        public frmMain()
        {
            InitializeComponent();
            FormView frm1 = new FormView(this);
            FormView.Visible = true;
        }

When I click on btnLink in custom PictureBox component, nothing happens, what's my mistake? Try to fire click event on button in custom components created dynamical. thanks

try to fire click event on button in custom components created dynamical


Solution

  • You have to remove the event handler:

    protected override void OnActivated(EventArgs e)
    {
        // Always keep the owner activated instead
        this.BeginInvoke(new Action(() => this.Owner.Activate()));
    }
    

    By calling this.Owner.Activate() you are giving focus to the parent form and customPB loses the focus, preventing the click event from working correctly.