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

May 11, 2008 – 9:59 pm








  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








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

May 11, 2008 – 9:45 pm

 






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. 








Check if a File or Directory Exists using Visual Basic 6.0

May 11, 2008 – 9:32 pm


  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

   
   

New Website Content - Added/Available (Updated!)

April 6, 2008 – 8:57 pm

  You may have or may not have noticed a few page links at the top of each page. These pages were to provide more static based information. These pages will continue to change and grow as more information or new information is found.

   

   

|

   

  Quick Update: I wanted to mention as well that a few weeks ago Microsoft released the Visual Basic 9.0 Language Specification 9.0 document. If you've wanted to know the Visual Basic language inside and out, then download and check out this huge document. You may be surprised at some coding techniques you could be using... Click here to goto the msdn download page... I may add this resource to a static page in the future.

   

|

   

 __________________________________________

   

   

FREE IDE Downloads - This page contains a small list of some available IDEs related to Visual Basic and/or Visual Basic.NET based programming. The programs listed should all be completely free of charge.

    

Service Pack Downloads - This page contains a list of all of the current service pack downloads related to Visual Basic and Visual Basic.NET. Only the latest versions will be listed here.

    

Utility Downloads - This is the newest page I added and contains a list of programs/utilities that I use on a somewhat regular basis. The page contains both programming and non-programming related programs that are Free of charge.

    

VB For Beginners - This page was mainly added for those who are basically very new to Visual Basic and Visual Basic.NET based programming. The page main contain both website listed tutorials or downloadable based tutorials like E-Books.

    

   

__________________________________________

    

   

Hopefully you will like the new content being made available to the site. As time goes on the pages should be updated with new content. I will try to remember to list new content related to each of the pages when I find something new or something I forgot about.

  

 Anyways, Have Fun!

  

               Jason

Adding Custom Tooltips - Visual Basic .NET

March 31, 2008 – 9:29 pm

 NOTE: This older article/tutorial was orginally on my vbcodesource.com website and is a few years old. So take that into consideration... :)
   
    In Visual Basic 6.0 we had a property for tooltip balloons. All you needed to do was type in the text you wanted to display. But you didn't really have much control over the toolTips in general. In Visual Basic.NET, that simple feature appears to have been removed by default. That doesn't mean we still can't have tooltips. Below you will see just how simple it is to implement this feature and control how your tooltips will react. You have more "say" in what goes on with your tooltips. For Example, you can now set the length of time the pointer must remain stationary within the tooptip region before the balloon will appear. 

    To get started, start a project. In your ToolBox look for a ToolTip component. If you do not see one, then right click inside your toolbox and select "Customize Toolbox". Click the ".Net Frameworks Componets" tab, scroll down till you find "ToolTip" then check the box and press OK. You should now see a ToolTip component available. Double click the ToolTip to add it to your project. I re-named mine toolTip. If you look at the properties available for the ToolTip you will see: Automatic Delay, Initial Delay, and such that are available. I will not go over any of these settings since they are pretty much self-explanatory. Go ahead and add a button to the form. I named mine btn. In the btn_MouseHover event put:
    

          toolTip.SetToolTip(btn, "Hello, This is just a simple test....")

      
    Start your project and place your mouse pointer within the button region and let it sit idle for a few seconds. You should then see the ToolTip balloon popup with the text you specified. You can also goto the 'btn' Button control property, you will see a property named: "Tooltip on tooltip". You can put the text you want displayed and it will use your ToolTip settings without having to execute the toolTip yourself. As you can see, it is very easy to add support for ToolTips.
    

 

    

                Jason

eXTReMe Tracker