简体   繁体   中英

C# - Changing button image event is not rising

I am making a simple program that changes the image of a button when the user interact with it. When the computer mouse gets inside the button the image highlights, when the mouse clicks it gets darker and when the mouse leaves the button, the image gets norma. To accomplish this behavior I am changing the button image between 3 images, BUT when I click the button and it displays a modal control, eg a MessageBox, when it goes back from the MessageBox, the image in the button is still the "mouse inside" image, but the mouse is out of the button already...so I think the button1_MouseLeave event is not rising, but I dont understand why and how to repair it...can you guys help me out on this one? The code I am using is simple:

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

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

        private void button1_MouseLeave(object sender, EventArgs e)
        {
            button1.Image = ((System.Drawing.Image)(Properties.Resources.Botao_Del_Normal));
        }

        private void button1_MouseEnter(object sender, EventArgs e)
        {
            button1.Image = ((System.Drawing.Image)(Properties.Resources.Botao_Del_Claro));
        }

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

        private void button1_MouseDown(object sender, MouseEventArgs e)
        {
            button1.Image = ((System.Drawing.Image)(Properties.Resources.Botao_Del_Clique));
        }

        private void button1_MouseUp(object sender, MouseEventArgs e)
        {
            button1.Image = ((System.Drawing.Image)(Properties.Resources.Botao_Del_Claro));
        }        
    } 
}

The mouse sure doesn't leave, because the focus on the button is stolen before that, so it doesn't receive those messages. You should also listen to the LostFocus event and switch to normal image when the button loses focus.

private void button1_LostFocus(object sender, EventArgs e)
{
    button1.Image = (System.Drawing.Image)Properties.Resources.Botao_Del_Normal;
}

Make sure to actually listen to the LostFocus event! Just adding the code won't do a thing.

Note that since the signatures of expected LostFocus and MouseLeave delegates are the same, you could actually listen to both events with the same method (and call it something like button1_MouseLeaveOrLostFocus or anything that suits you)

Please try button1_MouseHover event instead of button1_MouseEnter Event. I will work.

This is to be expected, as according to your code the image gets reset when you stop clicking, which will be triggered by the window losing focus due to the messageBox.

Delete the MouseUp handler to achieve the behaviour you desire

SOLVED: Ok guys, I figured how to make it work! I had to remove the MouseClick event, and added all the processing to the MouseUp event, so now MouseLeave is called after MouseUp as it should (even with ALT+TAB). The code now is:

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

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

        private void button1_MouseEnter(object sender, EventArgs e)
        {
            button1.Image = ((System.Drawing.Image)(Properties.Resources.Botao_Del_Claro));
        }

        private void button1_MouseDown(object sender, MouseEventArgs e)
        {
            button1.Image = ((System.Drawing.Image)(Properties.Resources.Botao_Del_Clique));              
        }

        private void button1_MouseLeave(object sender, EventArgs e)
        {
            button1.Image = ((System.Drawing.Image)(Properties.Resources.Botao_Del_Normal));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Image = ((System.Drawing.Image)(Properties.Resources.Botao_Del_Normal));
            // Processing is made here!
        }

    }
}
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent(); 
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            button1.Text = "Hello World!";
        }

        private void button1_MouseHover(object sender, EventArgs e)
        {
            button1.Text = "Mouse Hover";
        }

        private void button1_MouseDown(object sender, MouseEventArgs e)
        {
            button1.Text = "Mouse Down";
        }

        private void button1_MouseUp(object sender, MouseEventArgs e)
        {
            button1.Text = "Mouse Up";
        }

        private void button1_MouseLeave(object sender, EventArgs e)
        {
            button1.Text = "Mouse Leave";      
        }      
    }    
}

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