home
home profile hosting info news fun stuff Technical Stuff support contact us
VISUAL BASIC PROGRAMMING TIPS

Even though Microsoft has released Visual Studio.NET a while ago now, there are still loads of companies/people using Visual Basic, mostly due to their legacy software which was initially written in VB, or because they already have a VB developer license and, for the kind of programs they write, it might be too expensive to invest in a VB.NET license. I guess this section really applies to these folks -- all the others please look away :-)

Tips

Start the Default Program Registered for File Type

This isn't something out of ordinary, but I figured out it might be worth mentioning it here since I had quite a few people asking me about how to do this or how to find out which program is associated with a file type in Windows, in order to be able to start the program and pass the file name in the command line. Needless to say, that is not the way of opening a file with the registered application in Windows! Two reasons for that:

  1. Simply passing the file name as a parameter to a program doesn't necessarily open the file with that program. Certain programs expect an extra parameter before the file name which denotes the action that the program should take against that file (e.g. /open, /print, --file etc.).
  2. Some Windows program expects that the name of the file to be opened is passed via DDE, so simply passing it in the command line would mean nothing, the program will probably just discard it!

(There's actually a 3rd reason as well, which is quite obvious: not everyone configures or installs the programs in the same location -- so just hardcoding a path like C:\Program Files\Microsoft Office\msaccess.exe in your program will not necessarily work! Granted, it will probably work on more than half of the computers you will be running your code on, as most users accept the default settings during installations, however, there will still be a percentage of PC's this will not work on.)
The right way of doing it is shown below, and it is done via Windows API -- this ensures that the file is opened same as when you double-click on it in Explorer:

Public Const SW_SHOWNORMAL = 1
Public 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

... (code goes here) ...
ShellExecute hwnd, "open", Name of the file, vbNullString, vbNullString, SW_SHOWNORMAL

... (code goes here) ...


The crucial parameter is "open" which ensures that Windows opens the file. If you look at the MSDN comments for ShellExecute you will find out that other possible values include print also, if you want to print a file.

Download the source.
Read our download disclaimer.

Create an Outlook-like Control

Even though this is such old news, there are still people out there it seems requesting something that looks like the Outlook bar :-) So here it is -- not a big deal, just a couple of PictureBoxs and CommandButtons and loading/unloading controls dynamically on a UserControl.

Download the source.
Read our download disclaimer.

Add a Horizontal Scrollbar to a Listbox

This came to my attention on Experts Exchange when I realized that there are still people out there struggling with the lack of horizontal scrollbars in the ListBox control in VB. (I still don't know what was in the minds of the developers from Microsoft when they decided not to implement this facility -- nothing, more than likely I would say :D) So anyway, here it is: a quick way to add a horizontal scrollbar to your ListBox controls and even more, a way to compute the width of it.

' Add the following 2 lines to your declaration section
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Const LB_SETHORIZONTALEXTENT As Long = &H194

' Where you want to show the scrollbar, just call the function like this:
SendMessage YourListBox.hWnd, LB_SETHORIONTALEXTENT, width for scrollbar, 0


Note that the width for scrollbar is in pixels. Also note that a value of 0 (zero) will hide the horizontal scrollbar. And since quite often you will probably add items to the list dynamically, at runtime, you might not know the width of items inside the list. In order to cater for that, you can use the following function to show the scrollbar and compute the width required:

Public Sub AddScrollBar( ByRef lst As ListBox )
  Dim i As Integer
  Dim m As Integer
  Dim tmp As Integer

  ' Compute maximum list item width in pixels
  m = 0
  For i = 0 To lst.ListCount - 1
   tmp = ScaleX( Me.TextWidth(lst.List(i)), ScaleMode, vbPixels )
   If tmp > m Then m = tmp
  Next i

  SendMessage lst.hWnd, LB_SETHORIZONTALEXTENT, m, 0
End Sub

Go back to the Technical page.

Go back to the main page.

Home | Profile | Hosting | Info | News | Fun Stuff | Tech | Support | Contact Us | J2ME tips and news

© Copyright liviutudor.com.
Based on a template from TemplatesBox.com

Valid CSS! Valid XHTML 1.0 Transitional Get FireFox! Powered by Debian Linux Powered by Apache Web Server No Software Patents View My profile on LinkedIn

Interesting site about getting Londoners chatting during their daily commute I Can Talk, Me!
Liviu Tudor personal web page details hobbies also loads of technical tips and fun stuff for you to browse code to download free and applications Java J2ME mobile code free mobile Java J2ME applications to download and use on your mobile phone read technical Java J2ME articles details for Liviu Tudor Lipu Liv have a look at the technical tips provided for various technical categories fun stuff funny quotes ASCII art drawing ascii bitmaps fun knowledge base technical tips programming sysadmin windows Java java knowledge base