Post

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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:

1
Microsoft.VisualBasic.Interaction.Beep()
This post is licensed under CC BY 4.0 by the author.