Folder browser

jenCarlos 141 Reputation points
2021-07-18T22:28:36+00:00

Hello...
What I would like to do is similar to FolderBrowserDialog1 with the same behavior.

115608-fbdimg1.jpg
I found this code, which allows to build, folder browser, but I can't get the folder path when selecting the desired item.

Public Class Form1
Public Enum HRESULT As Integer
S_OK = 0
S_FALSE = 1
E_NOINTERFACE = &H80004002
E_NOTIMPL = &H80004001
E_FAIL = &H80004005
End Enum

<StructLayout(LayoutKind.Sequential)>                                                                                                                                                                                                                                                                   
Public Structure RECT                                                                                                                                                                                                                                                                                   
    Public Left As Integer                                                                                                                                                                                                                                                                              
    Public Top As Integer                                                                                                                                                                                                                                                                               
    Public Right As Integer                                                                                                                                                                                                                                                                             
    Public Bottom As Integer                                                                                                                                                                                                                                                                            
End Structure                                                                                                                                                                                                                                                                                           

Public Enum NSTCSTYLE As Integer                                                                                                                                                                                                                                                                        
    NSTCS_HASEXPANDOS = &H1                                                                                                                                                                                                                                                                             
    NSTCS_HASLINES = &H2                                                                                                                                                                                                                                                                                
    NSTCS_SINGLECLICKEXPAND = &H4                                                                                                                                                                                                                                                                       
    NSTCS_FULLROWSELECT = &H8                                                                                                                                                                                                                                                                           
    NSTCS_SPRINGEXPAND = &H10                                                                                                                                                                                                                                                                           
    NSTCS_HORIZONTALSCROLL = &H20                                                                                                                                                                                                                                                                       
    NSTCS_ROOTHASEXPANDO = &H40                                                                                                                                                                                                                                                                         
    NSTCS_SHOWSELECTIONALWAYS = &H80                                                                                                                                                                                                                                                                    
    NSTCS_NOINFOTIP = &H200                                                                                                                                                                                                                                                                             
    NSTCS_EVENHEIGHT = &H400                                                                                                                                                                                                                                                                            
    NSTCS_NOREPLACEOPEN = &H800                                                                                                                                                                                                                                                                         
    NSTCS_DISABLEDRAGDROP = &H1000                                                                                                                                                                                                                                                                      
    NSTCS_NOORDERSTREAM = &H2000                                                                                                                                                                                                                                                                        
    NSTCS_RICHTOOLTIP = &H4000                                                                                                                                                                                                                                                                          
    NSTCS_BORDER = &H8000                                                                                                                                                                                                                                                                               
    NSTCS_NOEDITLABELS = &H10000                                                                                                                                                                                                                                                                        
    NSTCS_TABSTOP = &H20000                                                                                                                                                                                                                                                                             
    NSTCS_FAVORITESMODE = &H80000                                                                                                                                                                                                                                                                       
    NSTCS_AUTOHSCROLL = &H100000                                                                                                                                                                                                                                                                        
    NSTCS_FADEINOUTEXPANDOS = &H200000                                                                                                                                                                                                                                                                  
    NSTCS_EMPTYTEXT = &H400000                                                                                                                                                                                                                                                                          
    NSTCS_CHECKBOXES = &H800000                                                                                                                                                                                                                                                                         
    NSTCS_PARTIALCHECKBOXES = &H1000000                                                                                                                                                                                                                                                                 
    NSTCS_EXCLUSIONCHECKBOXES = &H2000000                                                                                                                                                                                                                                                               
    NSTCS_DIMMEDCHECKBOXES = &H4000000                                                                                                                                                                                                                                                                  
    NSTCS_NOINDENTCHECKS = &H8000000                                                                                                                                                                                                                                                                    
    NSTCS_ALLOWJUNCTIONS = &H10000000                                                                                                                                                                                                                                                                   
    NSTCS_SHOWTABSBUTTON = &H20000000                                                                                                                                                                                                                                                                   
    NSTCS_SHOWDELETEBUTTON = &H40000000                                                                                                                                                                                                                                                                 
    NSTCS_SHOWREFRESHBUTTON = &H80000000                                                                                                                                                                                                                                                                
End Enum                                                                                                                                                                                                                                                                                                

Public Enum SHCONTF As Integer                                                                                                                                                                                                                                                                          
    SHCONTF_CHECKING_FOR_CHILDREN = &H10                                                                                                                                                                                                                                                                
    SHCONTF_FOLDERS = &H20                                                                                                                                                                                                                                                                              
    SHCONTF_NONFOLDERS = &H40                                                                                                                                                                                                                                                                           
    SHCONTF_INCLUDEHIDDEN = &H80                                                                                                                                                                                                                                                                        
    SHCONTF_INIT_ON_FIRST_NEXT = &H100                                                                                                                                                                                                                                                                  
    SHCONTF_NETPRINTERSRCH = &H200                                                                                                                                                                                                                                                                      
    SHCONTF_SHAREABLE = &H400                                                                                                                                                                                                                                                                           
    SHCONTF_STORAGE = &H800                                                                                                                                                                                                                                                                             
    SHCONTF_NAVIGATION_ENUM = &H1000                                                                                                                                                                                                                                                                    
    SHCONTF_FASTITEMS = &H2000                                                                                                                                                                                                                                                                          
    SHCONTF_FLATLIST = &H4000                                                                                                                                                                                                                                                                           
    SHCONTF_ENABLE_ASYNC = &H8000                                                                                                                                                                                                                                                                       
    SHCONTF_INCLUDESUPERHIDDEN = &H10000                                                                                                                                                                                                                                                                
End Enum                                                                                                                                                                                                                                                                                                
Public Enum NSTCROOTSTYLE As Integer                                                                                                                                                                                                                                                                    
    NSTCRS_VISIBLE = 0                                                                                                                                                                                                                                                                                  
    NSTCRS_HIDDEN = &H1                                                                                                                                                                                                                                                                                 
    NSTCRS_EXPANDED = &H2                                                                                                                                                                                                                                                                               
End Enum                                                                                                                                                                                                                                                                                                
Public Enum NSTCITEMSTATE As Integer                                                                                                                                                                                                                                                                    
    NSTCIS_NONE = 0                                                                                                                                                                                                                                                                                     
    NSTCIS_SELECTED = &H1                                                                                                                                                                                                                                                                               
    NSTCIS_EXPANDED = &H2                                                                                                                                                                                                                                                                               
    NSTCIS_BOLD = &H4                                                                                                                                                                                                                                                                                   
    NSTCIS_DISABLED = &H8                                                                                                                                                                                                                                                                               
    NSTCIS_SELECTEDNOEXPAND = &H10                                                                                                                                                                                                                                                                      
End Enum                                                                                                                                                                                                                                                                                                

Public Enum NSTCGNI As Integer                                                                                                                                                                                                                                                                          
    NSTCGNI_NEXT = 0                                                                                                                                                                                                                                                                                    
    NSTCGNI_NEXTVISIBLE = 1                                                                                                                                                                                                                                                                             
    NSTCGNI_PREV = 2                                                                                                                                                                                                                                                                                    
    NSTCGNI_PREVVISIBLE = 3                                                                                                                                                                                                                                                                             
    NSTCGNI_PARENT = 4                                                                                                                                                                                                                                                                                  
    NSTCGNI_CHILD = 5                                                                                                                                                                                                                                                                                   
    NSTCGNI_FIRSTVISIBLE = 6                                                                                                                                                                                                                                                                            
    NSTCGNI_LASTVISIBLE = 7                                                                                                                                                                                                                                                                             
End Enum                                                                                                                                                                                                                                                                                                


<ComImport>                                                                                                                                                                                                                                                                                             
<Guid("028212A3-B627-47e9-8856-C14265554E4F")>                                                                                                                                                                                                                                                          
<InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>                                                                                                                                                                                                                                                   
Public Interface INameSpaceTreeControl                                                                                                                                                                                                                                                                  
    Function Initialize(hwndParent As IntPtr, ByRef prc As RECT, nsctsFlags As NSTCSTYLE) As HRESULT                                                                                                                                                                                                    
    Function TreeAdvise(punk As IntPtr, ByVal pdwCookie As Integer) As HRESULT                                                                                                                                                                                                                          
    Function TreeUnadvise(pdwCookie As Integer) As HRESULT                                                                                                                                                                                                                                              
    Function AppendRoot(psiRoot As IntPtr, grfEnumFlags As SHCONTF, grfRootStyle As NSTCROOTSTYLE, pif As IntPtr) As HRESULT                                                                                                                                                                            
    Function InsertRoot(iIndex As Integer, psiRoot As IntPtr, grfEnumFlags As SHCONTF, grfRootStyle As NSTCROOTSTYLE, pif As IntPtr) As HRESULT                                                                                                                                                         
    Function RemoveRoot(psiRoot As IntPtr) As HRESULT                                                                                                                                                                                                                                                   
    Function RemoveAllRoots() As HRESULT                                                                                                                                                                                                                                                                
    Function GetRootItems(ByVal ppsiaRootItems As IntPtr) As HRESULT                                                                                                                                                                                                                                    
    Function SetItemState(psi As IntPtr, nstcisMask As NSTCITEMSTATE, nstcisFlags As NSTCITEMSTATE) As HRESULT                                                                                                                                                                                          
    Function GetItemState(psi As IntPtr, nstcisMask As NSTCITEMSTATE, ByVal pnstcisFlags As NSTCITEMSTATE) As HRESULT                                                                                                                                                                                   
    Function GetSelectedItems(ByVal psiaItems As IntPtr) As HRESULT                                                                                                                                                                                                                                     
    Function GetItemCustomState(psi As IntPtr, ByVal piStateNumber As Integer) As HRESULT                                                                                                                                                                                                               
    Function SetItemCustomState(psi As IntPtr, iStateNumber As Integer) As HRESULT                                                                                                                                                                                                                      
    Function EnsureItemVisible(psi As IntPtr) As HRESULT                                                                                                                                                                                                                                                
    Function SetTheme(pszTheme As String) As HRESULT                                                                                                                                                                                                                                                    
    Function GetNextItem(psi As IntPtr, nstcgi As NSTCGNI, ByVal ppsiNext As IntPtr) As HRESULT                                                                                                                                                                                                         
    Function HitTest(ppt As Point, nstcgi As NSTCGNI, ByVal ppsiOut As IntPtr) As HRESULT                                                                                                                                                                                                               
    Function GetItemRect(psi As IntPtr, ByRef prect As RECT) As HRESULT                                                                                                                                                                                                                                 
    Function CollapseAll() As HRESULT                                                                                                                                                                                                                                                                   
End Interface                                                                                                                                                                                                                                                                                           

Public CSIDL_DESKTOP As Integer = 0                                                                                                                                                                                                                                                                     

<DllImport("Shell32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>                                                                                                                                                                                                                                   
Public Shared Function SHGetFolderLocation(ByVal hwndOwner As IntPtr, ByVal nFolder As Integer, ByVal hToken As IntPtr, ByVal dwReserved As UInteger, <Out> ByRef ppidl As IntPtr) As HRESULT                                                                                                           
End Function                                                                                                                                                                                                                                                                                            

<DllImport("Shell32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>                                                                                                                                                                                                                                   
Public Shared Function SHCreateItemFromIDList(ByVal pidl As IntPtr, <[In], MarshalAs(UnmanagedType.LPStruct)> ByVal riid As Guid, <Out> ByRef ppv As IntPtr) As HRESULT                                                                                                                                 
End Function                                                                               
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,722 questions
{count} votes

Accepted answer
  1. Castorix31 85,116 Reputation points
    2021-07-19T21:43:18.357+00:00

    I had posted a method in this thread : Populating a listbox with files / folders from a root folder path

    with WM_NOTIFY (not the best way as I wrote...) to get the current selected item
    I get the name in the sbItemName variable


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.