简体   繁体   中英

C# Unable to Perform Mouse Click (WinForms)

I have a Button and several PictureBoxes. The PictureBoxes are hooked to MouseHover and MouseLeave Events. OnMouseEnter/OnMouseLeave the Button gets Shown/Hidden in a Specific Place of the PictureBox. The Button purpose is to Display OpenFileDialog upon Click. The event is assigned; It Clicks (visually); however it doesn't reach the Breakpoints / Click Method. I could figure that if I take the Button out of the PictureBox Area (Elsewhere on the Form); Enable and Show it straight away after form loading; it will reach the Breakpoints. Any clue why this could happen? Any help would be appreciated. Thanks in advance

Update: Added my code bellow.

private void PicBox_MouseEnter(object sender, EventArgs e)
{
    var pic = sender as PictureBox;

    ShowButton_LoadPhoto(pic);
    SelectedPictureBoxName = pic.Name;
}

private void PicBox_MouseLeave(object sender, EventArgs e)
{
    btn_LoadPhoto.Visible = false;
}

private void ShowButton_LoadPhoto(PictureBox pic)
{
    var locX = pic.Location.X + pic.Size.Width - btn_LoadPhoto.Size.Width - 3;
    var locY = pic.Location.Y + pic.Size.Height - btn_LoadPhoto.Size.Height - 3;

    btn_LoadPhoto.Location = new Point(locX, locY);

    btn_LoadPhoto.Enabled = true;
    btn_LoadPhoto.Show();
}

private void BtnLoad_Click(object sender, EventArgs e)
{
    MessageBox.Show("Test");
}

Designer Class:

    this.btn_LoadPhoto.Click += new System.EventHandler(this.BtnLoad_Click);

Alright; this was slightly obvious; yet easy to miss.

I've programmatically created the button and set everything manually as most accurate as possible. This made it easier to figure out the reason behind the issue.

Issue: Whenever the "PicureBox.OnMouseLeave" Occurs; the Button.Click Event gets Unavailable / Overridden (even though the Button is Visible and on Top of PictureBox).

Note: Interestingly; with the new (programmatically created) Button; the IDE will show a different behavior; and further wards let the user understand the reason why it was happening.

Answer: The OnMouseLeave Event was in conflict with the Click Event.

Remarks: Unlike the previous way ("Dropping a Button on the Form"); by Programmatically creating the Button; the IDE simply hid Button whenever the mouse moved into another Control (in this specific case; if the User Wanted to Click the Button). Previously; this wouldn't happen. The Button would Stand Visible in Front of the Control. But the Click Event Wouldn't Trigger (nor any Breakpoints in Between).

Thanks for the Help and Quick Replies.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM