次の方法で共有


共通のダイアログ ボックスを開く方法

この記事では、Windows Presentation Foundation (WPF) で共通のシステム ダイアログ ボックスを表示する方法について説明します。 Windows には、ファイルの選択や印刷のためのダイアログ ボックスなど、すべてのアプリケーションに共通するさまざまな種類の再利用可能なダイアログ ボックスが実装されています。

これらのダイアログ ボックスはオペレーティング システムによって提供されるため、オペレーティング システムで実行されるすべてのアプリケーション間で共有されます。 これらのダイアログ ボックスは、一貫したユーザー エクスペリエンスを提供し、 一般的なダイアログ ボックスと呼ばれます。 ユーザーは 1 つのアプリケーションで共通のダイアログ ボックスを使用するため、他のアプリケーションでそのダイアログ ボックスを使用する方法を学習する必要はありません。

メッセージ ボックスは、もう 1 つの一般的なダイアログ ボックスです。 詳細については、「 メッセージ ボックスを開く方法」を参照してください。

[ファイルを開く] ダイアログ ボックス

[ファイルを開く] ダイアログ ボックスは、開くファイルの名前を取得するファイルを開く機能によって使用されます。

WPF アプリケーションから表示されるファイルを取得する場所を示す [開く] ダイアログ ボックス。

共通の開いているファイル ダイアログ ボックスは、OpenFileDialog クラスとして実装され、Microsoft.Win32 名前空間にあります。 次のコードは、ダイアログを作成、構成、および表示する方法を示しています。

// Configure open file dialog box
var dialog = new Microsoft.Win32.OpenFileDialog();
dialog.FileName = "Document"; // Default file name
dialog.DefaultExt = ".txt"; // Default file extension
dialog.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension

// Show open file dialog box
bool? result = dialog.ShowDialog();

// Process open file dialog box results
if (result == true)
{
    // Open document
    string filename = dialog.FileName;
}
' Configure open file dialog box
Dim dialog As New Microsoft.Win32.OpenFileDialog()
dialog.FileName = "Document" ' Default file name
dialog.DefaultExt = ".txt" ' Default file extension
dialog.Filter = "Text documents (.txt)|*.txt" ' Filter files by extension

' Show open file dialog box
Dim result As Boolean? = dialog.ShowDialog()

' Process open file dialog box results
If result = True Then
    ' Open document
    Dim filename As String = dialog.FileName
End If

[ファイルを開く] ダイアログ ボックスの詳細については、「Microsoft.Win32.OpenFileDialog」を参照してください。

[ファイルの保存] ダイアログ ボックス

[ファイルの保存] ダイアログ ボックスは、保存するファイルの名前を取得するファイル保存機能によって使用されます。

WPF アプリケーションから表示されるファイルを保存する場所を示す [名前を付けて保存] ダイアログ ボックス。

共通の [ファイルの保存] ダイアログ ボックスは、SaveFileDialog クラスとして実装され、Microsoft.Win32 名前空間にあります。 次のコードは、ダイアログを作成、構成、および表示する方法を示しています。

// Configure save file dialog box
var dialog = new Microsoft.Win32.SaveFileDialog();
dialog.FileName = "Document"; // Default file name
dialog.DefaultExt = ".txt"; // Default file extension
dialog.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension

// Show save file dialog box
bool? result = dialog.ShowDialog();

// Process save file dialog box results
if (result == true)
{
    // Save document
    string filename = dialog.FileName;
}
' Configure save file dialog box
Dim dialog As New Microsoft.Win32.SaveFileDialog()
dialog.FileName = "Document" ' Default file name
dialog.DefaultExt = ".txt" ' Default file extension
dialog.Filter = "Text documents (.txt)|*.txt" ' Filter files by extension

' Show save file dialog box
Dim result As Boolean? = dialog.ShowDialog()

' Process save file dialog box results
If result = True Then
    ' Save document
    Dim filename As String = dialog.FileName
End If

[ファイルの保存] ダイアログ ボックスの詳細については、「Microsoft.Win32.SaveFileDialog」を参照してください。

[フォルダーを開く] ダイアログ ボックス

重要

[フォルダーを開く] ダイアログ ボックスは、.NET 8.0 以降で使用できます。

[フォルダーを開く] ダイアログ ボックスは、ユーザーが 1 つ以上のフォルダーを選択し、プログラムに戻すために使用します。 たとえば、ファイルの量やフォルダー内のファイル名など、フォルダーに関する情報がプログラムに表示された場合は、[フォルダーを開く] ダイアログを使用して、ユーザーがフォルダーを選択できます。

WPF アプリケーションからカメラ ロール フォルダーが選択された画像フォルダーを示す [フォルダーを開く] ダイアログ ボックス。

共通の [フォルダーを開く] ダイアログ ボックスは、 OpenFolderDialog クラスとして実装され、 Microsoft.Win32 名前空間にあります。 次のコードは、ダイアログを作成、構成、および表示する方法を示しています。

// Configure open folder dialog box
Microsoft.Win32.OpenFolderDialog dialog = new();

dialog.Multiselect = false;
dialog.Title = "Select a folder";

// Show open folder dialog box
bool? result = dialog.ShowDialog();

// Process open folder dialog box results
if (result == true)
{
    // Get the selected folder
    string fullPathToFolder = dialog.FolderName;
    string folderNameOnly = dialog.SafeFolderName;
}
' Configure open folder dialog box
Dim dialog As New Microsoft.Win32.OpenFolderDialog()

dialog.Multiselect = True
dialog.Title = "Select a folder"

' Show open folder dialog box
Dim result As Boolean? = dialog.ShowDialog()

' Process open folder dialog box results
If result = True Then

    ' Get multiple folder names
    For index = 0 To dialog.FolderNames.Length
        ' Get the selected folder
        Dim fullPathToFolder As String = dialog.FolderNames(index)
        Dim folderNameOnly As String = dialog.SafeFolderNames(index)
    Next

End If

[フォルダーを開く] ダイアログ ボックスの詳細については、「 Microsoft.Win32.OpenFolderDialog」を参照してください。

印刷ダイアログ ボックスは、ユーザーがデータを印刷するプリンターを選択して構成する印刷機能によって使用されます。

WPF アプリケーションから表示される印刷ダイアログ ボックス。

一般的な印刷ダイアログ ボックスは、PrintDialog クラスとして実装され、System.Windows.Controls 名前空間にあります。 次のコードは、作成、構成、および表示する方法を示しています。

// Configure printer dialog box
var dialog = new System.Windows.Controls.PrintDialog();
dialog.PageRangeSelection = System.Windows.Controls.PageRangeSelection.AllPages;
dialog.UserPageRangeEnabled = true;

// Show save file dialog box
bool? result = dialog.ShowDialog();

// Process save file dialog box results
if (result == true)
{
    // Document was printed
}
' Configure printer dialog box
Dim dialog As New System.Windows.Controls.PrintDialog()
dialog.PageRangeSelection = System.Windows.Controls.PageRangeSelection.AllPages
dialog.UserPageRangeEnabled = True

' Show save file dialog box
Dim result As Boolean? = dialog.ShowDialog()

' Process save file dialog box results
If result = True Then
    ' Document was printed
End If

印刷ダイアログ ボックスの詳細については、「System.Windows.Controls.PrintDialog」を参照してください。 WPF での印刷の詳細については、「印刷の 概要」を参照してください。

こちらも参照ください