3

2 つのボタンがあり、これらのボタンをクリックすると別のファイルが読み込まれます。MsgBox ファイルが大きいのでを使って readfile を表示したので、 で表示したいですrichTextBox

これらのボタンのいずれかをクリックしたときに、どうすれば を開いてrichTextBox表示できますか?read file

private void button1_Click(object sender, EventArgs e)
{  
    DisplayFile(FileSelected);//DisplayFile is the path of the file
    var ReadFile = XDocument.Load(FileSelected); //Read the selected file to display

    MessageBox.Show("The Selected" + " " + FileSelected + " " + "File Contains :" + "\n " + "\n " + ReadFile);
    button1.Enabled = false;
}

private void button2_Click(object sender, EventArgs e)
{
    FileInfo file = (FileInfo)comboBox2.SelectedItem;
    StreamReader FileRead = new StreamReader(file.FullName);
    string FileBuffer = FileRead.ReadToEnd(); //Read the selected file to display

    //MessageBox.Show("The Selected" + " " + file + " " +"File Contains :"  + "\n " + "\n " + FileBuffer);
    // richTextBox1.AppendText("The Selected" + " " + file + " " + "File Contains :" + "\n " + "\n " + FileBuffer);
    //richTextBox1.Text = FileBuffer;
}

それを行う他の方法はありますか?

4

1 に答える 1

3

これは簡単な例です(コードベースのフォームデザイン)。GUIデザイナを介してフォームを作成する方が良いでしょう。

private void button1_Click(object sender, EventArgs e)
{
    //test call of the rtBox
    ShowRichMessageBox("Test", File.ReadAllText("test.txt"));
}

/// <summary>
/// Shows a Rich Text Message Box
/// </summary>
/// <param name="title">Title of the box</param>
/// <param name="message">Message of the box</param>
private void ShowRichMessageBox(string title, string message)
{
    RichTextBox rtbMessage = new RichTextBox();
    rtbMessage.Text = message;
    rtbMessage.Dock = DockStyle.Fill;
    rtbMessage.ReadOnly = true;
    rtbMessage.BorderStyle = BorderStyle.None;

    Form RichMessageBox = new Form();
    RichMessageBox.Text = title;
    RichMessageBox.StartPosition = FormStartPosition.CenterScreen;

    RichMessageBox.Controls.Add(rtbMessage);
    RichMessageBox.ShowDialog();
}
于 2012-05-21T09:02:14.907 に答える