Can I change the title of my FolderBrowserDialog?
Asked Answered
M

5

26

I'm curious and it could give my little app a nice finishing touch. Thanks!

Mckeon answered 19/8, 2009 at 2:28 Comment(0)
Q
32

You can't if you use the class FolderBrowserDialog directly. But I read somewhere that it could be possible to change the title with P/Invoke and sending WM_SETTEXT message.

In my opinion, it is not worth the pain. Just use the property Description to add the information:

FolderBrowserDialog dlg = new FolderBrowserDialog();
dlg.Description = "Select the document folder";
Quesnay answered 19/8, 2009 at 2:40 Comment(3)
it there any way we change the title of "FolderBrowserDialog" . Because "dlg.Description" just show Description.Helban
Since .NET Core 3 you can use the UseDescriptionForTitle property on FolderBrowserDialog. learn.microsoft.com/en-us/dotnet/api/…Ray
Setting the UseDescriptionForTitle did the job for me! Thanks @RayKathie
R
3

The simple answer is that you can't. The dialog displays using the standard title for a folder browser style dialog on Windows. The best option is to ensure that you have meaningful descriptive text by setting the Description property.

Even if you were to use P/Invoke to call the SHBrowseForFolder Win32 API function directly, the only option you still can't change the actual title of the dialog. You can set the lpszTitle field of the BROWSEINFO structure, which is

A pointer to a null-terminated string that is displayed above the tree view control in the dialog box. This string can be used to specify instructions to the user.

Rockabilly answered 19/8, 2009 at 3:8 Comment(1)
If you use p/invoke, changing the title should be no problem. The documentation says that "If you implement a callback function, specified in the lpfn member of the BROWSEINFO structure, you receive a handle to the dialog box". That handle unlocks use of the entire Win32 API for customizations.Welty
I
1

Yes you can. There is a property for the FolderBrowserDialog class called UseDescriptionForTitle that indicates whether to use the value of the Description property as the dialog title.

Here is a code snippet :

var dialog = new System.Windows.Forms.FolderBrowserDialog() 
{
    Description = "Choose a folder",
    UseDescriptionForTitle = true
}
Isidroisinglass answered 15/11, 2023 at 9:30 Comment(1)
This is the best answer for modern WinForms. Useful to note here that UseDescriptionForTitle = true also removes the description text next to the selected folder.Cristacristabel
U
0

You can change it by using:

SetWindowText (hwnd, "Select a Folder");

Where hwnd is the window handle, which triggers the Browse For Folder Dialog.

Unconcern answered 23/4, 2019 at 2:46 Comment(0)
C
0

I was looking up how to do this, but largely had to figure it out on my own. I hope this saves anyone some time:

Above my main method, I put:

    [DllImport("user32.dll", EntryPoint = "SetWindowText", CharSet = CharSet.Ansi)]
    public static extern bool SetWindowText(IntPtr hWnd, String strNewWindowName);
    [DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Ansi)]
    public static extern IntPtr FindWindow(string className, string windowName);

Since I know that the thread will pause while the dialog is shown, I created a new thread to check for that dialog window while it exists. Like so:

    bool notfound = true;
    new Thread(() => { 
    while (notfound)
    {
        //looks for a window with the title: "Browse For Folder"
        IntPtr ptr = FindWindow(null, "Browse For Folder");
        if (ptr != IntPtr.Zero)
        {
            //tells the while loop to stop checking
            notfound = false;
            //changes the title
            SetWindowText(ptr, "Be happy!");
        }
    }
    }).Start();

Then, I initiate the dialog:

    using (var fbd = new FolderBrowserDialog())
    {
        DialogResult result = fbd.ShowDialog();
        if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
        {
            //do stuff
        }
    }

This has worked for me, and it's not so complicated. Hope this helps anyone that might stumble on this page. By the way, remember that the thread must start before the dialog window is initiated, so that it can run and check for the window as soon as it exists.

Companionway answered 25/6, 2019 at 5:19 Comment(1)
Just a couple of notes: (1) make sure the thread is created immediately before showing the dialog, as it'll max out a CPU core until it finds the dialog, and (2) be aware that this code only works in English - if you're running Windows in any other language, it will never find the dialog it's looking for, so it'll spin up a new thread each time you open a dialog and none of them will ever finish.Penelope

© 2022 - 2024 — McMap. All rights reserved.