-3

I have 2 forms which consist of:

Form1:

2buttons named: btnCopy and btnPaste (with functions inside like rtb.Copy(); and rtb.Paste(); that should work for richtextbox in Form2)

Form2:

1richtextbox named: rtb

My question was: How can I communicate between the 2buttons from Form1 (with its functions) and the richtextbox in Form2.

like: When I type text inside richtextbox(rtb) in Form2 then i SelectAll text then I Press the CopyButton(btnCopy) from Form1, text should be copied same as when I Press PasteButton(btnPaste) from Form1, text that has been copied should be Paste in RichTextBox(rtb) that could be Found on Form2 .

How can I do that?

microMolvi
  • 636
  • 11
  • 30
Elegiac
  • 366
  • 10
  • 25

4 Answers4

1

Let's say you have Form1 and ToolStrip Button name PasteToolStripButton like:

  public partial class Form1 : Form
{
    Form2 formChild;

    public Form1()
    {
        InitializeComponent();
        this.Load += new EventHandler(Form1_Load);
    }

    void Form1_Load(object sender, EventArgs e)
    {
       formChild = new Form2();
       formChild.MdiParent = this;
       formChild.Show();            
    }


   private void CopyToolStripButton_Click(object sender, EventArgs e)
    {
        formChild.CopyText(); // Method to copy Rich Text Box in Form2
    }

    private void PasteToolStripButton_Click(object sender, EventArgs e)
    {
        formChild.PasteText(); // Method in Form2 to Paste to the RichTextBox in Form2
    }

}

In your Form2 you need to add a Public method named PasteText and CopyText like:

  public void PasteText()
  {
     rtbChild.Text = Clipboard.GetText(); // this one simulates the rtb.Paste()
  }

  public void CopyText()
  {
     rtb.Copy(); 
  }

I also named the RichTextBox in Form2 as rtbChild so every time you click for example paste in will be copied in your RichTextBox in Form2.

Edper
  • 9,144
  • 1
  • 27
  • 46
  • wheres the rtb.Copy code and rtb.Paste execution in here sir? – Elegiac Jun 10 '13 at 04:39
  • error in form1 the name 'rtb' does not exit in the current context ... the rtb must be in form2 sir ... @Edper – Elegiac Jun 10 '13 at 05:05
  • change rtb to the name of your RichTextBox let's say if the name is RichTextBox1 then it should be RichTextBox1. – Edper Jun 10 '13 at 05:06
  • the rtb must be in form2 sir ... 2buttons only on form1 and form2 with a richtextbox – Elegiac Jun 10 '13 at 05:07
  • i also i try to commend the "rtb.Copy();" but another error appears pointing the content inside Form1_Load : Form that was specified to be the MdiParent for this form is not an MdiCOntainer. – Elegiac Jun 10 '13 at 05:09
  • You need to make your Form1 property IsMdiContaier to True. I added on the Form2 to Copy text from rtb in form2. check my update. – Edper Jun 10 '13 at 05:21
  • i commented this line: //formChild.MdiParent = this; and it worked coz it brought an error ... you can test that error if you want sir – Elegiac Jun 10 '13 at 05:35
  • sir can we fix the error for formChild.MdiParent = this? @Edper – Elegiac Jun 10 '13 at 05:39
  • I add the the formChild.MdiParent = this; so that Form2 will be inside the Mdi parent Form1. If you don't want then you could remove that. – Edper Jun 10 '13 at 05:43
0

Create a public property on Form1 then set it from Form2. EDIT: On Form1: public string TextForRTB {get; set;}

On Form2: Form1 a = new Form1(); a.TextForRtb = rtb.Text;

David Fenko
  • 131
  • 2
  • 8
0

Sol1: Pass one of the forms to the other, as Form1(Form parent){....} in the constructor, then you should see it's public properties and methods.

Sol2: Create custom events to raise it when text changed on your rich text box, so than the forms that initialized the form with this rich box will do something, like enable/disable a button or something

...Actually, there is a lot of solutions to this kind of behavior, and I wonder why you need to put your text box in a different form from your buttons that seems to be related very closely in business logic together!

sameh.q
  • 1,691
  • 2
  • 23
  • 48
0

You could expose 2 methods GetRichTextBoxContent and SetRichTextBoxContent in Form2. Which would update the contents of richTextBox in Form2.

Then you could work on the Instance of Form2 form Form1

Note: The major think here is how you get the Instance of Form2. It is up to your implementation to get that instance.

public class Form2 : Form
{
    public string GetRichTextBoxContent()
    {
        return this.richTextBox1.Text;
    }

    public void SetRichTextBoxContent(string content)
    {
        this.richTextBox1.Text = content;
    } 
}

public class Form1 : Form
{
    //Based on your implementation 
    Form2 form2 = new Form2();

    private void Button_CopyClick(object sender, EventArgs e)
    {
        var contentFromRtb = form2.GetRichTextBoxContent();
    } 
    private void Button_PasteClick(object sender, EventArgs e)
    {
        var someContent = "Content to be copied to text box"
        form2.SetRichTextBoxContent(someContent );
    } 
}
jacob aloysious
  • 2,547
  • 15
  • 16
  • wheres the rtb.Copy code and rtb.Paste execution in here sir? – Elegiac Jun 10 '13 at 04:39
  • btw, you dont need `copy` or `Paste` as `this.richTextBox1.Text` would get and set the contents for you.. – jacob aloysious Jun 10 '13 at 04:44
  • it doesnt work sir ... everytime i select text from rtb then press the button_copy from form1 and button_paste then i select in rtb theres no change sir ... it keeps its focus on buttons .what should i do? – Elegiac Jun 10 '13 at 04:56