FolderBrowserDialogとは? わかりやすく解説

FolderBrowserDialog イベント


FolderBrowserDialog クラス

ユーザーにフォルダを選択するよう要求します。このクラスは継承できません。

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文構文

Public NotInheritable Class
 FolderBrowserDialog
    Inherits CommonDialog
Dim instance As FolderBrowserDialog
public sealed class FolderBrowserDialog : CommonDialog
public ref class FolderBrowserDialog sealed
 : public CommonDialog
public final class FolderBrowserDialog extends
 CommonDialog
public final class FolderBrowserDialog extends
 CommonDialog
解説解説

このクラスは、ユーザーがフォルダを参照、作成、選択できる方法を提供します。このクラスは、ファイルではなく、フォルダの選択だけをユーザーに許可する場合に使用します。フォルダの参照はツリー コントロールを通じて行います。ファイル システムにあるフォルダだけ選択できます。仮想フォルダは選択できません。

通常、新しい FolderBrowserDialog を作成したら、参照の開始位置とするディレクトリを RootFolder に設定します。または、RootFolder のサブフォルダへの絶対パスを SelectedPath に設定することによって、最初に選択されるパスを指定することもできます。Description プロパティを使用すると、追加の指示情報をユーザーに提示することもできます。最後に、ShowDialog メソッドを呼び出すと、ダイアログ ボックスが表示されます。ダイアログ ボックスが閉じられ、ShowDialog によって得られるダイアログ ボックスの結果が DialogResult.OK の場合、選択されたフォルダへのパスを表す文字列が SelectedPath に格納されます。

ShowNewFolderButton プロパティを使用すると、[新しいフォルダ] ボタンによるフォルダの新規作成をユーザーに許可するかどうかを制御できます。

FolderBrowserDialog はモーダル ダイアログ ボックスです。このため、このダイアログ ボックスが表示されているときは、ユーザーがフォルダを選択するまでアプリケーションの他の機能は動作しません。ダイアログ ボックスがモーダルとして表示されている場合、キーボードやマウス クリックによる入力は、ダイアログ ボックスのオブジェクトに対してしか発生しません。呼び出しプログラムに対する入力が発生する前に、プログラムが (通常、何らかのユーザーの操作に対する応答として) ダイアログ ボックスを非表示にするか、または終了する必要があります。

使用例使用例

RichTextBox コントロール内にユーザーがリッチ テキスト (.rtf) ファイルを開くことができるアプリケーションを作成するコード例を次に示します。

' The following example displays an application that provides the ability
 to 
' open rich text files (rtf) into the RichTextBox. The example demonstrates
 
' using the FolderBrowserDialog to set the default directory for opening
 files.
' The OpenFileDialog class is used to open the file.
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.IO

Public Class FolderBrowserDialogExampleForm
 
    Inherits Form
    
    Private folderBrowserDialog1 As FolderBrowserDialog
    Private openFileDialog1 As OpenFileDialog
 
    
    Private richTextBox1 As RichTextBox

    Private mainMenu1 As MainMenu
    Private fileMenuItem As MenuItem
    Private WithEvents folderMenuItem As
 MenuItem, _
                       closeMenuItem As MenuItem, _
                       openMenuItem As MenuItem
    
    Private openFileName As String,
 folderName As String

    Private fileOpened As Boolean
 = False

    Public Sub New()
        Me.mainMenu1 = New System.Windows.Forms.MainMenu()
 
        Me.fileMenuItem = New System.Windows.Forms.MenuItem()
 
        Me.openMenuItem = New System.Windows.Forms.MenuItem()
 
        Me.folderMenuItem = New System.Windows.Forms.MenuItem()
 
        Me.closeMenuItem = New System.Windows.Forms.MenuItem()
 

        Me.openFileDialog1 = New System.Windows.Forms.OpenFileDialog()
 
        Me.folderBrowserDialog1 = New System.Windows.Forms.FolderBrowserDialog()
 
        Me.richTextBox1 = New System.Windows.Forms.RichTextBox()
 

        Me.mainMenu1.MenuItems.Add(Me.fileMenuItem)
 
        Me.fileMenuItem.MenuItems.AddRange( _
                    New System.Windows.Forms.MenuItem() {Me.openMenuItem,
 _
                                                         Me.closeMenuItem,
 _
                                                         Me.folderMenuItem})
 
        Me.fileMenuItem.Text = "File"
 

        Me.openMenuItem.Text = "Open..."
 

        Me.folderMenuItem.Text = "Select Directory..."
 

        Me.closeMenuItem.Text = "Close"
 
        Me.closeMenuItem.Enabled = False 

        Me.openFileDialog1.DefaultExt = "rtf"
 
        Me.openFileDialog1.Filter = "rtf files
 (*.rtf)|*.rtf" 

        ' Set the Help text description for the FolderBrowserDialog.
        Me.folderBrowserDialog1.Description = _
            "Select the directory that you want to use As the
 default." 

        ' Do not allow the user to create New files via the FolderBrowserDialog.
        Me.folderBrowserDialog1.ShowNewFolderButton = False
 

        ' Default to the My Documents folder.
        Me.folderBrowserDialog1.RootFolder = Environment.SpecialFolder.Personal
 

        Me.richTextBox1.AcceptsTab = True 
        Me.richTextBox1.Location = New System.Drawing.Point(8,
 8) 
        Me.richTextBox1.Size = New System.Drawing.Size(280,
 344) 
        Me.richTextBox1.Anchor = AnchorStyles.Top Or
 AnchorStyles.Left Or _
                                 AnchorStyles.Bottom Or AnchorStyles.Right
 

        Me.ClientSize = New System.Drawing.Size(296,
 360) 
        Me.Controls.Add(Me.richTextBox1) 
        Me.Menu = Me.mainMenu1 
        Me.Text = "RTF Document Browser"
 
    End Sub
    
    <STAThread()> _
    Shared Sub Main()
        Application.Run(New FolderBrowserDialogExampleForm())
    End Sub

    ' Bring up a dialog to open a file.
    Private Sub openMenuItem_Click(sender As
 object, e As System.EventArgs) _
        Handles openMenuItem.Click
        ' If a file is not opened, then set the initial directory to
 the
        ' FolderBrowserDialog.SelectedPath value.
        If (not fileOpened) Then
            openFileDialog1.InitialDirectory = folderBrowserDialog1.SelectedPath
 
            openFileDialog1.FileName = nothing 
        End If

        ' Display the openFile dialog.
        Dim result As DialogResult = openFileDialog1.ShowDialog()
 

        ' OK button was pressed.
        If (result = DialogResult.OK) Then
            openFileName = openFileDialog1.FileName 
            Try
                ' Output the requested file in richTextBox1.
                Dim s As Stream = openFileDialog1.OpenFile()
 
                richTextBox1.LoadFile(s, RichTextBoxStreamType.RichText) 
                s.Close()     
            
                fileOpened = True 

            Catch exp As Exception
                MessageBox.Show("An error occurred while attempting
 to load the file. The error is:" _
                                + System.Environment.NewLine + exp.ToString() + System.Environment.NewLine)
 
                fileOpened = False 
            End Try
            Invalidate() 

            closeMenuItem.Enabled = fileOpened 

        ' Cancel button was pressed.
        ElseIf (result = DialogResult.Cancel) Then
            return 
        End If
    End Sub

    ' Close the current file.
    Private Sub closeMenuItem_Click(sender
 As object, e As System.EventArgs)
 _
        Handles closeMenuItem.Click
        richTextBox1.Text = "" 
        fileOpened = False 

        closeMenuItem.Enabled = False 
    End Sub

    ' Bring up a dialog to chose a folder path in which to open or save
 a file.
    Private Sub folderMenuItem_Click(sender
 As object, e As System.EventArgs)
 _
        Handles folderMenuItem.Click
        ' Show the FolderBrowserDialog.
        Dim result As DialogResult = folderBrowserDialog1.ShowDialog()
 

        If ( result = DialogResult.OK ) Then
            folderName = folderBrowserDialog1.SelectedPath 
            If (not fileOpened) Then
                ' No file is opened, bring up openFileDialog in selected
 path.
                openFileDialog1.InitialDirectory = folderName 
                openFileDialog1.FileName = nothing
                openMenuItem.PerformClick() 
            End If
        End If
    End Sub

End Class
// The following example displays an application that provides the ability
 to 
// open rich text files (rtf) into the RichTextBox. The example demonstrates
 
// using the FolderBrowserDialog to set the default directory for opening
 files.
// The OpenFileDialog class is used to open the file.
using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

public class FolderBrowserDialogExampleForm
 : System.Windows.Forms.Form
{
    private FolderBrowserDialog folderBrowserDialog1;
    private OpenFileDialog openFileDialog1;
    
    private RichTextBox richTextBox1;

    private MainMenu mainMenu1;
    private MenuItem fileMenuItem, openMenuItem;
    private MenuItem folderMenuItem, closeMenuItem;
    
    private string openFileName, folderName;

    private bool fileOpened = false;

    // The main entry point for the application.
    static void Main() 
    {
        Application.Run(new FolderBrowserDialogExampleForm());
    }


    // Constructor.
    public FolderBrowserDialogExampleForm()
    {
        this.mainMenu1 = new System.Windows.Forms.MainMenu();
        this.fileMenuItem = new System.Windows.Forms.MenuItem();
        this.openMenuItem = new System.Windows.Forms.MenuItem();
        this.folderMenuItem = new System.Windows.Forms.MenuItem();
        this.closeMenuItem = new System.Windows.Forms.MenuItem();

        this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
        this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
        this.richTextBox1 = new System.Windows.Forms.RichTextBox();

        this.mainMenu1.MenuItems.Add(this.fileMenuItem);
        this.fileMenuItem.MenuItems.AddRange(
                            new System.Windows.Forms.MenuItem[]
 {this.openMenuItem,
                                                                 this.closeMenuItem
,
                                                                 this.folderMenuItem});
        this.fileMenuItem.Text = "File";

        this.openMenuItem.Text = "Open...";
        this.openMenuItem.Click += new System.EventHandler(this.openMenuItem_Click);

        this.folderMenuItem.Text = "Select Directory...";
        this.folderMenuItem.Click += new System.EventHandler(this.folderMenuItem_Click);

        this.closeMenuItem.Text = "Close";
        this.closeMenuItem.Click += new System.EventHandler(this.closeMenuItem_Click);
        this.closeMenuItem.Enabled = false;

        this.openFileDialog1.DefaultExt = "rtf";
        this.openFileDialog1.Filter = "rtf files (*.rtf)|*.rtf";

        // Set the help text description for the FolderBrowserDialog.
        this.folderBrowserDialog1.Description = 
            "Select the directory that you want to use as the default.";

        // Do not allow the user to create new files via the FolderBrowserDialog.
        this.folderBrowserDialog1.ShowNewFolderButton = false;

        // Default to the My Documents folder.
        this.folderBrowserDialog1.RootFolder = Environment.SpecialFolder.Personal;

        this.richTextBox1.AcceptsTab = true;
        this.richTextBox1.Location = new System.Drawing.Point(8,
 8);
        this.richTextBox1.Size = new System.Drawing.Size(280,
 344);
        this.richTextBox1.Anchor = AnchorStyles.Top | AnchorStyles.Left
 | 
                                   AnchorStyles.Bottom | AnchorStyles.Right;

        this.ClientSize = new System.Drawing.Size(296,
 360);
        this.Controls.Add(this.richTextBox1);
        this.Menu = this.mainMenu1;
        this.Text = "RTF Document Browser";
    }

    // Bring up a dialog to open a file.
    private void openMenuItem_Click(object
 sender, System.EventArgs e)
    {
        // If a file is not opened, then set the initial directory to
 the
        // FolderBrowserDialog.SelectedPath value.
        if (!fileOpened) {
            openFileDialog1.InitialDirectory = folderBrowserDialog1.SelectedPath;
            openFileDialog1.FileName = null;
        }

        // Display the openFile dialog.
        DialogResult result = openFileDialog1.ShowDialog();

        // OK button was pressed.
        if(result == DialogResult.OK) 
        {
            openFileName = openFileDialog1.FileName;
            try
            {
                // Output the requested file in richTextBox1.
                Stream s = openFileDialog1.OpenFile();
                richTextBox1.LoadFile(s, RichTextBoxStreamType.RichText);
                s.Close();    
            
                fileOpened = true;

            } 
            catch(Exception exp)
            {
                MessageBox.Show("An error occurred while
 attempting to load the file. The error is:" 
                                + System.Environment.NewLine + exp.ToString() + System.Environment.NewLine);
                fileOpened = false;
            }
            Invalidate();

            closeMenuItem.Enabled = fileOpened;
        }

        // Cancel button was pressed.
        else if(result == DialogResult.Cancel)
 
        {
            return;
        }
    }


    // Close the current file.
    private void closeMenuItem_Click(object
 sender, System.EventArgs e)
    {
        richTextBox1.Text = "";
        fileOpened = false;

        closeMenuItem.Enabled = false;
    }


    // Bring up a dialog to chose a folder path in which to open or
 save a file.
    private void folderMenuItem_Click(object
 sender, System.EventArgs e)
    {
        // Show the FolderBrowserDialog.
        DialogResult result = folderBrowserDialog1.ShowDialog();
        if( result == DialogResult.OK )
        {
            folderName = folderBrowserDialog1.SelectedPath;
            if(!fileOpened)
            {
                // No file is opened, bring up openFileDialog in selected
 path.
                openFileDialog1.InitialDirectory = folderName;
                openFileDialog1.FileName = null;
                openMenuItem.PerformClick();
            } 
        }
    }
}
// The following example displays an application that provides the ability
 to
// open rich text files (rtf) into the RichTextBox. The example demonstrates
// using the FolderBrowserDialog to set the default directory for opening
 files.
