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

jack pelon 96 Reputation points
2021-01-15T15:39:02.407+00:00

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 Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,669 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,221 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
762 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,031 Reputation points
    2021-01-15T17:53:54.827+00:00

    Hello @jack pelon

    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.


2 additional answers

Sort by: Most helpful
  1. jack pelon 96 Reputation points
    2021-01-16T04:36:28.717+00:00

    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. Castorix31 81,636 Reputation points
    2021-01-16T08:56:12.02+00:00

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