user1108948
user1108948

Reputation:

Access the richtextbox in the second form

In my windows form application, I added two forms: Form1 and Form2. There is a button in Form1 and a richtextbox in Form2. What I expect is once I click the button in Form1, Form2 is shown and also a file dialog displays.

Now I want to load a text from file to the rich text box, the question is how to access the richtextbox from the code?

 public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.Filter = "txt files (*.txt)|*.txt";
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            try
            {
                this.Hide();
                Form Form2 = new Form();
                Form2.Show();
                // load a text file to rich text box. How to access the rich text box here?
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
            }
        }

    }

UPDATE:

I tried to create an instance of Form2 and pass a string to its constructor, but it is not working. image

 public partial class Form2 : Form
{
    public Form2(string text)
    {
        InitializeComponent();
        richTextBox1.Text = text;
    }
}

Upvotes: 0

Views: 1933

Answers (3)

Gayan Akmeemana
Gayan Akmeemana

Reputation: 41

In Form1

namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.Hide();
        Form2 form2 = new Form2();
        form2.Show();
    }
  }
}

In Form2

namespace WindowsFormsApplication7
{
public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.Filter = "txt files (*.txt)|*.txt";
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            string text = File.ReadAllText(openFileDialog1.FileName);
            richTextBox1.Text = text;
        }
    }
  }
}

Upvotes: 0

ADP_X
ADP_X

Reputation: 333

Or you could declare the richtextbox from Form2 as public, or even declare a public property for loading data on it, and Access directly from Form1.

[Updated]

I may have explained it incorrectly. I meant make the richtextbox public, not just the form.

enter image description here

And after that just access to the control to load whatever you want:

Form2 objForm2 = new Form2(); 

        //Open file, etc... 

        objForm2.richTextBoxForm2.Text = "XXXX";
        objForm2.Show();
        objForm2.BringToFront();

Upvotes: 0

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236328

Just pass text via Form2 constructor (you should modify its constructor or add new one):

string text = File.ReadAllText(openFileDialog1.FileName);
Form2 form2 = new Form2(text);
form2.Show();

Here is how constructor should look like:

public Form2(string text)
{
    InitializeComponent();
    richtextbox.Text = text;
}

Bad solution: simply select richtextbox in designer and change its Modifiers property to public. You will break encapsulation of form, but control will be accessible outside Form2 class.

Upvotes: 2

Related Questions