// The OpenFileDialog is used to open the file.
#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
using namespace System::IO;
public ref class FolderBrowserDialogExampleForm:
 public System::Windows::Forms::Form
{
private:
   FolderBrowserDialog^ folderBrowserDialog1;
   OpenFileDialog^ openFileDialog1;
   RichTextBox^ richTextBox1;
   MainMenu^ mainMenu1;
   MenuItem^ fileMenuItem;
   MenuItem^ openMenuItem;
   MenuItem^ folderMenuItem;
   MenuItem^ closeMenuItem;
   String^ openFileName;
   String^ folderName;
   bool fileOpened;

public:

   // Constructor.
   FolderBrowserDialogExampleForm()
   {
      fileOpened = false;
      this->mainMenu1 = gcnew System::Windows::Forms::MainMenu;
      this->fileMenuItem = gcnew System::Windows::Forms::MenuItem;
      this->openMenuItem = gcnew System::Windows::Forms::MenuItem;
      this->folderMenuItem = gcnew System::Windows::Forms::MenuItem;
      this->closeMenuItem = gcnew System::Windows::Forms::MenuItem;
      this->openFileDialog1 = gcnew System::Windows::Forms::OpenFileDialog;
      this->folderBrowserDialog1 = gcnew System::Windows::Forms::FolderBrowserDialog;
      this->richTextBox1 = gcnew System::Windows::Forms::RichTextBox;
      this->mainMenu1->MenuItems->Add( this->fileMenuItem
 );
      array<System::Windows::Forms::MenuItem^>^temp0 = {this->openMenuItem
,this->closeMenuItem,this->folderMenuItem};
      this->fileMenuItem->MenuItems->AddRange( temp0
 );
      this->fileMenuItem->Text = "File";
      this->openMenuItem->Text = "Open...";
      this->openMenuItem->Click += gcnew System::EventHandler(
 this, &FolderBrowserDialogExampleForm::openMenuItem_Click
 );
      this->folderMenuItem->Text = "Select Directory...";
      this->folderMenuItem->Click += gcnew System::EventHandler(
 this, &FolderBrowserDialogExampleForm::folderMenuItem_Click
 );
      this->closeMenuItem->Text = "Close";
      this->closeMenuItem->Click += gcnew System::EventHandler(
 this, &FolderBrowserDialogExampleForm::closeMenuItem_Click
 );
      this->closeMenuItem->Enabled = false;
      this->openFileDialog1->DefaultExt = "rtf";
      this->openFileDialog1->Filter = "rtf files (*.rtf)|*.rtf";
      
      // Set the help text description for the FolderBrowserDialog.
      this->folderBrowserDialog1->Description = "Select
 the directory that you want to use as the default.";
      
      // Do not allow the user to create new files via the FolderBrowserDialog.
      this->folderBrowserDialog1->ShowNewFolderButton =
 false;
      
      // Default to the My Documents folder.
      this->folderBrowserDialog1->RootFolder = Environment::SpecialFolder::Personal;
      this->richTextBox1->AcceptsTab = true;
      this->richTextBox1->Location = System::Drawing::Point(
 8, 8 );
      this->richTextBox1->Size = System::Drawing::Size(
 280, 344 );
      this->richTextBox1->Anchor = static_cast<AnchorStyles>(AnchorStyles::Top
 | AnchorStyles::Left | AnchorStyles::Bottom | AnchorStyles::Right);
      this->ClientSize = System::Drawing::Size( 296, 360 );
      this->Controls->Add( this->richTextBox1
 );
      this->Menu = this->mainMenu1;
      this->Text = "RTF Document Browser";
   }


private:

   // Bring up a dialog to open a file.
   void openMenuItem_Click( Object^ /*sender*/, System::EventArgs^
 /*e*/ )
   {
      
      // If a file is not opened then set the initial directory to the
      // FolderBrowserDialog::SelectedPath value.
      if (  !fileOpened )
      {
         openFileDialog1->InitialDirectory = folderBrowserDialog1->SelectedPath;
         openFileDialog1->FileName = nullptr;
      }

      
      // Display the openFile Dialog.
      System::Windows::Forms::DialogResult result = openFileDialog1->ShowDialog();
      
      // OK button was pressed.
      if ( result == ::DialogResult::OK )
      {
         openFileName = openFileDialog1->FileName;
         try
         {
            
            // Output the requested file in richTextBox1.
            Stream^ s = openFileDialog1->OpenFile();
            richTextBox1->LoadFile( s, RichTextBoxStreamType::RichText );
            s->Close();
            fileOpened = true;
         }
         catch ( Exception^ exp ) 
         {
            MessageBox::Show( String::Concat( "An error occurred while
 attempting to load the file. The error is: ", System::Environment::NewLine,
 exp, System::Environment::NewLine ) );
            fileOpened = false;
         }

         Invalidate();
         closeMenuItem->Enabled = fileOpened;
      }
      // Cancel button was pressed.
      else
      
      // Cancel button was pressed.
      if ( result == ::DialogResult::Cancel )
      {
         return;
      }
   }


   // Close the current file.
   void closeMenuItem_Click( Object^ /*sender*/, System::EventArgs^
 /*e*/ )
   {
      richTextBox1->Text = "";
      fileOpened = false;
      closeMenuItem->Enabled = false;
   }


   // Bring up a dialog to chose a folder path in which to open/save
 a file.
   void folderMenuItem_Click( Object^ /*sender*/, System::EventArgs^
 /*e*/ )
   {
      
      // Show the FolderBrowserDialog.
      System::Windows::Forms::DialogResult result = folderBrowserDialog1->ShowDialog();
      if ( result == ::DialogResult::OK )
      {
         folderName = folderBrowserDialog1->SelectedPath;
         if (  !fileOpened )
         {
            
            // No file is opened, bring up openFileDialog in selected
 path.
            openFileDialog1->InitialDirectory = folderName;
            openFileDialog1->FileName = nullptr;
            openMenuItem->PerformClick();
         }
      }
   }

};


// The main entry point for the application.
int main()
{
   Application::Run( gcnew FolderBrowserDialogExampleForm );
}

// The following example displays an application that provides the ability
 to 
// open rich text files (rtf) into the RichTextBox. The example demonstrates
 
// using the FolderBrowserDialog to set the default directory for opening
 files.
// The OpenFileDialog class is used to open the file.
import System.*;
import System.Drawing.*;
import System.Windows.Forms.*;
import System.IO.*;

