Whenever trigger a messagebox used in my C# program I get a very annoying beep from my computer. How do I disable this beep using C# code.
The code I am using is ve
Here's how I solved that little problem.
Add this simple class to your project. It's part of my personal library now but you can add the class directly in your project
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace MyCSharpLibrary
{
public class Volume
{
[DllImport("winmm.dll")]
public static extern int waveOutGetVolume(IntPtr h, out uint dwVolume);
[DllImport("winmm.dll")]
public static extern int waveOutSetVolume(IntPtr h, uint dwVolume);
private static uint _savedVolumeLevel;
private static Boolean VolumeLevelSaved = false;
// ----------------------------------------------------------------------------
public static void On()
{
if (VolumeLevelSaved)
{
waveOutSetVolume(IntPtr.Zero, _savedVolumeLevel);
}
}
// ----------------------------------------------------------------------------
public static void Off()
{
waveOutGetVolume(IntPtr.Zero, out _savedVolumeLevel);
VolumeLevelSaved = true;
waveOutSetVolume(IntPtr.Zero, 0);
}
}
}
Now call Volume.Off() before calling MessageBox and Volume.On() after
Volume.Off();
MessageBox.Show("\n Information \n", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
Volume.On();
I prefer that approach because I don't have to make modifications to Windows and I can select any icons I want for my MessageBoxes.
Thank you, bye
This is going to sound weird until you have tried it. Open your command prompt, type:
net stop beep
I did a quick google and found 4 other ways:
(from here)
Try using the VisualBasic MsgBox class instead of the MessageBox. I cannot explain why, but on my computer, the VisualBasic one doesn't beep.
http://msdn.microsoft.com/en-us/library/sfw6660x%28VS.85%29.aspx
So instead of:
MessageBox.Show("text")
do:
MsgBox("text")
You'll have to import the MsgBox function from Microsoft.VisualBasic, instead of importing MessageBox from System.Windows.Forms. But that shouldn't be a problem; it's part of the .NET api.
EDIT: Ah ok, now I can explain it. The lack of sound is because the VB version of the MessageBox is undecorated (i.e. it's not informational, a question, etc.) by default. I assume there's no real need to use the VB MsgBox class after all--probably if you make the regular old MessageBox undecorated, it won't make any sound either. :)
From the searching I've done it looks like the beep is hardwired into the Win32 message box function:
So you need to either write your own method or stop the beep in the hardware. The former will work for everyone, the latter just for you.
You should leave it to the end user to decide what sounds he wants. He can configure/disable sounds for system events such as a messagebox in Control Panel / Sounds and Audio Devices / Sounds / Program Events.
Stopping the beep service required administrative privileges, and isn't something you'd normally do just for one application.
If you do go for a custom dialog to replace the MessageBox, please remember to implement CTL-C (copy messagebox contents to clipboard).
Despite this question being old I had the same problem but was able to fix it in a very simple way.
MessageBox.Show("Your text here", "Information", MessageBoxButtons.OK, MessageBoxIcon.None);
By setting the MessageBoxIcon
to None
the system produces no sound. However, there will be no icon on the box. Something like this.