«

»

Feb 11

FolderBrowesDialog Blank?

The problem: On a windows form there is a FolderBrowserDialog added. There is a button to use the FolderBrowserDialog.
Wen you click the button the FolderBrowserDialog is used. All word fine. Until it is deployed, then the dialog screen comes up with no tree to browse or select a folder.
Why? How?

On msdn i found some hints likeFolderBrowserDialog Class and Problem using FolderBrowser control in Setup project. This last one “solve” the problem.
But there was one think more to understand, how to pass the value to the new thread and getting it back from the thread.
This is how i did it.

Private _Directory As String = String.Empty
 
Private Sub cmdBrowse_Click(ByVal sender As System.Object,
                            ByVal e As System.EventArgs
                           ) Handles cmdBrowse.Click
    Dim thdThread As Threading.Thread = New Threading.Thread(
                New Threading.ParameterizedThreadStart(AddressOf dlgFolder))
    Dim StartPath As String = txtValue.Text
 
    'Open the FolderBrowerDialog in a other (static) thread.
    'This is done to prevent a empty (no tree) popup
    thdThread.SetApartmentState(Threading.ApartmentState.STA)
    thdThread.Start(StartPath)
    thdThread.Join()
 
    'Store result from other thread
    txtValue.Text = _Directory
 
End Sub
 
''' <summary>
''' Use folderBrowserDialog
''' </summary>
''' <param name="obj"></param>
''' <remarks>
''' Last update: EH 2012-02-11
''' To prevent problemens the advies to use STAThread is followed.
''' Do NOT use ByVal or ByRef, then you will get errors
''' </remarks>
<STAThread()> _
Public Sub dlgFolder(obj As Object)
    Dim strDirectory As String = CType(obj, String)
    Dim StartPath As String, strResult As String
 
    With FolderBrowserDialog
 
        ' Use Desktop gives acces to the network
        .RootFolder = Environment.SpecialFolder.Desktop
        If strDirectory = "" Then
            .SelectedPath = ""
        Else
            .SelectedPath = IO.Path.GetDirectoryName(strDirectory)
        End If
 
        StartPath = .SelectedPath
        strResult = StartPath
 
        If .ShowDialog = Windows.Forms.DialogResult.OK Then
            'Store result
            If StartPath <> .SelectedPath Then strResult = .SelectedPath
        End If
 
        If Not strResult.EndsWith(IO.Path.DirectorySeparatorChar) Then
            'Add missing char
            strResult &= IO.Path.DirectorySeparatorChar
        End If
 
        'Return the path to use
        _Directory = strResult
 
    End With
End Sub

HTH

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">