by Ross Mack - GUI Computing
One of the problems with some of the controls in Visual Basic is that they do not always seem to support all the features of the operating system. One such case is the DriveListBox that ships standard with VB. This thing is great for allowing the user to select drives. It even does mapped network drives. But it does not list UNC paths. When you think about it there are a large number of goood reasons why this is to. It would have to list all available UNC paths in Network Neighboorhood in some cases, and that is not what you want it to do.
However, in a recent application development I was using the combination of the DriveListBox and the DirectoryListBox to allow the user to select a directory. That's fine except when it came to UNC paths. They weren't displayed and the user had no way of selecting them even if that's what the current setting was. I had to come up with a fix of some sort. Turns out the fix was easier than I expected. After combing through the online Knowledgebase I sat down, had an extra strength coffee and thought about it for a minute. Then it struck me that the DriveListBox really was a standard combo that has been subclassed to have special contents. If I were lucky then it should still respond to standard combo box messages.
There was no direct way to add an item to the combo, but if I could do it through the SendMessage API that would at least do.
The following declarations need to go in a module somewhere:
Declare Function SendMessageString Lib "user32" Alias "SendMessageA" _ (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long Public Const CB_ADDSTRING = &H143
Then when you want to add a UNC path to the DriveListBox just make a call like the following:
SendMessageString Drive1.hWnd, CB_ADDSTRING, 0, "\\MyServer\MyShare"
Blow me down it actually works. It even works fine in conjunction with a Directory listbox. The one problem is this, when you select an item in the DriveListBox by setting the Drive property the drive list is refreshed and suddenly your new entry goes missing.
The simple way around this is to select drives directly with the API. Here is the code:
Public Const CB_SELECTSTRING = &H14D SendMessageString Drive1.hwnd, CB_SELECTSTRING, 0, "\\MyServer\MyShare"
I hope this helps someone else out there.
Oh, and while I'm at it, here are a couple of related Knowledgebase articles on Microsoft's web site.