Tag Archives: beep

How to Generate/Play Custom Beeps and Sound Tones in VB 6.0 and VB.NET

Once in awhile you may want to play certain sounds for certain reasons in your applications. You may want more than the generic Beep() that is in Visual Basic and .NET. But if you ONLY want a simple Beep to sound through the speakers, then use the simple built-in Sub that is available with ALL versions of Visual Basic as seen below…

Update: Thanks to Andreas for mentioning that Win95/Win98/WinME will only play the default beep sound using the API call below. Custom Freq/Durations are not supported until Win NT/2000 and above including XP/Vista.

Update Again: Windows Vista 64-bit will only play the default beep with this API code. Probably all 64-bit versions. So it looks like this code has become obsolete. Sorry about that 🙁 


'
'This Sub plays a simple beep sound through the computer. Supported in ALL versions of VB.

Beep()

 

BUT, what if you wanted to make different beeps or tones that could mean different things using certain frequencys and/or different duration combinations? What if you wanted to make a Morse Code application?

The code in this article will show a way to beep in VB 6.0 and Visual Basic.NET and an extra method for Visual Basic 2008/2010, and newer.  The way to do it with ALL versions of Visual Basic it to use a simple little Microsoft Windows API call. The API code is below…

 

Visual Basic 6.0 Declaration…

'
'This is the little API call to give us some say in the duration and frequency of the sound...
Private Declare Function Beep Lib"kernel32" (ByVal soundFrequency As Long, ByVal soundDuration As Long) As Long

 

Visual Basic.NET 02/03 and 05/08/2010

'
'This is the little API call to give us some say in the duration and frequency of the sound...
Private Declare Function Beep Lib"kernel32" (ByVal soundFrequency As Int32, ByVal soundDuration As Int32) As Int32

 

The soundFrequency parameter is where you can specify how high or low you want the frequency to be. I looks to have a Minimum supported value of 37hz and Maximum of 32767hz.

The soundDuration parameter is for how long you want the sound to play in milliseconds. IF you set the parameter to 5000, is will keep generating that tone for 5 seconds before it stops. Something to remember too is that this function will consume the calling thread. So if you set a tone for long periods of time your application will have to wait until the tone is finished generating before it will be responsive again if you call it inside your apps thread.

Simple example:

'
'This Function plays a simple beep sound through the computer at a frequency of 1000hz and for 3x seconds.
Beep 1000, 3000

 

You can get the return value of the Function if you want. Especially if it doesn’t appear to be working. IF the Return Value is Zero(0) then the call failed. This could happen if the Frequency you set it to Low and/or to High. Nothing more I can really say…

The next method works with Visual Basic 2008/2010 and higher. Its actually a managed wrapper around the Beep() api function. Its located in the: System.Console Class.

This is the Sub Declaration:

System.Console.Beep(Integer, Integer)

 

You can simply call this Sub the same way you did the API version…

'
'This Sub uses the Beep() Subroutine under the Console Class and plays a simple beep sound through the computer at a frequency of 4000hz and for 1x second.
Console.Beep(4000, 1000)

That’s all there is to it. If you want to make Morse code this code should be perfect for doing that. Hope you got something useful here. 🙂

Jason

Revised: 2015

Disable ‘Beep’ in Textbox when pressing the ‘Enter’ Key – VB.NET 2008/2010, VB 2013

This will stop that beep from sounding when you press the Enter key in the textbox or combobox control. This works for VB 2005, VB 2008, Visual Basic 2010, and VB 2013.

Just put the code in the Textbox _KeyDown event.

        '
        'This code will disable the ‘Beeping’ you get when you press the ‘Enter’ key in a
        'Textbox control. This code needs to go into the Textbox_KeyDown’ event.
        '
        If e.KeyCode = Keys.Enter Then

            e.SuppressKeyPress = True

        End If

 

Disable the ‘Beep’ in Textbox when pressing the ‘Enter’ Key in VB.NET

This will disable that ‘Beep’ when you press the ‘Enter’ key in a Textbox control. You want to put in your textbox controls _Keypress event. For example Textbox1_KeyPress

 

        '
        'put this code in the TextBox_KeyPress event of your Textbox.
        'You can use the 'Carriage Return Character' constant: 'vbCr'
        'as the KeyChar to check or you can use the constant as the KeyChar.
        '
        If e.KeyChar = vbCr Then

            e.Handled = True

        End If

Why it Beeps Pressing Enter in a Textbox Control and How to Fix it in .NET

I’ve seen many questions on how to disable the beep that occurs when you press the ‘Enter’ key in a single line Textbox control or a Combobox control. From my understanding the reason is because in Single Line mode, the textbox control doesn’t know how to handle the Enter/Return key, and is basically reporting an error when you get the beep. But it is pretty simple to keep that from happening.

Visual Basic 2005, VB 2008, and VB 2010

        ' This code will disable the 'Beeping' you get when you press the 'Enter' key in a Textbox
        'control. This code needs to go into the 'Textbox_KeyDown' event.
        If e.KeyCode = Keys.Enter Then

            e.SuppressKeyPress = True

        End If

Visual Basic.NET 2002/2003

The code below will also work with VB 05, 2008, and Visual Basic 2010. But the above snippet will NOT work on Visual Basic.NET 2002 or .NET 2003.

        '
        'This is for VB.NET 2002, .NET 2003, and Higher
        'Put in your textbox controls Textbox_KeyPress
        '
        If e.KeyChar = Chr(13) Then

            e.Handled = True

        End If
        '
        'Or you can use the code below. Which will be the same result as above.
        '
        If e.KeyChar = vbCr Then

            e.Handled = True

        End If

That should take care of the beeping problem. Take care.