tags:

views:

835

answers:

5

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 very simple.

MessageBox.show("text");
+2  A: 

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:

  1. local machine: sc stop beep && sc config beep start= disabled
  2. remote machine: sc \remoteMachine stop beep && sc \remoteMachine config beep start= disabled
  3. requires reboot: Device Manager -> View -> Show Hidden Devices -> Non Plug and Play -> Beep -> Disable
  4. use TweakUI: General > Settings -> Uncheck the Beep on Errors

(from here)

Christian Payne
+1 perhaps a little heavy handed, depending on the need.
kenny
+9  A: 

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.

ChrisF
+1 for writing your own dialog box method/class.
TrueWill
+2  A: 

Depending on how much you're leveraging the MessageBox (icons, etc) you can always make a custom dialog. Especially if it's a simple message you want to display, this would not be difficult or time consuming.

David in Dakota
+2  A: 

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).

Joe
A: 

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 undecroated (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. :)

Xanatos