Code Repo    |     RSS
MD's Technical Sharing



Wednesday, February 18, 2009

How to disable WebBrowser control's ‘click sound’

Depending on system preferences (in Control Panel/Sounds), the .NET WebBrowser control (just like Internet Explorer) may produce a click sound when certain events are done to it (e.g. back/forward navigation, DocumentText changes, etc.)

To avoid this:

Method 1: Do not changes the web browser document text via the DocumentText property. So instead of this:

webBrowser1.DocumentText = "<h1>Hello, world!</h1>";

try this:


webBrowser1.Document.OpenNew(true);
webBrowser1.Document.Write("<h1>Hello, world!</h1>");


Side-effects: This cause the web browser to steal focus from the active application when OpenNew is called!

Method 2: Use the Document Object Model (DOM)'s InnerHTML property to change the text.

Me.WebSMSView.Document.DomDocument.Body.InnerHTML = text


This doesn't have any side effects as method 1, and is preferable, but requires the use of Reflection in C#, and Option Strict Off in VB.NET

Method 3: Change system sound preferences from code. Disable it when the application has focus and then re-enable when the application closes/loses focus.

WebClickSound.cs: class to enable/disable the click sound

class WebClickSound
{
/// <summary>
/// Enables or disables the web browser navigating click sound.
/// </summary>
public static bool Enabled
{
get
{
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"AppEvents\Schemes\Apps\Explorer\Navigating\.Current");
string keyValue = (string)key.GetValue(null);
return String.IsNullOrEmpty(keyValue) == false && keyValue != "\"\"";
}
set
{
string keyValue;

if (value)
{
keyValue = "%SystemRoot%\\Media\\";
if (Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor > 0)
{
// XP
keyValue += "Windows XP Start.wav";
}
else if (Environment.OSVersion.Version.Major == 6)
{
// Vista
keyValue += "Windows Navigation Start.wav";
}
else
{
// Don't know the file name so I won't be able to re-enable it
return;
}
}
else
{
keyValue = "\"\"";
}

// Open and set the key that points to the file
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"AppEvents\Schemes\Apps\Explorer\Navigating\.Current", true);
key.SetValue(null, keyValue, RegistryValueKind.ExpandString);
isEnabled = value;
}
}
}

Main form: use the above code in these 3 events: Activated, Deactivated and FormClosing

private void Form1_Activated(object sender, EventArgs e)
{
// Disable the sound when the program has focus
WebClickSound.Enabled = false;
}

private void Form1_Deactivate(object sender, EventArgs e)
{
// Enable the sound when the program is out of focus
WebClickSound.Enabled = true;
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// Enable the sound on app exit
WebClickSound.Enabled = true;
}


This works but is probably an overkill for this task.

Reference:

How To Disable WebBrowser ‘Click Sound’ in your app only, http://stackoverflow.com/questions/10456/howto-disable-webbrowser-click-sound-in-your-app-only

2 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. Thank you! Very usefull artikle!

    ReplyDelete

Note: Only a member of this blog may post a comment.