C#/VB.NET Play System Sound

In order to play one of the system sounds from your C#/VB .NET you can use the ready to use system sounds the Windows uses for common tasks. Basically, there are 5 sounds that Windows presents:

  1. Asterisk
  2. Beep
  3. Exclamation
  4. Hand
  5. Question

Here is a snap example of filling the system sounds to a combo box and play one of them when an item is selected:


        private void Form1_Load(object sender, EventArgs e)
        {

            comboBox1.DisplayMember = "Text";
            comboBox1.ValueMember = "Value";

            var systemSounds = new[]
            {
                new { Text = "Asterisk", Value = System.Media.SystemSounds.Asterisk },
                new { Text = "Beep", Value = System.Media.SystemSounds.Beep },
                new { Text = "Exclamation", Value = System.Media.SystemSounds.Exclamation },
                new { Text = "Hand", Value = System.Media.SystemSounds.Hand },
                new { Text = "Question", Value = System.Media.SystemSounds.Question }
            };

            comboBox1.DataSource = systemSounds;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ((System.Media.SystemSound)comboBox1.SelectedValue).Play();
        }

That’s it 🙂

Please note, by default most of the sounds are the same, and you can change them in the System Sounds Properties in the Control Panel.

Relevant Links:

SystemSounds Class
https://docs.microsoft.com/en-us/dotnet/api/system.media.systemsounds?view=dotnet-plat-ext-3.1

More advanced example of using the Windows System Sounds
https://www.codeproject.com/Articles/2740/Play-Windows-sound-events-defined-in-system-Contro

Microncode Components and Libraries:
https://microncode.com/developers/

VS/C#/VB “The type exists in both versions” Error

Some .NET libraries or components may use the same type / enum name which can make the Visual Studio compiler generate an error with the message:

“the type exists in both versions”

Visual Studio compiler error

In order to fix that issue, you can do as follow:

  1. Goto References and select one of the libraries that generated this error.
  2. In the Properties change the Alias property to ‘MyAlias’.
  3. Add to the top of the class that used the library:
//Declear the alias name of the library
extern alias MyAlias;

//------------------------------
//The rest of your code...
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

That’s it 🙂

From now on you can create a reference to the library like:

MyAlias.TheLibrary.ClassName xyz;