I think the answers from David in Dakota, and Jeff Donnici, above, give you all you need, but I'll "flesh out" a trivial example of the broader approach wisely suggested by Jeff that I hope will get you started.
One slight area of disagreement I may have with Jeff's answer is that I believe that any solution to this problem is a variation of "dependency injection" or "dependency management" : even if you go the "most abstract" route and have "publishers" "broadcast" events to "unknown subscribers," but that's just my personal opinion.
Here's a simple example of the type that was helpful for me to understand in the past.
Create a single public static class into which you insert static references to the TextBoxes on all Forms you want to possibly modify (or store the Forms themselves, or whatever) : define public static methods to move data from one form's textbox to the other(s) : a very quick outline : (use of extra long variable names is deliberate, and is for expository purposes only)
using System;
using System.Collections.Generic;
using System.Windows.Forms;
// note : compiles against FrameWork 2.0 and 4.0
// wanted this to work w/o Linq, automatic properties, etc.
namespace MessageHandler
{
public static class TextBoxMessenger
{
// internal List of TextBoxes of interest on all Forms
internal static List<TextBox> _messageEnabledTBxes = new List<TextBox>();
// public Property to get/set the collection of TextBoxes of interest
public static List<TextBox>MessageEnabledTextBoxes
{
get { return _messageEnabledTBxes; }
set { _messageEnabledTBxes = value; }
}
// in case you want to register one TextBox at a time
public static void RegisterTextBoxForMessaging(TextBox theTBx)
{
_messageEnabledTBxes.Add(theTBx);
}
// send from one specific TBx to another
public static void setText(TextBox originTBx, TextBox destinationTBx)
{
destinationTBx.Text = originTBx.Text;
}
// send to a specified list of TextBoxes
public static void setText(TextBox originTBx, List<TextBox> destinationTBxs)
{
foreach (TextBox theTBx in destinationTBxs)
{
theTBx.Text = originTBx.Text;
}
}
// set text in all other TextBoxes in MessageEnabledTextBoxes list
public static void setText(TextBox originTBx)
{
foreach (TextBox theTBx in _messageEnabledTBxes)
{
// a needless check, since assigning the text of the
// original TextBox to itself wouldn't "hurt" anything
// but, imho, much better "practice" to always test
if (theTBx != originTBx) theTBx.Text = originTBx.Text;
}
}
}
}
So in action how does this work : let's use an example where your Form1 Load event looks like this :
// assume Form2 has a single TextBox on it named 'textBox1'
public Form2 myForm2;
private void Form1_Load(object sender, EventArgs e)
{
myForm2 = new Form2();
myForm2.Show();
// register all the TextBoxes
// note the redundant use of 'this here : it's a deliberate choice to make
// the code communicate to a future user/reader/maintainter
TextBoxMessenger.RegisterTextBoxForMessaging(this.textBox1);
TextBoxMessenger.RegisterTextBoxForMessaging(this.textBox2);
TextBoxMessenger.RegisterTextBoxForMessaging((TextBox)myForm2.Controls["textBox1"]);
// or ...
//TextBoxMessenger.MessageEnabledTextBoxes = new List<TextBox>
//{
// this.textBox1, this.textBox2, (TextBox)myForm2.Controls["textBox1"]
//};
}
And you might test the above like this by putting a button on Form1 :
private void button1_Click(object sender, EventArgs e)
{
// tests
// one to all ...
//TextBoxMessenger.setText(this.textBox1);
// one to a specific TextBox on another Form
//TextBoxMessenger.setText(this.textBox1, (TextBox) myForm2.Controls["textBox1"]);
// one to a List<TextBox>
TextBoxMessenger.setText(this.textBox1, new List<TextBox> { this.textBox2, (TextBox)myForm2.Controls["textBox1"]});
}
But, note the "ugliness," the "bad code smell," of things like :
(TextBox) myForm2.Controls["textBox1"] // casting is evil ! set a bloody reference !
That's the kind of thing you want to get rid of by moving beyond an example like this into the "arena" that Jeff is, I believe, pointing you to.
Hope this is helpful. In the "long run" the abstraction of message passing into a higher-level, which I believe Jeff prescribes, is, imho, the "royal road" to making code examples much more powerful and more "generalizable" than this example. best,