question

jackpelon-3894 avatar image
0 Votes"
jackpelon-3894 asked ·

WPF C #, How to emit sounds when passing the mouse over a button and also when clicking on the button.

Hello and happy new year. I am a newbie to C # WPF programming. How can I make a button emit a sound when passing the mouse over the button and also when clicking on the button emit another sound. I will be very grateful for your help and I thank you in advance and give you a warm hug.

windows-wpfdotnet-csharpdotnet-wpf-xaml
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered ·

Hello @jackpelon-3894

This can be done with OnClicl and OnMouseOver events - see full source code here.

XAML

57233-11111111111.png

Code behind


 private void Element_OnMouseEnter(object sender, MouseEventArgs e)
 {
     SystemSounds.Beep.Play();
 }
    
 private void Button_OnClick(object sender, RoutedEventArgs e)
 {
     SystemSounds.Exclamation.Play();
 }

You could play a wav file via SoundPlayer.



11111111111.png (63.9 KiB)
· 1 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello miss "karenpayneoregon". I am grateful for her kind help. I was doing tests trying to follow your code to emit a sound when passing the mouse over a button with a "sound_1.wav" and then when clicking on the same button emitting another "sound_clic.wav" and had no success ... There could be perhaps an easier way to achieve it. Maybe you could show me another example please. I appreciate your patience and her time, I send you a cordial greeting and I wish you a good day or night .... see you later, miss.


0 Votes 0 ·
jackpelon-3894 avatar image
0 Votes"
jackpelon-3894 answered ·

Hello miss "karenpayneoregon". I am grateful for her kind help. I was doing tests trying to follow your code to emit a sound when passing the mouse over a button with a "sound_1.wav" and then when clicking on the same button emitting another "sound_clic.wav" and had no success ... There could be perhaps an easier way to achieve it. Maybe you could show me another example please. I appreciate your patience and her time, I send you a cordial greeting and I wish you a good day or night .... see you later, miss.

· 2 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

What I provided works, have no idea why that does not work for you. Once a button is on the window, select the button, select properties. Now select the lighten bolt, find Click, double click, add the following

 System.Media.SoundPlayer mediaPlayer = new System.Media.SoundPlayer(@"c:\SomeSound.wav");
 mediaPlayer.Play();

Do the same for Mouse over e.g.
57289-11111111111.png


0 Votes 0 ·
11111111111.png (10.6 KiB)

Hello miss "karenpayneoregon". I am very very grateful for her kind help. After several tests I managed to make it work, I realized that I needed to add the "using System.Windows.Media;" and the "using static System.Media.SystemSounds;". I'm really a very newbie even in programming ... I wish you a good day and I send you a cordial hug from Peru. See you later miss "karenpayneoregon", thank you, thank you very much ...

0 Votes 0 ·
Castorix31 avatar image
0 Votes"
Castorix31 answered ·

You can see for example : Playing audio
(and add MouseHover event for over)



· 1 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you, thank you very much ... your advice was also helpful but as I am a newbie it will take me longer to deduce all this, thanks and have a good day ... see you later ...

0 Votes 0 ·