Button click Open richTextBox and display the readfile

c#, visual-studio-2010, winforms

Solution

Here is a simple example (code based form design). It's better if you create the form via the GUI designer:

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

Problem

I have 2 buttons and I read different files when I click on these buttons. I used the to display the readfile using `MsgBox` since the files are big, so i want to display it in a `richTextBox`. How can I open a `richTextBox` and display the `read file` when I click on any one of these buttons??? ``` 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; } ``` Is there any other way to do it?

Original source