0

I have a FolderBrowserDialog and I would like to have (in the title) the text "Select destination folder" instead of "Browse for folder".

I know that I can customize the description with:

folderDlg.Description = "Select destination folder"

but this would be a stopgap solution (if I don't find a way to do what I want).

I also saw the answer given here for customizing the FolderBrowserDialog but I don't feel ready to do this.

Is there a more simple way?

Community
  • 1
  • 1
genespos
  • 3,211
  • 6
  • 38
  • 70
  • I actually don't think that can be done. It's a bit annoying as I know you can change the title bar on the `OpenFileDialog` by accessing the `Title` property yet the `FolderBrowserDialog` doesn't have that property unfortunately. – Bugs Oct 20 '16 at 15:50
  • What is so horrible about using `Description`? Its not like "Browse for folder" is wrong – Ňɏssa Pøngjǣrdenlarp Oct 20 '16 at 15:58
  • @Plutonix Nothing horrible, I just think that title is more visible (to the user) than the description, but it seems I'll be forced to use description – genespos Oct 20 '16 at 16:06
  • Thats true, but you are assuming that the user will be bewildered by the dialog - they have probably seen one before and know what to do. – Ňɏssa Pøngjǣrdenlarp Oct 20 '16 at 16:10
  • @Plutonix You're right, but I'm using FolderBrowserDialg to select the destination folder to save a file, and this isn't what usually happens. – genespos Oct 20 '16 at 16:14
  • Why don't you just use a [SaveFileDialog](https://msdn.microsoft.com/en-us/library/system.windows.forms.savefiledialog(v=vs.110).aspx) then? – djv Oct 20 '16 at 17:08
  • @Verdolino Good point. It's because I'm saving DataGridView data to an Excel file so I first get filename and destination folder then I automatically create and save the excel file, without showing it to the user – genespos Oct 20 '16 at 17:37
  • So you don't want the user to be able to change or even see the excel file name? – djv Oct 20 '16 at 17:46
  • @Verdolino No. The user select both file name and folder but I want to avoid that the user interfere in the creating/saving process. After he will be able to open himself the file and modify it. – genespos Oct 20 '16 at 21:07
  • SaveFileDialog only returns the filename and whether the user canceled. You still need to save the file. – djv Oct 20 '16 at 21:18
  • @Verdolino I'm trying to follow your hint but this gives me other problems. Anyway thank you for showing me another way. – genespos Oct 21 '16 at 09:55

1 Answers1

0

Declare these APIs in any module

Public Declare Auto Function FindWindow Lib "user32" _
    (ByVal ClassName As String, ByVal WindowTitle As String) As IntPtr
Public Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" _
    (ByVal hwnd As IntPtr, ByVal lpString As String) As Long

Suppose we have a FolderBrowserDialog named folderDialog in a Form named mainForm

Add this In module / class that has the Sub showing the Dialog

Imports System.Threading

Use This code in sub showing dialog

...
    Dim thr As New Thread(
        Sub()
            Dim bNoWind As Boolean = True
            Dim iPtr As IntPtr
            While bNoWind
                iPtr = FindWindow(Nothing, "Browse For Folder")
                If iPtr <> IntPtr.Zero Then
                    bNoWind = False
                    SetWindowText(iPtr, "My Folder Browser Title")
                End If
            End While
        End Sub
    )
    thr.Start()

    mainForm.folderDialog.ShowDialog()
...

The idea is to start a new thread before showing the Dialog, because that showing the dialog pauses until "OK" or "Cancel" is pressed. The new tread starts searching for a window with the default title. When it is shown, it changes the title and the searching routine ends.

After closing the Browser window, the tread is closed.

This thread can be moved to any general function, to change Title on any type of window that Title can't be changed:

Imports System.Threading
Module modWindow

    Public Declare Auto Function FindWindow Lib "user32" _
        (ByVal ClassName As String, ByVal WindowTitle As String) As IntPtr
    Public Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" _
        (ByVal hwnd As IntPtr, ByVal lpString As String) As Long

    Public thrWindow AsTread

    Sub ChangeWindowTitle(sDefault as String, sCustom as String)
        thrWindow = Nothing
        thrWindow = new Thread(
            Sub()
                Dim bNoWind As Boolean = True
                Dim iPtr As IntPtr
                While bNoWind
                    iPtr = FindWindow(Nothing, sDefault)
                    If iPtr <> IntPtr.Zero Then
                        bNoWind = False
                        SetWindowText(iPtr, sCustom)
                    End If
                End While
            End Sub
        )
        thrWindow.Start()
    End Sub

End Module

And in sub showing dialog:

    ...
    ChangeWindowTitle("Browse For Folder", "My Folder Browser Title")
    mainForm.folderDialog.ShowDialog()
    ...
AlexKots
  • 81
  • 1
  • 2