Posts Making some noise with VB.NET
Post
Cancel

Making some noise with VB.NET

Brad Abrams and Scott Hanselman provided a couple of ways to create some noise with C# using a couple of interop methods.  Both methods have their uses.  Here is the VB.NET way of doing the same thing.

Public Enum MessageBeepType
  [Default] = -1
  OK = &H0
  [Error] = &H10
  Question = &H20
  Warning = &H30
  Information = &H40
End Enum

' This allows you to 'beep' using one of the sounds mapped using the
' sound mapper in control panel.
<RUNTIME.INTEROPSERVICES.DLLIMPORT("USER32.DLL", setlasterror:="True)> _
Public Shared Function MessageBeep(ByVal type As MessageBeepType) As Boolean
End Function

' Make a sound using the 'PC Beep' method, allowing you to specify the
' frequency and duration of the sound.
<RUNTIME.INTEROPSERVICES.DLLIMPORT("KERNEL32.DLL")>_
Public Shared Function Beep(ByVal frequency As Integer, ByVal duration As Integer) As Boolean
End Function

[Update] Dan Fernandez points out yet another way using the Microsoft.VisualBasic namespace:

Microsoft.VisualBasic.Interaction.Beep()

This post is licensed under CC BY 4.0 by the author.