public class FolderBrowserDialogExampleForm
 extends System.Windows.Forms.Form
{
    private FolderBrowserDialog folderBrowserDialog1;
    private OpenFileDialog openFileDialog1;
    private RichTextBox richTextBox1;
    private MainMenu mainMenu1;
    private MenuItem fileMenuItem, openMenuItem;
    private MenuItem folderMenuItem, closeMenuItem;
    private String openFileName, folderName;
    private boolean fileOpened = false;

    // The main entry point for the application.
    public static void main(String[]
 args)
    {
        Application.Run(new FolderBrowserDialogExampleForm());
    } //main

    // Constructor.
    public FolderBrowserDialogExampleForm()
    {
        this.mainMenu1 = new System.Windows.Forms.MainMenu();
        this.fileMenuItem = new System.Windows.Forms.MenuItem();
        this.openMenuItem = new System.Windows.Forms.MenuItem();
        this.folderMenuItem = new System.Windows.Forms.MenuItem();
        this.closeMenuItem = new System.Windows.Forms.MenuItem();

        this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
        this.folderBrowserDialog1 = 
            new System.Windows.Forms.FolderBrowserDialog();
        this.richTextBox1 = new System.Windows.Forms.RichTextBox();

        this.mainMenu1.get_MenuItems().Add(this.fileMenuItem);
        this.fileMenuItem.get_MenuItems().AddRange(
            new System.Windows.Forms.MenuItem[] { this.openMenuItem,
 
            this.closeMenuItem, this.folderMenuItem
 });
        this.fileMenuItem.set_Text("File");

        this.openMenuItem.set_Text("Open...");
        this.openMenuItem.add_Click(new System.EventHandler(
            this.openMenuItem_Click));

        this.folderMenuItem.set_Text("Select Directory...");
        this.folderMenuItem.add_Click(new System.EventHandler(
            this.folderMenuItem_Click));

        this.closeMenuItem.set_Text("Close");
        this.closeMenuItem.add_Click(new System.EventHandler(
            this.closeMenuItem_Click));
        this.closeMenuItem.set_Enabled(false);

        this.openFileDialog1.set_DefaultExt("rtf");
        this.openFileDialog1.set_Filter("rtf files (*.rtf)|*.rtf");
        // Set the help text description for the FolderBrowserDialog.
        this.folderBrowserDialog1.set_Description("Select
 the directory " 
            + "that you want to use as the default.");
        // Do not allow the user to create new files via the 
        // FolderBrowserDialog.
        this.folderBrowserDialog1.set_ShowNewFolderButton(false);
        // Default to the My Documents folder.
        this.folderBrowserDialog1.set_RootFolder(
            Environment.SpecialFolder.Personal);

        this.richTextBox1.set_AcceptsTab(true);
        this.richTextBox1.set_Location(new
 System.Drawing.Point(8, 8));
        this.richTextBox1.set_Size(new System.Drawing.Size(280,
 344));
        this.richTextBox1.set_Anchor(AnchorStyles.Top | AnchorStyles.Left
 
            | AnchorStyles.Bottom | AnchorStyles.Right);

        this.set_ClientSize(new System.Drawing.Size(296,
 360));
        this.get_Controls().Add(this.richTextBox1);
        this.set_Menu(this.mainMenu1);
        this.set_Text("RTF Document Browser");
    } //FolderBrowserDialogExampleForm

    // Bring up a dialog to open a file.
    private void openMenuItem_Click(Object
 sender, System.EventArgs e)
    {
        // If a file is not opened, then set the initial directory to
 the
        // FolderBrowserDialog.SelectedPath value.
        if (!(fileOpened)) {
            openFileDialog1.set_InitialDirectory(
                folderBrowserDialog1.get_SelectedPath());
            openFileDialog1.set_FileName(null);
        }
        // Display the openFile dialog.
        DialogResult result = openFileDialog1.ShowDialog();
        // OK button was pressed.
        if (result.Equals(get_DialogResult().OK)) {
            openFileName = openFileDialog1.get_FileName();
            try {
                // Output the requested file in richTextBox1.
                Stream s = openFileDialog1.OpenFile();
                richTextBox1.LoadFile(s, RichTextBoxStreamType.RichText);
                s.Close();
                fileOpened = true;
            }
            catch (System.Exception exp) {
                MessageBox.Show("An error occurred while
 attempting to " 
                    + "load the file. The error is:" 
                    + System.Environment.get_NewLine() 
                    + exp.ToString() + System.Environment.get_NewLine());
                fileOpened = false;
            }
            Invalidate();
            closeMenuItem.set_Enabled(fileOpened);
        }
        // Cancel button was pressed.
        else {
            if (result.Equals(get_DialogResult().Cancel)) {
                return;
            }
        }
    } //openMenuItem_Click

    // Close the current file.
    private void closeMenuItem_Click(Object
 sender, System.EventArgs e)
    {
        richTextBox1.set_Text("");
        fileOpened = false;
        closeMenuItem.set_Enabled(false);
    } //closeMenuItem_Click

    // Bring up a dialog to chose a folder path in which to open or
 save a file.
    private void folderMenuItem_Click(Object
 sender, System.EventArgs e)
    {
        // Show the FolderBrowserDialog.
        DialogResult result = folderBrowserDialog1.ShowDialog();
        if (result.Equals(get_DialogResult().OK)) {
            folderName = folderBrowserDialog1.get_SelectedPath();
            if (!(fileOpened)) {
                // No file is opened, bring up openFileDialog in selected
 path.
                openFileDialog1.set_InitialDirectory(folderName);
                openFileDialog1.set_FileName(null);
                openMenuItem.PerformClick();
            }
        }
    } //folderMenuItem_Click
} //FolderBrowserDialogExampleForm
継承階層継承階層
System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
       System.Windows.Forms.CommonDialog
        System.Windows.Forms.FolderBrowserDialog
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
FolderBrowserDialog メンバ
System.Windows.Forms 名前空間
RootFolder
SelectedPath
ShowDialog

FolderBrowserDialog コンストラクタ

FolderBrowserDialog クラスの新しいインスタンスを初期化します。

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文構文

Dim instance As New FolderBrowserDialog
public FolderBrowserDialog ()
public:
FolderBrowserDialog ()
public FolderBrowserDialog ()
public function FolderBrowserDialog ()
解説解説

各種プロパティを既定値に設定して初期化する既定のコンストラクタです。

新しい FolderBrowserDialog が作成されると、RootFolder プロパティは Environment.SpecialFolder.Desktop に設定され、Description プロパティは空文字列 ("") に設定され、SelectedPath プロパティは空文字列に設定され、ShowNewFolderButton プロパティは true に設定されます。

通常、新しい FolderBrowserDialog を作成したら、参照の開始位置とするディレクトリを RootFolder に設定します。または、RootFolder のサブフォルダへのパスを SelectedPath に設定することによって、最初に選択されるパスを指定することもできます。Description プロパティを使用すると、追加の指示情報をユーザーに提示することもできます。最後に、ShowDialog メソッドを呼び出すと、ダイアログ ボックスが表示されます。ダイアログ ボックスが閉じられ、ShowDialog の結果が DialogResult.OK の場合、選択されたフォルダへのパスを表す文字列が SelectedPath に格納されます。

FolderBrowserDialog はモーダル ダイアログ ボックスです。このため、このダイアログ ボックスが表示されているときは、ユーザーがフォルダを選択するまでアプリケーションの他の機能は動作しません。ダイアログ ボックスがモーダルとして表示されている場合、キーボードやマウス クリックによる入力は、ダイアログ ボックスのオブジェクトに対してしか発生しません。呼び出しプログラムに対する入力が発生する前に、プログラムが (通常、何らかのユーザーの操作に対する応答として) ダイアログ ボックスを非表示にするか、または終了する必要があります。

使用例使用例
' The following example displays an application that provides the ability
 to 
' open rich text files (rtf) into the RichTextBox. The example demonstrates
 
' using the FolderBrowserDialog to set the default directory for opening
 files.
' The OpenFileDialog class is used to open the file.
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.IO

Public Class FolderBrowserDialogExampleForm
 
    Inherits Form
    
    Private folderBrowserDialog1 As FolderBrowserDialog
    Private openFileDialog1 As OpenFileDialog
 
    
    Private richTextBox1 As RichTextBox

    Private mainMenu1 As MainMenu
    Private fileMenuItem As MenuItem
    Private WithEvents folderMenuItem As
 MenuItem, _
                       closeMenuItem As MenuItem, _
                       openMenuItem As MenuItem
    
    Private openFileName As String,
 folderName As String

    Private fileOpened As Boolean
 = False

    Public Sub New()
        Me.mainMenu1 = New System.Windows.Forms.MainMenu()
 
        Me.fileMenuItem = New System.Windows.Forms.MenuItem()
 
        Me.openMenuItem = New System.Windows.Forms.MenuItem()
 
        Me.folderMenuItem = New System.Windows.Forms.MenuItem()
 
        Me.closeMenuItem = New System.Windows.Forms.MenuItem()
 

        Me.openFileDialog1 = New System.Windows.Forms.OpenFileDialog()
 
        Me.folderBrowserDialog1 = New System.Windows.Forms.FolderBrowserDialog()
 
        Me.richTextBox1 = New System.Windows.Forms.RichTextBox()
 

        Me.mainMenu1.MenuItems.Add(Me.fileMenuItem)
 
        Me.fileMenuItem.MenuItems.AddRange( _
                    New System.Windows.Forms.MenuItem() {Me.openMenuItem,
 _
                                                         Me.closeMenuItem,
 _
                                                         Me.folderMenuItem})
 
        Me.fileMenuItem.Text = "File"
 

        Me.openMenuItem.Text = "Open..."
 

        Me.folderMenuItem.Text = "Select Directory..."
 

        Me.closeMenuItem.Text = "Close"
 
        Me.closeMenuItem.Enabled = False 

        Me.openFileDialog1.DefaultExt = "rtf"
 
        Me.openFileDialog1.Filter = "rtf files
 (*.rtf)|*.rtf" 

        ' Set the Help text description for the FolderBrowserDialog.
        Me.folderBrowserDialog1.Description = _
            "Select the directory that you want to use As the
 default." 

        ' Do not allow the user to create New files via the FolderBrowserDialog.
        Me.folderBrowserDialog1.ShowNewFolderButton = False
 

        ' Default to the My Documents folder.
        Me.folderBrowserDialog1.RootFolder = Environment.SpecialFolder.Personal
 

        Me.richTextBox1.AcceptsTab = True 
        Me.richTextBox1.Location = New System.Drawing.Point(8,
 8) 
        Me.richTextBox1.Size = New System.Drawing.Size(280,
 344) 
        Me.richTextBox1.Anchor = AnchorStyles.Top Or
 AnchorStyles.Left Or _
                                 AnchorStyles.Bottom Or AnchorStyles.Right
 

        Me.ClientSize = New System.Drawing.Size(296,
 360) 
        Me.Controls.Add(Me.richTextBox1) 
        Me.Menu = Me.mainMenu1 
        Me.Text = "RTF Document Browser"
 
    End Sub
    
    <STAThread()> _
    Shared Sub Main()
        Application.Run(New FolderBrowserDialogExampleForm())
    End Sub

    ' Bring up a dialog to open a file.
    Private Sub openMenuItem_Click(sender As
 object, e As System.EventArgs) _
        Handles openMenuItem.Click
        ' If a file is not opened, then set the initial directory to
 the
        ' FolderBrowserDialog.SelectedPath value.
        If (not fileOpened) Then
            openFileDialog1.InitialDirectory = folderBrowserDialog1.SelectedPath
 
            openFileDialog1.FileName = nothing 
        End If

        ' Display the openFile dialog.
        Dim result As DialogResult = openFileDialog1.ShowDialog()
 

        ' OK button was pressed.
        If (result = DialogResult.OK) Then
            openFileName = openFileDialog1.FileName 
            Try
                ' Output the requested file in richTextBox1.
                Dim s As Stream = openFileDialog1.OpenFile()
 
                richTextBox1.LoadFile(s, RichTextBoxStreamType.RichText) 
                s.Close()     
            
                fileOpened = True 

            Catch exp As Exception
                MessageBox.Show("An error occurred while attempting
 to load the file. The error is:" _
                                + System.Environment.NewLine + exp.ToString() + System.Environment.NewLine)
 
                fileOpened = False 
            End Try
            Invalidate() 

            closeMenuItem.Enabled = fileOpened 

        ' Cancel button was pressed.
        ElseIf (result = DialogResult.Cancel) Then
            return 
        End If
    End Sub

    ' Close the current file.
    Private Sub closeMenuItem_Click(sender
 As object, e As System.EventArgs)
 _
        Handles closeMenuItem.Click
        richTextBox1.Text = "" 
        fileOpened = False 

        closeMenuItem.Enabled = False 
    End Sub

    ' Bring up a dialog to chose a folder path in which to open or save
 a file.
    Private Sub folderMenuItem_Click(sender
 As object, e As System.EventArgs)
 _
        Handles folderMenuItem.Click
        ' Show the FolderBrowserDialog.
        Dim result As DialogResult = folderBrowserDialog1.ShowDialog()
 

        If ( result = DialogResult.OK ) Then
            folderName = folderBrowserDialog1.SelectedPath 
            If (not fileOpened) Then
                ' No file is opened, bring up openFileDialog in selected
 path.
                openFileDialog1.InitialDirectory = folderName 
                openFileDialog1.FileName = nothing
                openMenuItem.PerformClick() 
            End If
        End If
    End Sub

End Class
// The following example displays an application that provides the ability
 to 
// open rich text files (rtf) into the RichTextBox. The example demonstrates
 
// using the FolderBrowserDialog to set the default directory for opening
 files.
// The OpenFileDialog class is used to open the file.
using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

