In my program, I need to force the user to select the save folder. I’m doing this by setting the SelectedPath to be MyComputer.
My problem is the OK button is not initially disabled.
Behaviour:
- “My Computer” is selected (light shade of grey) but the OK button is enabled.
- Clicking on “My Computer” (selection now shaded dark blue) still has the OK button selected.
- If I select say Desktop and then re-select “My Computer”, the OK button only then gets disabled.
I’ve tried playing around with both the SelectedPath and RootFolder, along with Environment.SpecialFolder.MyComputer and Environment.GetPath() to no avail.
How can I disable the OK button when the SelectedPath is not a valid folder?
EDIT:
This is running on Windows XP, .Net 4.0, developed in Visual Studio 2010 Professional.
EDIT #2: Full example code added.
using System;
using System.Windows.Forms;
using System;
using System.Windows.Forms;
namespace FolderBrowser
{
public class Form1 : Form
{
private System.ComponentModel.IContainer components;
private System.Windows.Forms.Button button1;
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
this.button1.Location = new System.Drawing.Point(10, 10);
this.button1.Text = "Open FolderBrowserDialog";
this.button1.Click += new System.EventHandler(this.SelectSaveFolderItem_Click);
this.Controls.Add(this.button1);
this.ResumeLayout(false);
}
public Form1()
{
InitializeComponent();
}
private void SelectSaveFolderItem_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.SelectedPath = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}";
fbd.ShowDialog();
}
}
Ref: MSDN: Disable a button in a folder browser dialog
In the above msdn thread a user wished to disable the accept button in folder browser dialog when a directory didn’t exist. I feel this relates to your question:
How can I disable the OK button when the SelectedPath is not a valid folder?The solution in that thread is to use a Timer event to disable the OK Button after the dialog is shown. I converted the VB.Net code to C# for you: