• Recent Posts

  • Meta

  • Tags

    autocomplete benchmark data cpu cycles default browser disable beep on enter display display notify icon doevents extensions file exists finding specific text getinputstate gzip high cpu usage how to find text icon ide extensions and enhancements in intensive task in textbox control mcisendstring no doevents open webpage play mp3 play wave scroll textbox search text search text in textbox show icon show notify icon stop beep on enter system tray text tar text in system tray time consuming using vb.net vb vb.net vb 2005 vb 2008 visual basic visual basic.net visual basic 2005 visual studio 2005 ehancements vs 2005 IDE extensions
  • How to Playback a Audio Compact Disk (Play/Stop/Pause a CD)

    By Jason | May 8, 2008

    Click Star to Rate Post
    1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 4.2 out of 5)
    Loading ... Loading ...
     

     


     

      Thankfully playing audio based .cda cds is a easy process and does not require having to rely on 3rd party controls. You can simply use the Microsoft Windows MCI Command Interface to do this. As you may be aware of you can do almost all of your Media based playback needs by using this interface including playing movie files. I do plan on making a new article on playing movie files in the future. I have a complete tutorial with some source code on how to use and program the mciSendString API so you can get a much deeper understanding on how it works and how to use it. Click on this link if you want to read it.

       

      To get started you first need to setup the main API call that is required. Simply add the API below to your declarations section...

       

    _____________________________

       

     Visual Basic 5.0/6.0....

       

    '

    'Api call to send the commands to the mci device

    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

        

    Visual Basic .NET...

         '

    'Api call to send the commands to the mci device

    Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer

          

    _____________________________

       

      Now that the main API call is setup and declared you need to start using it to program the device and send it commands. The first thing you need to do is create a new device and set the device to use the CDAudio driver. As you may have guessed this driver will do AudioCD playback. Below is how to create a new device using the CDAudio driver.

       

    Note: In this article if your using VB 5/6 you only need to change Integer or Int32 types to Long types.

       

    _____________________________

       

    '

    'This will hold the mci return value. The value in this variable will not be used in this article however.

    Dim retVal As Integer

         

    '

    'This will be used to specify the drive letter of the CD or DVDRom drive to use.

    Dim cdDrive As String

        

    'This should be set to the CD or DVD drive your Music CD is in.

    cdDrive = "D:\"

       

    '

    'Add quotes which will keep from having to get the short path since the mci device could fail if using the normal long file and path. Remember to set cdDrive to the CDRom or DVDRom optical drive. Specify like: D: or D:\ if D is your drive, or E: or E:\ and so on.

    cdDrive = Chr(34) & cdDrive & Chr(34)

        

    '

    'Create a new CDAudio device with the alias CD. "CD" is how we will refer to the device  in this program.

    retVal = mciSendString("open " & cdDrive & " type cdaudio alias cd wait shareable", vbNullString, 0, 0)

        

    '

    'Send the command to set the time format to the device default. Set this so when asking for time/track information we can get the value as a Tracks/Minutes/Seconds/Frames format.

    retVal = mciSendString("set cd time format tmsf", vbNullString, 0, 0)

       

    _____________________________

       

    Now that a device has been created to use the CDAudio driver its time to start sending some commands.

       

    _____________________________

       

    The code below will return the total number of tracks on the audio cd in the cd drive...

       

    '

    'This variable is used to receive the value when requesting information from the mci device.

    Dim buf As String

       

    '

    'Give it a 128 space buffer.

    buf = Space(128)

       

    '

    'This will get the total number of tracks returned from the device.

    Dim trackCount As Integer

       

    retVal = mciSendString("status cd number of tracks", buf, 128, 0)

        

    '

    'Get the integer value from the buffer. It will return 0 (zero)  if no tracks are detected or if no audio cd is present.

    trackCount = CInt(Val(buf))

         

    _____________________________

       

      If trackCount has the number 12 then there are 12 tracks starting at Track #1 through to Track #12. So when referring to a specific track you need to specify the exact number you want the information from.

       

      Next you can now go ahead and start Playing, Stopping, and Pausing/Resuming a Audio CD's tracks. First you want to tell the device which track you want to play. The next set of codes will do that.

       

    _____________________________

       

    '

    'This will hold the track number to play. It is then set to track 2 below.

    Dim trackToPlay As Integer

       

    trackToPlay = 2

        

    '

    'You need to set the time format to the cd devices default so it knows that 1, 2, 3, 4, and so on is the track number.

    'Set the time format to the cd devices default Tracks, Minutes, Seconds, Frames when you are wanting to seek to a specific track.

    retVal = mciSendString("set cd time format tmsf", vbNullString, 0, 0)

        

    '

    'This will tell the device to move to the track number specified. Simply put in the track number you want to play.

    retVal = mciSendString("seek cd to " & trackToPlay, vbNullString, 0, 0)

         

    _____________________________

       

    You can now start Playing, Stopping, Pausing, and whatever it is you want to do.

       

    _____________________________

       

    '

    'Start playing the cd at the track you already specified above.

    retVal = mciSendString("play cd", vbNullString, 0, 0)

       

    '

    'Stop the cd from playing.

    retVal = mciSendString("stop cd", vbNullString, 0, 0)

       

    '

    'Will pause the CD if its playing.

    retVal = mciSendString("pause cd", vbNullString, 0, 0)

        

    '

    'Resumes the cd to playing if it was paused.

    retVal = mciSendString("resume cd", vbNullString, 0, 0)

       

    _____________________________

       

      Thats ALL there is to it! IF the codes in this article was properly implemented then it should Start, Stop, Pause, and Resume an Audio Compact Disk (CD). There is MUCH more you can do with Audio CD's as well like getting the length, position, fast forward, Opening/Closing the CD Door and so on. But some of those features might be implemented in a future article.

       

      I suggest you really go check out my MCI Command tutorial this site or a downloadable .doc version at my vbcodesource.com website so you would learn how to actually implement your own custom functionality by simply checking out the documentation at microsoft.msdn.com.

       

      If you don't care or have the time to create your own source code for audio CD based playback then you can check out my AudioCD based class libraries. My vbcodesource.com website has MANY librarys available that greatly simplifies the task of creating a CD player based application. I even recently released a brand new library named csAudioCD Library v2.5 Pro Enhanced for Visual Basic 2005 and VB 2008 that has Over 50 subs and properties. I have libraries for VB.NET 02/03 and VB 5.0/6.0 as well. I suggest you goto my main website under the controls and examples section if your interested. There are example applications available as well on how to use the audio cd libraries.

       

      Anyways, I hope you got something useful from this article and it was easy to understand as well. Sometime in the future I may expand on this subject by showing how to get position, duration based information and such. Have Fun!

       

                         Jason

     


     

    Popularity: 15% [?]

    Topics: - (.5.0/6.0), - (.5.0/6.0), - (.NET All + 05/08), - (.NET All + 05/08), - .All VB (Related to All), .All VB (Related to All) | No Comments »

    Stop your Code from Executing Again(Running Twice) if its Still Running

    By Jason | April 30, 2008

    Click Star to Rate Post
    1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 1 out of 5)
    Loading ... Loading ...
     

     


     

      While this tip or trick is very basic and simple, it could end up being very beneficial and useful. Depending on what your code is and what your doing it could be a big negative to have the same code run more than one instance at the same time or in other words running twice or more.

       

      Say a user clicked a button with code that performs some task like making a new thread, but the user clicks or accidentally clicks the Button more than once while the code is still running from the first click. Depending on what the code is it could cause a error or even a crash. So this little tip is simply to help keep something like that from happening.

       

      All I am going to do is create a variable named: "isCodeRunning" as a Boolean type. Declare it in the declaration section so it will be accessible in the entire form if need be. By default when a boolean is created it will return "False". Which in this case is what you want since it will signify that the code is not running at this time.

       

    ___________________________________

       

    '

    'Simple variable used to set whether specific codes are currently running or not.

    Dim isCodeRunning As Boolean

       

    ___________________________________   

      Once that is completed simply check the variables return value before executing your code and if it is True then you can exit the code to keep it from performing the task more than once at the same time.

      Remember to set the "isCodeRunning" variable to 'True' Before executing your main code but After you've made the check to see if the code is running or not. Likewise you need to set the "isCodeRunning" variable to 'False' at the end of the code you executed.

       

    Below is some simple example code....

       

    ___________________________________

       

    '

    'This will simply check the variable to see if the code is already running before proceeding.

    If isCodeRunning Then

       

        MsgBox("Hold on already! The code is still running. So wait before your previous request has finished executing before trying to run my code again!")

       

         '

        'Don't execute anymore code since its apparently still running.

        Exit Sub

       

    End If

       

    '

    'This is important! Before executing your main code you need to go ahead and set the isCodeRunning variable to 'TRUE' so you can accurately check to see if the code is still running.

    isCodeRunning = True

       

    '

    'Now run whatever code between setting the "isCodeRunning" variable to 'True' and setting the "isCodeRunning" variable to 'False'....

    '

       

    MsgBox("Hello this is where my code is...")

       

    '

    'This is important! Remember at the end of your code to set the isCodeRunning variable to 'False' so you can accurately check to see if the code is still executing or not.

    isCodeRunning = False

        

    ___________________________________   

      Thats all there is to it to keep a possible problem from taking place if your code is currently running and another instance is trying to be executed at the same time. Hopefully this simple post is understandable and helps someone out there. Have Fun!

        

                         Jason

     


     

    Popularity: 28% [?]

    Topics: - (.5.0/6.0), - (.5.0/6.0), - (.NET All + 05/08), - (.NET All + 05/08), - (.NET All + 05/08), - .All VB (Related to All), .All VB (Related to All), .All VB (Related to All) | No Comments »

    Extract the Associated Icon from a File Easily using VB 2005/2008 - Tip!

    By Jason | April 25, 2008

    Click Star to Rate Post
    1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 3 out of 5)
    Loading ... Loading ...
     


      Microsoft added a new feature in Visual Basic 2005 (and is present in 08 as well) that will very easily get the icon associated with a specified file. In the past you would have to use the Windows APIs to extract a icon from a file. But now you can use a feature in Version 2.0 and higher of the DotNET Framework to perform this task which is what this little tip will show.

       

     The function to use is 'ExtractAssociatedIcon' that is located under the System.Drawing.Icon namespace.

       

    Public Shared Function ExtractAssociatedIcon(ByVal filePath As String) As System.Drawing.Icon

       

      As you can see this is a very simple function. All you need to do is specify the path of the file whose icon you want to extract and handle the return value of the function which is the Icon associated with the file specified in the filePath parameter.

       

     ____________________________________

       

     The code below will get the Icon associated with the file I specified and display the Icon in a Picturebox control.

       

    picSource.Image = Drawing.Icon.ExtractAssociatedIcon("pathAndFileToGetIcon").ToBitmap()

        

    ____________________________________

        

      Using the code above, i'm using a picturebox that I named picSource while converting the Icon to a Bitmap which can then be displayed easier in the picturebox image property. IF you want to save the icon image displayed in the picturebox control, the small piece of code below will do the trick.

           

    picSource.Image.Save("pathAndFileToSaveTo")

       

    ____________________________________

       

           Well, thats all there is to it I guess. Since MS added this 'ExtractAssociatedIcon' Function to the framework you no longer have to use the APIs to get similar results if your using Visual Basic .NET 2005 or version 2008. Have Fun!

       

                              Jason

    Popularity: 30% [?]

    Topics: - (.NET All + 05/08), - .All VB (Related to All), .All VB (Related to All), .All VB (Related to All) | 2 Comments »

    Open Url/Webpage in Default WebBrowser - VB 5.0/6.0

    By Jason | April 19, 2008

    Click Star to Rate Post
    1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5 out of 5)
    Loading ... Loading ...
     

      I figured this would be very easy to do since VB has a Shell command built-in. But I would keep getting a File not Found message everytime I would try and open a web address. It would work for other tasks like shelling "shutdown -r -f -t 0" which is a process that can be be used to shutdown or restart the computer, but not opening a URL. So I decided to go ahead and use the Windows APIs to do this task. Below is the API Call and codes to open a url in the default web browser.

         

    ____________________________________

         

    This Function will be used to process the url which will have windows open the web browser pointing to that url path.

       

    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
        
     ____________________________________

         

      Now all thats needed is to execute that function passing the url you want opened in the third parameter. For this task the 3rd parameter is the most important. Well, I guess 2, 3, 6 should be set. Parameter #2 is how to process the task in the 3rd parameter which will be set to "open". You could just put a blank string like "" which will still open the url. But just enter "open" to be on the safe side. Parameter #6 is the window style to open the application. Like 1 is for normal and such. As far as I know the other paramters is not required for this to work so I just set them to 0 or vbnullstring.

        

    '

    'In the last parameter you can specify the program window style.

    '0 = Hide, 1 = Normal, 2 = Minimize, or 3 = Maximize.

    '

    'For other styles just open the VB API Viewer application under your VB's Tools directory and open the Win32Api.txt file and check out the constants that starts with "SW" like SW_NORMAL.

    '

    'Anyways, this will have the Windows Shell open the Default WebBrowser with the url specified in the 3rd parameter.

    '

       

    ShellExecute  0, "open", "http://www.vbcodesource.info", vbNullString, vbNullString, 1

        

    ____________________________________

       

     I added the next codes just to show how to open another program and pass arguments to the program. This code will launch the FireFox webbrowser and open the URL in the 4th parameter.

       

    '

    'This simply shows how to open a specific program like Firefox which may not be the default webbrowser and how to get it to go to a specific url.

        ShellExecute  0, "open", "C:\Program Files\Mozilla Firefox\Firefox.exe", "http://www.msdn.com", vbNullString, 1    

    ____________________________________________

        

    Thats all there is to it!. As you can see with a simple API call you can get the job done. Anyways, have fun!

        

                        Jason

    Popularity: 48% [?]

    Topics: - (.5.0/6.0), - (.5.0/6.0), - .All VB (Related to All), .All VB (Related to All) | No Comments »

    Create Custom MY.MySettings to Memorize/Remember a Applications Size and Position in VB.NET 2005/2008

    By Jason | April 9, 2008

    Click Star to Rate Post
    1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 2.33 out of 5)
    Loading ... Loading ...
     

     

    Note: This article/tip/tutorial is from my www.vbcodesource.com page and is a year or so old. So keep that in mind...

        

    Note #2:  Also this tutorial is aimed at the Visual Basic .NET 2005 and VB 2008 versions since the earlier versions of dot net do Not have the My feature. But the principals still apply if your using a older version of vb.net.

         

       

     

       

       

    _______________________________________________

       

       

       This small tutorial is to show you how you can create your own properties to display under the 'My.MySettings' Class. This article will focus on adding Two(2x) new properties/settings called: ApplicationSize and ApplicationPosition. I will use them to store the applications position and Size when it exits or closes. Then the next time the application/program loads, it will use these settings and remember the exact Position and Size from the last time it was opened and closed. This is a potentially nice feature that you can add to your applications to give a more user-friendly experience.

       

        Once you have your project loaded, goto the 'Project' menu and select the 'Properties' option from the list, which is usually the last item listed. Then select the "Settings" Tab item. You will then see the settings grid which is used to configure the application settings.

       

    Name: This is where you put the name of your property/setting. For this tutorial, put in ApplicationPosition as a name, and ApplicationSize as another name. ApplicationSetting and ApplicationSize will actually be listed under: My.MySettings.Default.

       

    Type: Here you can select the type you want your setting/property to be. Example: Point, Size, Date, Integer, and so on. For the article, set the type for ApplicationPosition to be: 'Point'. For the ApplicationSize set to be the: 'Size' type.

       

    Scope: This is where you can specify if the property/setting can only be modified by your Application, or if the person using your application can change the setting. If 'Application' is set, then the user cannot modify the setting at runtime. If 'User' is selected, then the setting Can be modified at runtime by the user. If you want to have certain preferences that the user can modify, then 'User' is the scope you want to use. For the article I went ahead and set it to 'User'.

       

    Value: You can specify a default value for the property/setting that will be used if the property is being used for the first time or no other value have been specified at runtime. This value will also be used if you 'Reset' the properties. Example: My.MySettings.Default.Reset() will reset the settings to your original values.

       

       

    _______________________________________________

       

       

        If you are following the article as a exercise, then your Project Properties should look similar to the picture below (IF you can read it that is...

        

       

     

        

       

    _______________________________________________

       

       

        Under the: My.Settings.Default interface, you should have: My.MySettings.Default.ApplicationPosition and My.MySettings.Default.ApplicationSize available as properties that can now be used. To get your application to use these properties and enable it to remember the size and position to load, you will want to put a few lines of code in the Form_Load event, and the Form_FormClosing event.

       

    Form_Load Event put:

       

            Me.Location = My.MySettings.Default.ApplicationPosition

     

        

     

            Me.Size = My.MySettings.Default.ApplicationSize

     

        

       

    Form_FormClosing Event put:

       

     

            My.MySettings.Default.ApplicationPosition = Me.Location

     

        

     

            My.MySettings.Default.ApplicationSize = Me.Size

     

       

     

       
        Thats all there is to it. Now your application will have the ability to memorize the position and size when it was previously ran and can load in the same position and at the same size. You do NOT need to specifically use the registry or create a ini file to enable these user-friendly capabilities. 

       

        There ARE more features and properties available for creating custom properties/settings, but those will have to be covered in a future article. Hope you got something worthwhile from this small tutorial :) 

       

        Click Here for a example application with this article. 

    Popularity: 58% [?]

    Topics: - (.NET All + 05/08), - (.NET All + 05/08), - (.NET All + 05/08), - .All VB (Related to All), .All VB (Related to All), .All VB (Related to All) | No Comments »

    Check if a File or Directory Exists using Visual Basic 6.0

    By Jason | April 8, 2008

    Click Star to Rate Post
    1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 4 out of 5)
    Loading ... Loading ...
     


      There could be a time when you need to check if files or folders/directories exists or not. There are actually a few different ways to accomplish this task. Some ways is by messing around with the Dir Function located in the FileSystem Class, the Windows API or using the FileSystemObject located in the Windows Scripting Host Object Model. I will show how to use the Dir method and the FileSystem Scripting Object to check if a folder/file exits or not.

       

       

    ___________________________________________

         

        

    Using the DIR Function...

       

      This feature is built into the core of the Visual Basic 6.0 Runtime, so you don't need to add any references or components. In my experience this method works just fine, but there could be a scenerio that it may not work for you. I haven't run into one yet though.

       

      Basically what you want to do it call the Dir function while putting the path to the file or directory and if it returns nothing then its not seeing the path you specified and thus the file or directory does not exists.

       

        

        If Dir( "c:\myFile.txt") <> "" Then

            MsgBox "It Exists!"

        Else

            MsgBox "No Go!"

        End If
       

       

    Thats all there is to it! As I said, I do not know how this method would work in every scenerio, but i've seen no problems yet.

        

       

    ___________________________________________

        

       

    Using the Scripting FileSystemObject...

       

      This method is not built into the VB 5.0 or 6.0 runtime. So you will first want to go to the Project menu and click on the References item. Once all of the objects are displayed scroll down till you see - "Windows Script Host Object Model", check it then click OK.

       

      You now need to create a reference to the FileSystemObject in the scripting class you just added...

       

       

        Dim f As FileSystemObject

        Set f = New FileSystemObject

       

       

    Now you just need to call the available FileExists and FolderExists Functions while passing the path for the file and the path for the directory you want to check.

        

       

        MsgBox f.FileExists("c:\myFile.txt")

       

        MsgBox f.FolderExists("c:\")

       

       

    Depending on whether the file or folder path you specified exits or not the messagebox should have thrown a True or False message.

       

       

    ________________________________

       

       

      Thats all there is to it for doing basic checking to see if a file exists or if a folder/directory exists or not. The FileSystemObject method is more elegant and probably more reliable than using the Dir Function but at the cost of having to add a Reference to the Windows Scripting Object which is not apart of the Visual Basic 5.0 or Visual Basic 6.0 Runtime Library. The Dir method still seems to work ok for me. Also both ways appear to be case in-sensitive so you won't have to worry about the letter casing being exact. IF you know of some other ways please feel free to leave a message with the way you do it. Anyways, Have Fun!

        

                    Jason

       
       

    Popularity: 74% [?]

    Topics: - (.5.0,6.0), - (.5.0/6.0), - (.5.0/6.0), - .All VB (Related to All), .All VB (Related to All) | No Comments »

    « Previous Entries