public class FolderBrowserDialogExampleForm
 : System.Windows.Forms.Form
{
    private FolderBrowserDialog folderBrowserDialog1;
    private OpenFileDialog openFileDialog1;
    
    private RichTextBox richTextBox1;

    private MainMenu mainMenu1;
    private MenuItem fileMenuItem, openMenuItem;
    private MenuItem folderMenuItem, closeMenuItem;
    
    private string openFileName, folderName;

    private bool fileOpened = false;

    // The main entry point for the application.
    static void Main() 
    {
        Application.Run(new FolderBrowserDialogExampleForm());
    }


    // Constructor.
    public FolderBrowserDialogExampleForm()
    {
        this.mainMenu1 = new System.Windows.Forms.MainMenu();
        this.fileMenuItem = new System.Windows.Forms.MenuItem();
        this.openMenuItem = new System.Windows.Forms.MenuItem();
        this.folderMenuItem = new System.Windows.Forms.MenuItem();
        this.closeMenuItem = new System.Windows.Forms.MenuItem();

        this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
        this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
        this.richTextBox1 = new System.Windows.Forms.RichTextBox();

        this.mainMenu1.MenuItems.Add(this.fileMenuItem);
        this.fileMenuItem.MenuItems.AddRange(
                            new System.Windows.Forms.MenuItem[]
 {this.openMenuItem,
                                                                 this.closeMenuItem
,
                                                                 this.folderMenuItem});
        this.fileMenuItem.Text = "File";

        this.openMenuItem.Text = "Open...";
        this.openMenuItem.Click += new System.EventHandler(this.openMenuItem_Click);

        this.folderMenuItem.Text = "Select Directory...";
        this.folderMenuItem.Click += new System.EventHandler(this.folderMenuItem_Click);

        this.closeMenuItem.Text = "Close";
        this.closeMenuItem.Click += new System.EventHandler(this.closeMenuItem_Click);
        this.closeMenuItem.Enabled = false;

        this.openFileDialog1.DefaultExt = "rtf";
        this.openFileDialog1.Filter = "rtf files (*.rtf)|*.rtf";

        // Set the help text description for the FolderBrowserDialog.
        this.folderBrowserDialog1.Description = 
            "Select the directory that you want to use as the default.";

        // Do not allow the user to create new files via the FolderBrowserDialog.
        this.folderBrowserDialog1.ShowNewFolderButton = false;

        // Default to the My Documents folder.
        this.folderBrowserDialog1.RootFolder = Environment.SpecialFolder.Personal;

        this.richTextBox1.AcceptsTab = true;
        this.richTextBox1.Location = new System.Drawing.Point(8,
 8);
        this.richTextBox1.Size = new System.Drawing.Size(280,
 344);
        this.richTextBox1.Anchor = AnchorStyles.Top | AnchorStyles.Left
 | 
                                   AnchorStyles.Bottom | AnchorStyles.Right;

        this.ClientSize = new System.Drawing.Size(296,
 360);
        this.Controls.Add(this.richTextBox1);
        this.Menu = this.mainMenu1;
        this.Text = "RTF Document Browser";
    }

    // Bring up a dialog to open a file.
    private void openMenuItem_Click(object
 sender, System.EventArgs e)
    {
        // If a file is not opened, then set the initial directory to
 the
        // FolderBrowserDialog.SelectedPath value.
        if (!fileOpened) {
            openFileDialog1.InitialDirectory = folderBrowserDialog1.SelectedPath;
            openFileDialog1.FileName = null;
        }

        // Display the openFile dialog.
        DialogResult result = openFileDialog1.ShowDialog();

        // OK button was pressed.
        if(result == DialogResult.OK) 
        {
            openFileName = openFileDialog1.FileName;
            try
            {
                // Output the requested file in richTextBox1.
                Stream s = openFileDialog1.OpenFile();
                richTextBox1.LoadFile(s, RichTextBoxStreamType.RichText);
                s.Close();    
            
                fileOpened = true;

            } 
            catch(Exception exp)
            {
                MessageBox.Show("An error occurred while
 attempting to load the file. The error is:" 
                                + System.Environment.NewLine + exp.ToString() + System.Environment.NewLine);
                fileOpened = false;
            }
            Invalidate();

            closeMenuItem.Enabled = fileOpened;
        }

        // Cancel button was pressed.
        else if(result == DialogResult.Cancel)
 
        {
            return;
        }
    }


    // Close the current file.
    private void closeMenuItem_Click(object
 sender, System.EventArgs e)
    {
        richTextBox1.Text = "";
        fileOpened = false;

        closeMenuItem.Enabled = false;
    }


    // Bring up a dialog to chose a folder path in which to open or
 save a file.
    private void folderMenuItem_Click(object
 sender, System.EventArgs e)
    {
        // Show the FolderBrowserDialog.
        DialogResult result = folderBrowserDialog1.ShowDialog();
        if( result == DialogResult.OK )
        {
            folderName = folderBrowserDialog1.SelectedPath;
            if(!fileOpened)
            {
                // No file is opened, bring up openFileDialog in selected
 path.
                openFileDialog1.InitialDirectory = folderName;
                openFileDialog1.FileName = null;
                openMenuItem.PerformClick();
            } 
        }
    }
}
// The following example displays an application that provides the ability
 to
// open rich text files (rtf) into the RichTextBox. The example demonstrates
// using the FolderBrowserDialog to set the default directory for opening
 files.
