I have a WinForms application. I would like to be able to press a button on form2 which will then reflect on the richtextbox on form1.
For example, if the button on form2 is coded to type "Hello" upon being clicked, then I'd like the "Hello" text to appear on the richtextbox on form1.
How do I go about doing this? I've searched online but can't find anything.
Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Text;
using System.Drawing.Printing;
using System.Diagnostics;
namespace Basic_Word_Processor_Version1._0._0
{
public partial class Form1 : Form
{
public Form1()
{
Instance = this;
}
private string filepath = null;
private int checkPrint;
Code
public static Form1 Instance { get; private set; }
// You still need this like in the first scenario.
public RichTextBox RichTextBox1 { get { return richTextBoxPrintCtrl1; } }
// This constructor should already exist. Just add the one line to it.
}
Form3
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Basic_Word_Processor_Version1._0._0
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
Form1.Instance.richTextBoxPrintCtrl1.Text = "";
}
public partial class Form1 : Form
{
public static Form1 Instance { get; private set; }
public RichTextBox RichTextBox1 { get { return richTextBoxPrintCtrl1; } }
public Form1()
{
InitializeComponent();
Instance = this;
}
}
You could expose the control via a property. Assuming you have a reference to form1 in form2:
In form1:
public RichTextBox PrintCtrl1 { get { return richTextBoxPrintCtrl1; } }
In form2:
form1.PrintCtrl1.Text = "Howdy from form2.";
Update: If you don't have a reference to form1 in form2, you can expose the instance of form1 also via a static property:
In form1:
public static Form1 Instance { get; private set; }
// You still need this like in the first scenario.
public RichTextBox PrintCtrl1 { get { return richTextBoxPrintCtrl1; } }
// This constructor should already exist. Just add the one line to it.
public Form1()
{
Instance = this;
}
And then in form2, you'd do this instead of what I showed above:
Form1.Instance.PrintCtrl1.Text = "Howdy from form2.";
Your Form1 class should now look like this (plus anything else you've added):
public partial class Form1 : Form
{
public static Form1 Instance { get; private set; }
public RichTextBox PrintCtrl1 { get { return richTextBoxPrintCtrl1; } }
public Form1()
{
InitializeComponent();
Instance = this;
}
}
And your Form3 class should look like this:
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
protected void button1_Click(object sender, EventArgs e)
{
Form1.Instance.PrintCtrl1.Text = "";
}
}
I know there is already an accepted answer on this page, and yes, while the answer will "work" it is bad for two reasons. First, getting in the habit of using static to gain visibility to things is a very bad habit to get into, and when used unnecessarily, violates the concepts of OOP programming. Secondly, by doing the public static form instance, you have made it so that the second form is non reusable. It cannot do anything but interact with the first form. A better approach is to use events to facilitate the communication between your forms. The following code sample demonstrates how to do this.
Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Declare your new form
Form2 form2 = new Form2();
//Register the event
form2.changeTextEvent += new EventHandler<TextChangedEventArgs> (form2_changeTextEvent);
//Show your new form
form2.ShowDialog();
}
//Handler for the event from form 2
void form2_changeTextEvent(object sender, TextChangedEventArgs e)
{
//Sets the text of this form equal to the text in our custom event args
//Just a simple example of doing something with the event arg
this.Text = e.Text;
}
}
Form2:
public partial class Form2 : Form
{
//Declare your event
public event EventHandler<TextChangedEventArgs> changeTextEvent;
private String newText = "Custom events FTW!!!";
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//If the event is registered fire it, otherwise do nothing
if (changeTextEvent != null)
{
//fire the event and give our custom event args some text
changeTextEvent(sender, new TextChangedEventArgs(newText));
}
}
}
Custom Event Arg:
public class TextChangedEventArgs : EventArgs
{
private String text;
//Did not implement a "Set" so that the only way to give it the Text value is in
//the constructor
public String Text
{
get { return text; }
}
public TextChangedEventArgs(String theText)
: base()
{
text = theText;
}
}
Implemented in this manner, Form2 is now completely reusable and can trigger an event in any control/form that registers the event. Different forms could react to the event in different ways but form2 never needs to change.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.