Open And Close CD Tray


Note: I use Visual Basic Enterprise 6 for this tutorial.

Hey there! this is just a simple visual basic tutorial for those of you who have problems with opening and closing your CD tray.

Common Uses:
1. too lazy to bend over and open it yourself.
2. its stuck for an unknown reason.

First off open up your Visual Basic program

and create a new Standard EXE

TutorialNinja Image

After doing so you will want to grab the Command Button tool and on the form draw a random button.

Off to the side in the Properties menu select the box to the right of (name) and rename it cmdOpen. Then in the box to the right of Caption replace it with Open Tray!

Or you can use this image to help you!

TutorialNinja Image

You will want to do that again but instead of Open Tray and cmdOpen you will want to use Close Tray and cmdClose.

You an also add another command button and name it cmdPClose with the caption of Close Program.

Now what you want to do is double click on the cmdOpen command button use the following code

VISUALBASIC Code
  1. mciSendString "set cdaudio door open", 0, 0, 0 'Open the CD Tray
  2. MsgBox "Tray Opened!" 'say the cd tray was opened


So far the code that the code window should show is:
VISUALBASIC Code
  1. Private Sub cmdOpen_Click()
  2. mciSendString "set cdaudio door open", 0, 0, 0 'Open the CD Tray
  3. MsgBox "Tray Opened!" 'say the cd tray was opened
  4. End Sub


Next, double click the Close Tray button and add:
VISUALBASIC Code
  1. mciSendString "set cdaudio door closed", 0, 0, 0 'Close Tray
  2. msgbox "Tray Closed!"


With that done theres really only 1 thing to do which is the cmdPClose command button.

With that, double click Close Program button and add:

VISUALBASIC Code
  1. Me.Hide 'Close the program
  2. MsgBox "Goo Bye!" 'Good bye ^^


That should be it But before you go to compile you will want to add one last code to the top of the document that will make this program work for real which is:

VISUALBASIC Code
  1. Option Explicit
  2. Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long


That should go above ALL OTHER CODING or you may get an error.

When thats done feel free to add graphics or other random things then click F5 to test.

There is a download in the downloads section of the website with a preview of this :)

Hope you learned something!
ShadowMage's Avatar
Author:
Views:
3,237
Rating:
Posted on Wednesday 20th August 2008 at 01:57 PM
cyruswu
cyruswu's Avatar
Ahh man! I wish I learn't Visual Basic.
Posted on Tuesday 19th August 2008 at 06:36 PM
ritchielee666
ritchielee666's Avatar
This was a brilliant tuturial!
VB 9.0 (2008) Wouldn't have it, but i fixed that!
Posted on Tuesday 19th August 2008 at 03:55 PM
ritchielee666
ritchielee666's Avatar
pretty handy, mine never opens when i press the button!
Posted on Thursday 27th December 2007 at 01:36 PM
Dalez
Dalez's Avatar
Thanks, this has worked fine!
Posted on Sunday 6th May 2007 at 10:12 PM
ShadowMage
ShadowMage's Avatar
Lol MSN has a script with that name :P
Posted on Sunday 6th May 2007 at 09:36 PM
DanielXP
DanielXP's Avatar
CUP HOLDER!
Posted on Sunday 6th May 2007 at 09:27 PM
ShadowMage
ShadowMage's Avatar
lol no prob :) theres a download in the Other category
Posted on Sunday 6th May 2007 at 09:10 PM
Diablosblizz
Diablosblizz's Avatar
Haha this is a VERY lazy way of opening the CD Drive! I like it! Ty!