// The OpenFileDialog is used to open the file.
#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
using namespace System::IO;
public ref class FolderBrowserDialogExampleForm:
 public System::Windows::Forms::Form
{
private:
   FolderBrowserDialog^ folderBrowserDialog1;
   OpenFileDialog^ openFileDialog1;
   RichTextBox^ richTextBox1;
   MainMenu^ mainMenu1;
   MenuItem^ fileMenuItem;
   MenuItem^ openMenuItem;
   MenuItem^ folderMenuItem;
   MenuItem^ closeMenuItem;
   String^ openFileName;
   String^ folderName;
   bool fileOpened;

public:

   // Constructor.
   FolderBrowserDialogExampleForm()
   {
      fileOpened = false;
      this->mainMenu1 = gcnew System::Windows::Forms::MainMenu;
      this->fileMenuItem = gcnew System::Windows::Forms::MenuItem;
      this->openMenuItem = gcnew System::Windows::Forms::MenuItem;
      this->folderMenuItem = gcnew System::Windows::Forms::MenuItem;
      this->closeMenuItem = gcnew System::Windows::Forms::MenuItem;
      this->openFileDialog1 = gcnew System::Windows::Forms::OpenFileDialog;
      this->folderBrowserDialog1 = gcnew System::Windows::Forms::FolderBrowserDialog;
      this->richTextBox1 = gcnew System::Windows::Forms::RichTextBox;
      this->mainMenu1->MenuItems->Add( this->fileMenuItem
 );
      array<System::Windows::Forms::MenuItem^>^temp0 = {this->openMenuItem
,this->closeMenuItem,this->folderMenuItem};
      this->fileMenuItem->MenuItems->AddRange( temp0
 );
      this->fileMenuItem->Text = "File";
      this->openMenuItem->Text = "Open...";
      this->openMenuItem->Click += gcnew System::EventHandler(
 this, &FolderBrowserDialogExampleForm::openMenuItem_Click
 );
      this->folderMenuItem->Text = "Select Directory...";
      this->folderMenuItem->Click += gcnew System::EventHandler(
 this, &FolderBrowserDialogExampleForm::folderMenuItem_Click
 );
      this->closeMenuItem->Text = "Close";
      this->closeMenuItem->Click += gcnew System::EventHandler(
 this, &FolderBrowserDialogExampleForm::closeMenuItem_Click
 );
      this->closeMenuItem->Enabled = false;
      this->openFileDialog1->DefaultExt = "rtf";
      this->openFileDialog1->Filter = "rtf files (*.rtf)|*.rtf";
      
      // Set the help text description for the FolderBrowserDialog.
      this->folderBrowserDialog1->Description = "Select
 the directory that you want to use as the default.";
      
      // Do not allow the user to create new files via the FolderBrowserDialog.
      this->folderBrowserDialog1->ShowNewFolderButton =
 false;
      
      // Default to the My Documents folder.
      this->folderBrowserDialog1->RootFolder = Environment::SpecialFolder::Personal;
      this->richTextBox1->AcceptsTab = true;
      this->richTextBox1->Location = System::Drawing::Point(
 8, 8 );
      this->richTextBox1->Size = System::Drawing::Size(
 280, 344 );
      this->richTextBox1->Anchor = static_cast<AnchorStyles>(AnchorStyles::Top
 | AnchorStyles::Left | AnchorStyles::Bottom | AnchorStyles::Right);
      this->ClientSize = System::Drawing::Size( 296, 360 );
      this->Controls->Add( this->richTextBox1
 );
      this->Menu = this->mainMenu1;
      this->Text = "RTF Document Browser";
   }


private:

   // Bring up a dialog to open a file.
   void openMenuItem_Click( Object^ /*sender*/, System::EventArgs^
 /*e*/ )
   {
      
      // If a file is not opened then set the initial directory to the
      // FolderBrowserDialog::SelectedPath value.
      if (  !fileOpened )
      {
         openFileDialog1->InitialDirectory = folderBrowserDialog1->SelectedPath;
         openFileDialog1->FileName = nullptr;
      }

      
      // Display the openFile Dialog.
      System::Windows::Forms::DialogResult result = openFileDialog1->ShowDialog();
      
      // OK button was pressed.
      if ( result == ::DialogResult::OK )
      {
         openFileName = openFileDialog1->FileName;
         try
         {
            
            // Output the requested file in richTextBox1.
            Stream^ s = openFileDialog1->OpenFile();
            richTextBox1->LoadFile( s, RichTextBoxStreamType::RichText );
            s->Close();
            fileOpened = true;
         }
         catch ( Exception^ exp ) 
         {
            MessageBox::Show( String::Concat( "An error occurred while
 attempting to load the file. The error is: ", System::Environment::NewLine,
 exp, System::Environment::NewLine ) );
            fileOpened = false;
         }

         Invalidate();
         closeMenuItem->Enabled = fileOpened;
      }
      // Cancel button was pressed.
      else
      
      // Cancel button was pressed.
      if ( result == ::DialogResult::Cancel )
      {
         return;
      }
   }


   // Close the current file.
   void closeMenuItem_Click( Object^ /*sender*/, System::EventArgs^
 /*e*/ )
   {
      richTextBox1->Text = "";
      fileOpened = false;
      closeMenuItem->Enabled = false;
   }


   // Bring up a dialog to chose a folder path in which to open/save
 a file.
   void folderMenuItem_Click( Object^ /*sender*/, System::EventArgs^
 /*e*/ )
   {
      
      // Show the FolderBrowserDialog.
      System::Windows::Forms::DialogResult result = folderBrowserDialog1->ShowDialog();
      if ( result == ::DialogResult::OK )
      {
         folderName = folderBrowserDialog1->SelectedPath;
         if (  !fileOpened )
         {
            
            // No file is opened, bring up openFileDialog in selected
 path.
            openFileDialog1->InitialDirectory = folderName;
            openFileDialog1->FileName = nullptr;
            openMenuItem->PerformClick();
         }
      }
   }

};


// The main entry point for the application.
int main()
{
   Application::Run( gcnew FolderBrowserDialogExampleForm );
}

// The following example displays an application that provides the ability
 to 
// open rich text files (rtf) into the RichTextBox. The example demonstrates
 
// using the FolderBrowserDialog to set the default directory for opening
 files.
// The OpenFileDialog class is used to open the file.
import System.*;
import System.Drawing.*;
import System.Windows.Forms.*;
import System.IO.*;

public class FolderBrowserDialogExampleForm
 extends System.Windows.Forms.Form
{
    private FolderBrowserDialog folderBrowserDialog1;
    private OpenFileDialog openFileDialog1;
    private RichTextBox richTextBox1;
    private MainMenu mainMenu1;
    private MenuItem fileMenuItem, openMenuItem;
    private MenuItem folderMenuItem, closeMenuItem;
    private String openFileName, folderName;
    private boolean fileOpened = false;

    // The main entry point for the application.
    public static void main(String[]
 args)
    {
        Application.Run(new FolderBrowserDialogExampleForm());
    } //main

    // Constructor.
    public FolderBrowserDialogExampleForm()
    {
        this.mainMenu1 = new System.Windows.Forms.MainMenu();
        this.fileMenuItem = new System.Windows.Forms.MenuItem();
        this.openMenuItem = new System.Windows.Forms.MenuItem();
        this.folderMenuItem = new System.Windows.Forms.MenuItem();
        this.closeMenuItem = new System.Windows.Forms.MenuItem();

        this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
        this.folderBrowserDialog1 = 
            new System.Windows.Forms.FolderBrowserDialog();
        this.richTextBox1 = new System.Windows.Forms.RichTextBox();

        this.mainMenu1.get_MenuItems().Add(this.fileMenuItem);
        this.fileMenuItem.get_MenuItems().AddRange(
            new System.Windows.Forms.MenuItem[] { this.openMenuItem,
 
            this.closeMenuItem, this.folderMenuItem
 });
        this.fileMenuItem.set_Text("File");

        this.openMenuItem.set_Text("Open...");
        this.openMenuItem.add_Click(new System.EventHandler(
            this.openMenuItem_Click));

        this.folderMenuItem.set_Text("Select Directory...");
        this.folderMenuItem.add_Click(new System.EventHandler(
            this.folderMenuItem_Click));

        this.closeMenuItem.set_Text("Close");
        this.closeMenuItem.add_Click(new System.EventHandler(
            this.closeMenuItem_Click));
        this.closeMenuItem.set_Enabled(false);

        this.openFileDialog1.set_DefaultExt("rtf");
        this.openFileDialog1.set_Filter("rtf files (*.rtf)|*.rtf");
        // Set the help text description for the FolderBrowserDialog.
        this.folderBrowserDialog1.set_Description("Select
 the directory " 
            + "that you want to use as the default.");
        // Do not allow the user to create new files via the 
        // FolderBrowserDialog.
        this.folderBrowserDialog1.set_ShowNewFolderButton(false);
        // Default to the My Documents folder.
        this.folderBrowserDialog1.set_RootFolder(
            Environment.SpecialFolder.Personal);

        this.richTextBox1.set_AcceptsTab(true);
        this.richTextBox1.set_Location(new
 System.Drawing.Point(8, 8));
        this.richTextBox1.set_Size(new System.Drawing.Size(280,
 344));
        this.richTextBox1.set_Anchor(AnchorStyles.Top | AnchorStyles.Left
 
            | AnchorStyles.Bottom | AnchorStyles.Right);

        this.set_ClientSize(new System.Drawing.Size(296,
 360));
        this.get_Controls().Add(this.richTextBox1);
        this.set_Menu(this.mainMenu1);
        this.set_Text("RTF Document Browser");
    } //FolderBrowserDialogExampleForm

    // Bring up a dialog to open a file.
    private void openMenuItem_Click(Object
 sender, System.EventArgs e)
    {
        // If a file is not opened, then set the initial directory to
 the
        // FolderBrowserDialog.SelectedPath value.
        if (!(fileOpened)) {
            openFileDialog1.set_InitialDirectory(
                folderBrowserDialog1.get_SelectedPath());
            openFileDialog1.set_FileName(null);
        }
        // Display the openFile dialog.
        DialogResult result = openFileDialog1.ShowDialog();
        // OK button was pressed.
        if (result.Equals(get_DialogResult().OK)) {
            openFileName = openFileDialog1.get_FileName();
            try {
                // Output the requested file in richTextBox1.
                Stream s = openFileDialog1.OpenFile();
                richTextBox1.LoadFile(s, RichTextBoxStreamType.RichText);
                s.Close();
                fileOpened = true;
            }
            catch (System.Exception exp) {
                MessageBox.Show("An error occurred while
 attempting to " 
                    + "load the file. The error is:" 
                    + System.Environment.get_NewLine() 
                    + exp.ToString() + System.Environment.get_NewLine());
                fileOpened = false;
            }
            Invalidate();
            closeMenuItem.set_Enabled(fileOpened);
        }
        // Cancel button was pressed.
        else {
            if (result.Equals(get_DialogResult().Cancel)) {
                return;
            }
        }
    } //openMenuItem_Click

    // Close the current file.
    private void closeMenuItem_Click(Object
 sender, System.EventArgs e)
    {
        richTextBox1.set_Text("");
        fileOpened = false;
        closeMenuItem.set_Enabled(false);
    } //closeMenuItem_Click

    // Bring up a dialog to chose a folder path in which to open or
 save a file.
    private void folderMenuItem_Click(Object
 sender, System.EventArgs e)
    {
        // Show the FolderBrowserDialog.
        DialogResult result = folderBrowserDialog1.ShowDialog();
        if (result.Equals(get_DialogResult().OK)) {
            folderName = folderBrowserDialog1.get_SelectedPath();
            if (!(fileOpened)) {
                // No file is opened, bring up openFileDialog in selected
 path.
                openFileDialog1.set_InitialDirectory(folderName);
                openFileDialog1.set_FileName(null);
                openMenuItem.PerformClick();
            }
        }
    } //folderMenuItem_Click
} //FolderBrowserDialogExampleForm
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
FolderBrowserDialog クラス
FolderBrowserDialog メンバ
System.Windows.Forms 名前空間

