Click here to Skip to main content
16,007,858 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: changing columwidth dynamically Pin
magnifique8-Feb-05 0:26
magnifique8-Feb-05 0:26 
GeneralRe: changing columwidth dynamically Pin
Stephan Wright9-Feb-05 0:57
Stephan Wright9-Feb-05 0:57 
GeneralRe: changing columwidth dynamically Pin
Stephan Wright9-Feb-05 2:14
Stephan Wright9-Feb-05 2:14 
Generalproblem in sending sms webservice Pin
iram aziz7-Feb-05 19:28
sussiram aziz7-Feb-05 19:28 
GeneralRe: problem in sending sms webservice Pin
Dave Kreskowiak8-Feb-05 1:13
mveDave Kreskowiak8-Feb-05 1:13 
GeneralDynamic Property Addition to PropertyGrid Ctl (VB.Net) Pin
Member 16899447-Feb-05 17:25
Member 16899447-Feb-05 17:25 
GeneralUsing open file dialog to open my document folder Pin
Mekong River7-Feb-05 15:27
Mekong River7-Feb-05 15:27 
GeneralRe: Using open file dialog to open my document folder Pin
rwestgraham7-Feb-05 20:46
rwestgraham7-Feb-05 20:46 
Kind of complicated because you have to get the location of the My Documents folder from the shell. The following is the code for a simple form with a single brows button that finds the My Documents folder and opens a file dialog to that folder.

You should break out the shell code into a module so you can reuse it. I just included it in a form so you can cut and paste until you understand how it works. Create a form, add a button - btnBrowse and a FileDialog control, and the add the code as follows:

____________________________________________________________________________________________



'Import the InteropServices Class to enable PInvoke calls
Imports System.Runtime.InteropServices
'Import the Text Class to enable string functions
Imports System.Text

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

(This code was deleted for purpose of this example)

#End Region



'Visual Basic representation of the IMalloc interface.
<InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("00000002-0000-0000-C000-000000000046")> _
Private Interface IMalloc
<PreserveSig()> Function Alloc(ByVal cb As Integer) As IntPtr
<PreserveSig()> Function Realloc(ByVal pv As IntPtr, ByVal cb As Integer) As IntPtr
<PreserveSig()> Sub Free(ByVal pv As IntPtr)
<PreserveSig()> Function GetSize(ByVal pv As IntPtr) As Integer
<PreserveSig()> Function DidAlloc(ByVal pv As IntPtr) As Integer
<PreserveSig()> Sub HeapMinimize()
End Interface

'Function used to allocate memory when needed by the Shell APIs
Private Declare Function SHGetMalloc Lib "shell32" Alias "SHGetMalloc" _
(ByRef ppMalloc As IMalloc) _
As Integer


'Function to retrieve the PPIDL code for a special folder such as the Desktop folder.
Private Declare Function SHGetSpecialFolderLocation Lib "shell32" Alias "SHGetSpecialFolderLocation" _
(ByVal hwndOwner As IntPtr, _
ByVal nFolder As Integer, _
ByRef ppidl As IntPtr) _
As Integer


'Function to retrieve the string path for a folder from an input PPIDL code.
Private Declare Function SHGetPathFromIDList Lib "shell32" Alias "SHGetPathFromIDList" _
(ByVal pidl As IntPtr, _
ByVal Path As StringBuilder) _
As Integer




'Define a constant for Maximum folder path length
Private Const MAX_PATH As Integer = 260

'Define constants for standard IDL's of special folders
Private Const CSIDL_DESKTOP As Integer = &H0 'Desktop
Private Const CSIDL_INTERNET As Integer = &H1 'Internet Explorer Folder
Private Const CSIDL_PROGRAMS As Integer = &H2 'Start Menu\Programs Folder
Private Const CSIDL_CONTROLS As Integer = &H3 'Control Panel
Private Const CSIDL_PERSONAL As Integer = &H5 'MyDocuments
Private Const CSIDL_FAVORITES As Integer = &H6 'Favorites
Private Const CSIDL_STARTUP As Integer = &H7 'Start Menu\Programs\Startup
Private Const CSIDL_STARTMENU As Integer = &HB 'Start Menu
Private Const CSIDL_DRIVES As Integer = &H11 'MyComputer
Private Const CSIDL_WINDOWS As Integer = &H24 'Windows Folder
Private Const CSIDL_SYSTEM As Integer = &H25 'WinSys Folder
Private Const CSIDL_PROGRAM_FILES As Integer = &H26 'C:\Program Files
Private Const CSIDL_MYPICTURES As Integer = &H27 'C:\Program Files\My Pictures


'Helper function that returns the IMalloc interface used by the shell.
Private Function GetSHMalloc() As IMalloc
Dim malloc As IMalloc
SHGetMalloc(malloc)
Return malloc
End Function

'Call this function with one of the above contants to return path string
Private Function GetSpecialFolderPath(ByVal csidlFolder As Integer) As String

'Declare and initialize a variable for the folder pidl
Dim pidlFolder As IntPtr = IntPtr.Zero

'Declare and initialize a variable for the owner handle - can be null in this case
Dim hWndOwner As IntPtr = IntPtr.Zero

'Get the IDL for the specifed Folder.
SHGetSpecialFolderLocation(hWndOwner, csidlFolder, pidlFolder)

'Check to make sure we got a pointer to the folder
If (pidlFolder.Equals(IntPtr.Zero) = True) Then
'Error handling here
End If

Dim sbFolderPath As StringBuilder = New StringBuilder(MAX_PATH)

'Get the string path from the pidl
If (0 = SHGetPathFromIDList(pidlFolder, sbFolderPath)) Then
'Error handling here
End If

'Free memory
Dim malloc As IMalloc = GetSHMalloc()
If (pidlFolder.Equals(IntPtr.Zero) = False) Then
malloc.Free(pidlFolder)
End If

' Convert to a string and return.
Return sbFolderPath.ToString()

End Function

'Click browse button to open to My Documents
Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
Dim strMyDocsPath As String = GetSpecialFolderPath(CSIDL_PERSONAL)
Me.OpenFileDialog1.InitialDirectory = strMyDocsPath
Me.OpenFileDialog1.ShowDialog()
End Sub

End Class

_____________________________________________________________________________________________


GeneralRe: Using open file dialog to open my document folder Pin
Dave Kreskowiak8-Feb-05 1:09
mveDave Kreskowiak8-Feb-05 1:09 
GeneralRe: Using open file dialog to open my document folder Pin
rwestgraham8-Feb-05 8:57
rwestgraham8-Feb-05 8:57 
GeneralRe: Using open file dialog to open my document folder Pin
Mekong River9-Feb-05 4:23
Mekong River9-Feb-05 4:23 
GeneralRe: Using open file dialog to open my document folder Pin
Dave Kreskowiak8-Feb-05 1:10
mveDave Kreskowiak8-Feb-05 1:10 
GeneralRe: Using open file dialog to open my document folder Pin
Mekong River9-Feb-05 4:21
Mekong River9-Feb-05 4:21 
GeneralArrays Pin
Catalyst5007-Feb-05 12:02
Catalyst5007-Feb-05 12:02 
GeneralRe: Arrays Pin
numbrel7-Feb-05 14:51
numbrel7-Feb-05 14:51 
GeneralRe: Arrays Pin
Catalyst5008-Feb-05 11:24
Catalyst5008-Feb-05 11:24 
GeneralRe: Arrays Pin
Anonymous9-Feb-05 1:35
Anonymous9-Feb-05 1:35 
GeneralRe: Arrays Pin
Stephan Wright9-Feb-05 1:43
Stephan Wright9-Feb-05 1:43 
GeneralParameters problem using CR and VB.NET Pin
jlizardo7-Feb-05 11:47
jlizardo7-Feb-05 11:47 
GeneralExcel2000 Problem using Process.Start Pin
sean087-Feb-05 3:52
sean087-Feb-05 3:52 
GeneralClose Button But No Icon Pin
Sumit Domyan7-Feb-05 1:59
Sumit Domyan7-Feb-05 1:59 
GeneralRe: Close Button But No Icon Pin
Dave Kreskowiak7-Feb-05 3:25
mveDave Kreskowiak7-Feb-05 3:25 
GeneralRe: Close Button But No Icon Pin
Sumit Domyan7-Feb-05 16:41
Sumit Domyan7-Feb-05 16:41 
GeneralRe: Close Button But No Icon Pin
Dave Kreskowiak8-Feb-05 0:59
mveDave Kreskowiak8-Feb-05 0:59 
GeneralRe: Close Button But No Icon Pin
rwestgraham7-Feb-05 10:31
rwestgraham7-Feb-05 10:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.