1

私のプログラムでは、ユーザーに保存フォルダーを選択させる必要があります。これを行うには、SelectedPathをMyComputerに設定します。

私の問題は、[OK]ボタンが最初は無効になっていないことです。

行動:

  • 「マイコンピュータ」が選択されていますが(灰色の薄い色合い)、[OK]ボタンが有効になっています。
  • [マイコンピュータ]をクリックすると(選択範囲が濃い青で表示されます)、[OK]ボタンが選択されたままになります。
  • 「デスクトップ」を選択してから「マイコンピュータ」を再度選択すると、[OK]ボタンが無効になります。

SelectedPathとRootFolderの両方を、Environment.SpecialFolder.MyComputerとEnvironment.GetPath()とともに試してみましたが、役に立ちませんでした。

SelectedPathが有効なフォルダでない場合、どうすれば[OK]ボタンを無効にできますか?

編集: これは、Visual Studio 2010Professionalで開発されたWindowsXP、.Net4.0で実行されています。

編集#2:完全なサンプルコードが追加されました。

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();
        }

    }
4

1 に答える 1

2

参照:MSDN:フォルダブラウザダイアログのボタンを無効にする

上記のmsdnスレッドで、ユーザーは、ディレクトリが存在しない場合に、フォルダブラウザダイアログの承認ボタンを無効にしたいと考えていました。これはあなたの質問に関連していると思います: How can I disable the OK button when the SelectedPath is not a valid folder?

そのスレッドの解決策は、ダイアログが表示された後、タイマーイベントを使用して[OK]ボタンを無効にすることです。VB.NetコードをC#に変換しました。

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Runtime.InteropServices;
public class Form1
{
    [DllImport("user32.dll", EntryPoint = "FindWindowA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]

    private static extern Int32 FindWindow(string lpClassName, string lpWindowName);
    [DllImport("user32.dll", EntryPoint = "FindWindowExA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]

    private static extern Int32 FindWindowEx(Int32 hWnd1, Int32 hWnd2, string lpsz1, string lpsz2);
    [DllImport("user32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]

    private static extern Int32 EnableWindow(Int32 hwnd, Int32 fEnable);


    private void Button1_Click(System.Object sender, System.EventArgs e)
    {
        Timer1.Enabled = true;

        FolderBrowserDialog fld = new FolderBrowserDialog();

        fld.ShowDialog(this);

    }


    private void Timer1_Tick(System.Object sender, System.EventArgs e)
    {
        Int32 hwndMainWindow = default(Int32);

        hwndMainWindow = FindWindow("#32770".Trim(), Constants.vbNullString);
        // '#32770 (Dialog)#32770 (Dialog)


        if (hwndMainWindow) {
            Int32 hwndBtn = default(Int32);

            hwndBtn = FindWindowEx(hwndMainWindow, IntPtr.Zero, "Button", "OK");


            if (hwndBtn) {
                EnableWindow(hwndBtn, 0);

            }

        }

        Timer1.Enabled = false;

    }

}
于 2012-05-28T02:32:16.343 に答える