FolderBrowserDialog プロパティ


パブリック プロパティパブリック プロパティ

プロテクト プロパティプロテクト プロパティ
参照参照

関連項目

FolderBrowserDialog クラス
System.Windows.Forms 名前空間
RootFolder
SelectedPath
ShowDialog

FolderBrowserDialog メソッド


パブリック メソッドパブリック メソッド

( プロテクト メソッド も参照)
  名前 説明
パブリック メソッド CreateObjRef  リモート オブジェクトとの通信に使用するプロキシの生成に必要な情報をすべて格納しているオブジェクトを作成します。 ( MarshalByRefObject から継承されます。)
パブリック メソッド Dispose  オーバーロードされます。 Component によって使用されているリソースを解放します。 ( Component から継承されます。)
パブリック メソッド Equals  オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。)
パブリック メソッド GetHashCode  特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。)
パブリック メソッド GetLifetimeService  対象のインスタンスの有効期間ポリシーを制御する、現在の有効期間サービス オブジェクトを取得します。 ( MarshalByRefObject から継承されます。)
パブリック メソッド GetType  現在のインスタンスの Type を取得します。 ( Object から継承されます。)
パブリック メソッド InitializeLifetimeService  対象のインスタンスの有効期間ポリシーを制御する、有効期間サービス オブジェクトを取得します。 ( MarshalByRefObject から継承されます。)
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。)
パブリック メソッド Reset オーバーライドされます。 プロパティを既定値にリセットします。
パブリック メソッド ShowDialog  オーバーロードされます。 コモン ダイアログ ボックスを実行します。 ( CommonDialog から継承されます。)
パブリック メソッド ToString  Component の名前を格納している String を返します (存在する場合)。このメソッドはオーバーライドできません。 ( Component から継承されます。)
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

FolderBrowserDialog クラス
System.Windows.Forms 名前空間
RootFolder
SelectedPath
ShowDialog

FolderBrowserDialog メンバ

ユーザーにフォルダを選択するよう要求します。このクラスは継承できません。

FolderBrowserDialog データ型で公開されるメンバを以下の表に示します。


パブリック コンストラクタパブリック コンストラクタ
  名前 説明
パブリック メソッド FolderBrowserDialog FolderBrowserDialog クラスの新しいインスタンスを初期化します。
パブリック プロパティパブリック プロパティ
プロテクト プロパティプロテクト プロパティ
パブリック メソッドパブリック メソッド
( プロテクト メソッド も参照)
  名前 説明
パブリック メソッド CreateObjRef  リモート オブジェクトとの通信に使用するプロキシの生成に必要な情報をすべて格納しているオブジェクトを作成します。 (MarshalByRefObject から継承されます。)
パブリック メソッド Dispose  オーバーロードされます。 Component によって使用されているリソースを解放します。 (Component から継承されます。)
パブリック メソッド Equals  オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 (Object から継承されます。)
パブリック メソッド GetHashCode  特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 (Object から継承されます。)
パブリック メソッド GetLifetimeService  対象のインスタンスの有効期間ポリシーを制御する、現在の有効期間サービス オブジェクトを取得します。 (MarshalByRefObject から継承されます。)
パブリック メソッド GetType  現在のインスタンスの Type を取得します。 (Object から継承されます。)
パブリック メソッド InitializeLifetimeService  対象のインスタンスの有効期間ポリシーを制御する、有効期間サービス オブジェクトを取得します。 (MarshalByRefObject から継承されます。)
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。)
パブリック メソッド Reset オーバーライドされます。 プロパティを既定値にリセットします。
パブリック メソッド ShowDialog  オーバーロードされます。 コモン ダイアログ ボックスを実行します。 (CommonDialog から継承されます。)
パブリック メソッド ToString  Component の名前を格納している String を返します (存在する場合)。このメソッドはオーバーライドできません。 (Component から継承されます。)
プロテクト メソッドプロテクト メソッド
パブリック イベントパブリック イベント
参照参照

関連項目

FolderBrowserDialog クラス
System.Windows.Forms 名前空間
RootFolder
SelectedPath
ShowDialog



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「FolderBrowserDialog」の関連用語

FolderBrowserDialogのお隣キーワード

   

英語⇒日本語
日本語⇒英語
   



FolderBrowserDialogのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS