, , , , , "" , , , , .
, , , , " " " ": " " "" "" " ", .
, .
, , ( - ): (): : ( )
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace MessageHandler
{
public static class TextBoxMessenger
{
internal static List<TextBox> _messageEnabledTBxes = new List<TextBox>();
public static List<TextBox>MessageEnabledTextBoxes
{
get { return _messageEnabledTBxes; }
set { _messageEnabledTBxes = value; }
}
public static void RegisterTextBoxForMessaging(TextBox theTBx)
{
_messageEnabledTBxes.Add(theTBx);
}
public static void setText(TextBox originTBx, TextBox destinationTBx)
{
destinationTBx.Text = originTBx.Text;
}
public static void setText(TextBox originTBx, List<TextBox> destinationTBxs)
{
foreach (TextBox theTBx in destinationTBxs)
{
theTBx.Text = originTBx.Text;
}
}
public static void setText(TextBox originTBx)
{
foreach (TextBox theTBx in _messageEnabledTBxes)
{
if (theTBx != originTBx) theTBx.Text = originTBx.Text;
}
}
}
}
, , : , Form1 Load :
public Form2 myForm2;
private void Form1_Load(object sender, EventArgs e)
{
myForm2 = new Form2();
myForm2.Show();
TextBoxMessenger.RegisterTextBoxForMessaging(this.textBox1);
TextBoxMessenger.RegisterTextBoxForMessaging(this.textBox2);
TextBoxMessenger.RegisterTextBoxForMessaging((TextBox)myForm2.Controls["textBox1"]);
}
, , Form1:
private void button1_Click(object sender, EventArgs e)
{
TextBoxMessenger.setText(this.textBox1, new List<TextBox> { this.textBox2, (TextBox)myForm2.Controls["textBox1"]});
}
"", " ", , :
(TextBox) myForm2.Controls [ "textBox1" ]// - ! !
, , , , "", , , .
, . " " , , , , , - imho, " ", "", . ,