Click to See Complete Forum and Search --> : Buttons and sounds


January 20th, 2000, 07:25 AM
Hi,
How do i set a particular sound for the button pressed.
for eg: If i have a menu in Visual Basic and when i click on the first option of the menu i want a sound to play at the same time .
Can anyone please tell me the code for that?
Also if i have several buttons in my Page like "UPDATE" or "ADD" how do i assign sounds for them when they are clicked on.
Thanks for any answers,
New to Visual Basic......... ;)

JimmyT
January 20th, 2000, 08:22 AM
If you want just simple sounds, you can use the 'Beep' command. There is adequate support for this command in the online help for VB.

On the other hand, if you want to use the wide variety of Windows sounds (or other files) I would recommend the following:

1) Add a Microsoft Multimedia Control to the form. This can be done by Clicking on the 'Project' Menu item, then on the 'Components' list item, then finding the Microsoft Multimedia Control and clicking on it. This control is then added to the form just like any other regular control.

2) Set the properties for the Multimedia Control so that 'Visible' = False (you don't need to see the control to make it work for you. Set 'DeviceType' = "WaveAudio".

3) Here is some sample code that plays two different tones depending on which menu item you select.

To make it work, start a new Standard EXE project and add a Multimedia Control to Form1 as described above (The default name for this control is MMControl1). Set the properties that I described above.

Add a Menu to your form. I created a main menu titled "Menu" that has two sub menu entries below it. These are called mnuItem(0) and mnuItem(1).

Paste this code into your code window for Form1:



option Explicit

private Sub Form_Load()
MMControl1.PlayEnabled = true
End Sub

private Sub mnuItem_Click(Index as Integer)
Select Case Index
Case 0
MMControl1.FileName = "C:\Windows\Media\Chord.wav"
Case 1
MMControl1.FileName = "C:\Windows\Media\Ding.wav"
End Select
MMControl1.Command = "Open"
MMControl1.Command = "Play"
End Sub





Run the program.

Open the menu and alternately click on the items in the submenu, you should hear different sounds for each menu item.

This example can be extended to allow the MMControl to handle clicks on any control that you desire or to create sounds in response to user entries, mark progress in the program, or for any other use that you see fit. The C:\Windows\Media directory has a wide variety of the standard Windows sounds, but the MMControl can be used to play .AVI, .WAV, etc from any file on your computer. You just need to specify the correct filename to play it.

Good Luck...

Starcraft
January 20th, 2000, 11:55 AM
'DELCLARATION

Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" _
(ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

Const SND_SYNC = &H0000
Const SND_ASYNC = &H0001
Const SND_NODEFAULT = &H0002
Const SND_LOOP = &H0008
Const SND_NOSTOP = &H0010

'CODE

SoundFile$ = "c:\windows\media\chimes.wav"
wFlags% = SND_ASYNC Or SND_NODEFAULT
x% = sndPlaySound(SoundFile$,wFlags%)