RTF (Rich Text Format) ファイルまたは標準の ASCII テキスト ファイルを RichTextBox コントロールに読み込みます。
Overloads Public Sub LoadFile( _
ByVal path As String _)
[C#]
public void LoadFile(stringpath);
[C++]
public: void LoadFile(String* path);
[JScript]
public function LoadFile(
path : String);
パラメータ
- path
コントロールに読み込むファイルの名前と位置。
例外
例外の種類 | 条件 |
---|---|
IOException | コントロールへのファイルの読み込み中に発生したエラー。 |
ArgumentException | 読み込み中のファイルは RTF ドキュメントではありません。 |
解説
LoadFile メソッドを使用してファイルを読み込む場合、読み込まれているファイルの内容で、 RichTextBox コントロールの内容がすべて置換されます。この結果、 Text プロパティと Rtf プロパティの値が変更されます。このメソッドを使用して、以前に作成したテキストや RTF ドキュメントをコントロールに読み込んで処理できます。ファイルを保存する場合は、 SaveFile メソッドを使用します。
メモ このバージョンの LoadFile メソッドを使用する場合、読み込まれているファイルが RTF ドキュメントでないと例外が発生します。ASCII テキスト ファイルなど別の種類のファイルを読み込むには、 RichTextBoxStreamType 列挙体からの値をパラメータとして受け入れる、このメソッドの別バージョンを使用します。
メモ LoadFile メソッドは、 RichTextBox のハンドルが作成されるまでファイルを開きません。 LoadFile メソッドを呼び出す前に、コントロールのハンドルが作成されていることを確認してください。
使用例
[Visual Basic, C#, C++] RTF ファイルを RichTextBox コントロール内で開く例を次に示します。この例は、 OpenFileDialog クラスを使用して、ユーザーにファイルを指定するように要求するダイアログ ボックスを表示します。次に、ユーザーが指定したファイルを RTF ファイルであると見なし、そのファイルを読み込みます。ファイルが RTF ファイルでない場合は、例外がスローされます。この例は、コードが richTextBox1
という名前の RichTextBox コントロールを保持する Form クラス内にあることを前提にしています。
Public Sub LoadMyFile()
' Create an OpenFileDialog to request a file to open.
Dim openFile1 As New OpenFileDialog()
' Initialize the OpenFileDialog to look for RTF files.
openFile1.DefaultExt = "*.rtf"
openFile1.Filter = "RTF Files|*.rtf"
' Determine whether the user selected a file from the OpenFileDialog.
If (openFile1.ShowDialog() = System.Windows.Forms.DialogResult.OK) _
And (openFile1.FileName.Length > 0) Then
' Load the contents of the file into the RichTextBox.
richTextBox1.LoadFile(openFile1.FileName)
End If
End Sub
[C#]
public void LoadMyFile()
{
// Create an OpenFileDialog to request a file to open.
OpenFileDialog openFile1 = new OpenFileDialog();
// Initialize the OpenFileDialog to look for RTF files.
openFile1.DefaultExt = "*.rtf";
openFile1.Filter = "RTF Files|*.rtf";
// Determine whether the user selected a file from the OpenFileDialog.
if(openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
openFile1.FileName.Length > 0)
{
// Load the contents of the file into the RichTextBox.
richTextBox1.LoadFile(openFile1.FileName);
}
}
[C++]
public:
void LoadMyFile()
{
// Create an OpenFileDialog to request a file to open.
OpenFileDialog* openFile1 = new OpenFileDialog();
// Initialize the OpenFileDialog to look for RTF files.
openFile1->DefaultExt = S"*.rtf";
openFile1->Filter = S"RTF Files|*.rtf";
// Determine whether the user selected a file from the OpenFileDialog.
if(openFile1->ShowDialog() == System::Windows::Forms::DialogResult::OK &&
openFile1->FileName->Length > 0)
{
// Load the contents of the file into the RichTextBox.
richTextBox1->LoadFile(openFile1->FileName);
}
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ
.NET Framework セキュリティ:
- FileIOPermission (ファイルを開くために必要なアクセス許可)。 System.Security.Permissions.FileIoPermissionAccess.Read (関連する列挙体)
参照
RichTextBox クラス | RichTextBox メンバ | System.Windows.Forms 名前空間 | RichTextBox.LoadFile オーバーロードの一覧 | SaveFile