question

jenCarlosLopezReyes-3297 avatar image
0 Votes"
jenCarlosLopezReyes-3297 asked jenCarlosLopezReyes-3297 commented

Folder browser

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

![115608-fbdimg1.jpg][1]
[1]: /answers/storage/attachments/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                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                          
 <DllImport("User32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>                                                                                                                                                                                                                                  
 Public Shared Function MoveWindow(hWnd As IntPtr, X As Integer, Y As Integer, nWidth As Integer, nHeight As Integer, bRepaint As Boolean) As Boolean                                                                                                                                                  
 End Function                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                          
 <DllImport("User32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>                                                                                                                                                                                                                                  
 Public Shared Function FindWindowEx(ByVal hWndParent As IntPtr, ByVal hWndChildAfter As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr                                                                                                                                      
 End Function                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                          
 <DllImport("Ole32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>                                                                                                                                                                                                                                   
 Public Shared Function OleInitialize(pvReserved As IntPtr) As HRESULT                                                                                                                                                                                                                                 
 End Function                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                          
 Dim pNameSpaceTreeControl As INameSpaceTreeControl                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                             
 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load                                                                                                                                                                                                                          
     OleInitialize(IntPtr.Zero)                                                                                                                                                                                                                                                                        
     Dim CLSID_NamespaceTreeControl As New Guid("AE054212-3535-4430-83ED-D501AA6680E6")                                                                                                                                                                                                                
     Dim NameSpaceTreeControlType As Type = Type.GetTypeFromCLSID(CLSID_NamespaceTreeControl, True)                                                                                                                                                                                                    
     Dim NameSpaceTreeControl As Object = Activator.CreateInstance(NameSpaceTreeControlType)                                                                                                                                                                                                           
     pNameSpaceTreeControl = DirectCast(NameSpaceTreeControl, INameSpaceTreeControl)                                                                                                                                                                                                                   
     Dim hr As HRESULT = pNameSpaceTreeControl.Initialize(Me.Handle, Nothing, NSTCSTYLE.NSTCS_RICHTOOLTIP Or NSTCSTYLE.NSTCS_FAVORITESMODE Or NSTCSTYLE.NSTCS_FULLROWSELECT Or NSTCSTYLE.NSTCS_NOREPLACEOPEN Or NSTCSTYLE.NSTCS_AUTOHSCROLL Or NSTCSTYLE.NSTCS_HASEXPANDOS Or NSTCSTYLE.NSTCS_HASLINES)
     If (hr = HRESULT.S_OK) Then                                                                                                                                                                                                                                                                       
         Dim pidl As IntPtr = IntPtr.Zero                                                                                                                                                                                                                                                              
         hr = SHGetFolderLocation(IntPtr.Zero, CSIDL_DESKTOP, IntPtr.Zero, 0, pidl)                                                                                                                                                                                                                    
         If (hr = HRESULT.S_OK) Then                                                                                                                                                                                                                                                                   
             Dim psi As IntPtr = IntPtr.Zero                                                                                                                                                                                                                                                           
             Dim GUID_IShellItem As New Guid("43826d1e-e718-42ee-bc55-a1e261c37bfe")                                                                                                                                                                                                                   
             hr = SHCreateItemFromIDList(pidl, GUID_IShellItem, psi)                                                                                                                                                                                                                                   
             If (hr = HRESULT.S_OK) Then                                                                                                                                                                                                                                                               
                 hr = pNameSpaceTreeControl.AppendRoot(psi, SHCONTF.SHCONTF_FOLDERS Or SHCONTF.SHCONTF_NONFOLDERS, NSTCROOTSTYLE.NSTCRS_EXPANDED, IntPtr.Zero)                                                                                                                                
             End If                                                                                                                                                                                                                                                                                    
         End If                                                                                                                                                                                                                                                                                        
     End If                                                                                                                                                                                                                                                                                            
     Me.ClientSize = New Size(800, 600)                                                                                                                                                                                                                                                                
     CenterToScreen()                                                                                                                                                                                                                                                                                  
 End Sub                                                                                                                                                                                                                                                                                               
 Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize                                                                                                                                                                                                                          
     Dim hWndTree As IntPtr = FindWindowEx(Me.Handle, IntPtr.Zero, "NameSpaceTreeControl", Nothing)                                                                                                                                                                                                    
     MoveWindow(hWndTree, 0, 0, Me.ClientSize.Width, Me.ClientSize.Height, True)                                                                                                                                                                                                                       
 End Sub                                                                                                                                                                                                                                                                                               

End Class

For this reason I turn to you so that you can help me with this operation, which is complicated for me, I don't have much knowledge with this INameSpaceTreeControl interface.

The operations you would like it to do:
1.- Button1 that allows me to create an editable folder by clicking on said button.
2.- Button2 that when you press it to show the folder path of the selected item.
3.- Button3 to cancel.

I hope you can help me
Thanks.


dotnet-visual-basic
fbdimg1.jpg (17.3 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Often people have a fundamental problem to solve and they think they have a solution to the fundamental problem but then they need help with that solution. Every time I have seen that the person has chosen the wrong solution. In this case, you are trying to get the files in a directory (or something like that) and put them in a tree control and you are trying to do it the hard, very low-level way. You need to describe (1) what you need to do and ask (2) how to do it using managed classes.


0 Votes 0 ·

Hello
The code creates a TreeView with the INameSpaceTreeControl interface.
Example

116071-captura.jpg



Now what I want is, when I select the node / item from the created TreeView, it shows me the folder path in a MessageBox.Show as a result.

Thank you

0 Votes 0 ·
captura.jpg (29.7 KiB)

1 Answer

Castorix31 avatar image
0 Votes"
Castorix31 answered jenCarlosLopezReyes-3297 commented

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



· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Great, but, I would like to adapt it, that when I press a Button1 it shows me its folder path in MessageBox.Show.

is there any way to do that?


Also, is there a way to create a new folder in the TreeView created by pressing Button2?

example
116022-poppa.jpg



If possible could you help me with an example please.

0 Votes 0 ·
poppa.jpg (53.0 KiB)
Castorix31 avatar image Castorix31 jenCarlosLopezReyes-3297 ·

> Great, but, I would like to adapt it, that when I press a Button1 it shows me its folder path in MessageBox.Show.
is there any way to do that?

I don't see the problem : just display the variable sbItemName (by making it global or with another global variable), which contains the current selected folder, in a MessageBox
But why do you want to reprogram all the FolderBrowserDialog ? (it is re-inventing the wheel...)

0 Votes 0 ·

Thank you.

I tried to customize the FolderBrowserDialog control, which would do the copy operation in a project that I am doing, but it seemed very difficult, so I decided to make my own FolderBrowserDialog for my purpose.

0 Votes 0 ·
Show more comments