Migrating SHBrowseForFolder dialog into .Net

MKoslof
04-01-2004, 07:29 PM
A big, growing challenge for VB programmers is migrating their VB apps into the .Net Framework. One of the biggest challenges is getting Win 32 API calls and functions to run successfully in .Net. There is a learning curve between VB and VB.Net and this code sample will show you how to create a public class for implementing the popular BrowseForFolder API dialog boxes. This Public class provides several functions for calling different dialog boxes such as:

setting a default directory (the initial function I am calling with my button_click example), browsing only for other computers on a network, browsing for only directory folders, browsing for files (no call back procedure for setting the initial directory), browsing via the CISDL, etc.

Since this code piece is lengthy, it will not fit on one thread. So instead of breaking the code up over several threads (this may get confusing) I have copied the whole code piece into a text file. This text file is attached. It will open in any text editor, such as TexPad or NotePad. You can simply copy this code into a standard module..it should work as is. I recommend using it in the 1.1 Framework, I have not tested it in 1.0, but I imagine it would still compile OK. To call any of the functions in this public class, say in a button click, you simply have to instantiate a new class instance like so:

In the New clsFolderBrowser constructor, you just need to pass in the handle you want, I have signified that by putting "handle" in the slot provided.



Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim strString As String
Dim xClass As clsFolderBrowser = New clsFolderBrowser(Handle)

strString = xClass.BrowseForFiles("C:\Testing")
Me.TextBox1.Text = strString

End Sub

OnErr0r
04-01-2004, 08:24 PM
That's an excellent Win32 example. I should mention that the BrowseForFolder dialog is also accessible via the native FolderBrowserDialog class.

You could do something like this:

Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click
Dim fbd As New FolderBrowserDialog

fbd.Description = "Select a folder:" ' Add a caption
fbd.ShowNewFolderButton = False ' Don't show the button
fbd.RootFolder = Environment.SpecialFolder.Desktop ' Start at the desktop

If fbd.ShowDialog() = DialogResult.OK Then
Debug.WriteLine(fbd.SelectedPath)
End If
End Sub

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum