How do I disable the c# message box beep?

后端 未结 7 985
攒了一身酷
攒了一身酷 2020-12-16 13:30

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

相关标签:
7条回答
  • 2020-12-16 14:06

    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

    0 讨论(0)
  • 2020-12-16 14:14

    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)

    0 讨论(0)
  • 2020-12-16 14:14

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

    0 讨论(0)
  • 2020-12-16 14:17

    From the searching I've done it looks like the beep is hardwired into the Win32 message box function:

    • Bytes
    • VB Forums

    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.

    0 讨论(0)
  • 2020-12-16 14:17

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

    0 讨论(0)
  • 2020-12-16 14:17

    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.

    0 讨论(0)
提交回复
热议问题