-2

I want to make my PictureBox change the "Button normally" picture to "Button pressed". After I held down the mouse button, PictureBox should change its image from "Button normally" to "Button pressed" and after releasing the mouse key, it should get back to "Button normally". I've uploaded my pictures down below . Thanks in advance ☺

my first image: Button normally

My second image: Button pressed

Edit: My code:

'''C# 
private void pictureBox2_Up(object sender, MouseEventArgs e)
    {
        pictureBox2.Image = Properties.Resources.test_voice_button_pressed;
        say("Hi, im Dude, your smart assistant");
//pictureBox2_Up means MouseUp and the same thing for pictureBox2_Down
    }

private void pictureBox2_Down(object sender, MouseEventArgs e)
{
    pictureBox2.Image = Properties.Resources.test_voice_button_normal;
}
'''
Arash
  • 1
  • 5
  • Where is the code for this feat and what is the problem or question? Note that Visual Studio doesnt seem to have anything to do with the non-question – Ňɏssa Pøngjǣrdenlarp Jun 19 '21 at 18:41
  • If use XAML - you need check this [How to change button image when clicked in xaml](https://stackoverflow.com/questions/20585730/how-to-change-button-image-when-clicked-in-xaml) – Genusatplay Jun 19 '21 at 18:42
  • @Genusatplay I'm using C# – Arash Jun 19 '21 at 18:53
  • @ŇɏssaPøngjǣrdenlarp I edited my post so you will understand it better. Also, both pictures exist in my project resources – Arash Jun 19 '21 at 18:54
  • @Arash Do you mean using WinForms? – Genusatplay Jun 19 '21 at 18:57
  • See related question https://stackoverflow.com/questions/13455439/adding-a-mouse-click-eventhandler-to-a-picturebox, you need to use MouseUp and MouseDown events. – user700390 Jun 19 '21 at 18:58
  • @Genusatplay Yes – Arash Jun 19 '21 at 19:17
  • @user700390 Thanks for your comment. I tried it but it won't change the picture until I click it again but I don't want it. I want it to be like: I click, picture changes to "Button pressed" then immediately goes back to "Button normally" – Arash Jun 19 '21 at 19:20
  • @Arash There could be logical error in your event handlers, can you update the question to show the code that you wrote? – user700390 Jun 19 '21 at 19:23
  • You do realize that you've got your "pressed" wired up to your "mouse up"? Careful with images in Resources; Resources is a generator – Caius Jard Jun 19 '21 at 19:37
  • @CaiusJard Thanks for your comment and help but I didn't get what you mean :( Can you please explain more clearly that what I did wrong there and what should I do to fix it? – Arash Jun 19 '21 at 19:41
  • @Arash I think your logic is backwards in the event handlers compared with your explanation of the desired behavior. Try using `test_voice_button_pressed` in the `pictureBox2_Down()` method. – user700390 Jun 19 '21 at 19:42

1 Answers1

0

You should use MouseDown and MouseUp actions. Here's the code:

       private void pictureBox2_Up(object sender, MouseEventArgs e)
    {
        pictureBox2.Image = Properties.Resources.test_voice_button_normal;
        say("Hi, im Dude, your smart assistant");
    }

    private void pictureBox2_Down(object sender, MouseEventArgs e)
    {
        pictureBox2.Image = Properties.Resources.test_voice_button_pressed;
    }

Special thanks to @user700390 and @CaiusJard.

Arash
  • 1
  • 5