В этом примере, пользователь вычисляет в дочерней форме расчет за электроэнергию, сумма одновременно отображается и в дочерней форме и в родительской. Точно так же и с расчетом за воду. В родительской форме эти суммы одновременно складываются.
Counter.cs
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespace_0020 {-
classCounter { -
ushortshowStart; -
ushortshowEnd; -
decimalprice; -
publicCounter(stringarg0,stringarg1,stringarg2) { -
ushort.TryParse(arg0,outshowStart); -
ushort.TryParse(arg1,outshowEnd); -
decimal.TryParse(arg2,outprice); -
} -
publicushortResultShow() { -
return(ushort)(showEnd - showStart); -
} -
publicdecimalSum() { -
returnMath.Round((decimal)(ResultShow() * price) , 2); -
} -
} }
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespace_0020 {-
classCounter { -
ushortshowStart; -
ushortshowEnd; -
decimalprice; -
publicCounter(stringarg0,stringarg1,stringarg2) { -
ushort.TryParse(arg0,outshowStart); -
ushort.TryParse(arg1,outshowEnd); -
decimal.TryParse(arg2,outprice); -
} -
publicushortResultShow() { -
return(ushort)(showEnd - showStart); -
} -
publicdecimalSum() { -
returnMath.Round((decimal)(ResultShow() * price) , 2); -
} -
} }
GeneralSum.cs
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespace_0020 {-
classGeneralSum { -
publicstaticdecimalAdd(paramsstring[] args) { -
decimalres = 0; -
for(inti=0; i<args.Length; i++) { -
decimalval; -
decimal.TryParse(args[i],outval); -
res += val; -
} -
returnMath.Round(res, 2); -
} -
} }
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespace_0020 {-
classGeneralSum { -
publicstaticdecimalAdd(paramsstring[] args) { -
decimalres = 0; -
for(inti=0; i<args.Length; i++) { -
decimalval; -
decimal.TryParse(args[i],outval); -
res += val; -
} -
returnMath.Round(res, 2); -
} -
} }
Form2Elcectro.cs
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespace_0020 {-
publicpartialclassForm2Elcectro : Form { -
publicForm2Elcectro() { -
InitializeComponent(); -
} -
privatevoidbutton1_Click(objectsender, EventArgs e) { -
//вычисляем сумму -
Counter C =newCounter(textBox1.Text, textBox2.Text, textBox3.Text); -
textBox4.Text = C.Sum().ToString(); -
//создаем переменную класса Form1 и присваиваем ей значение через ссылку -
Form1 F1 = (Form1)this.Owner; -
//текстовому полю родительской формы присваиваем значение суммы через свойство -
F1.TextBox1SumElectro.Text = C.Sum().ToString(); -
} -
privatevoidbutton2_Click(objectsender, EventArgs e) { -
//кнопка Очистить очищает все текстовые поля -
foreach(Control iinControls) { -
if(i.GetType() ==typeof(TextBox)) { -
i.Text =string.Empty; -
} -
} -
} -
} }
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespace_0020 {-
publicpartialclassForm2Elcectro : Form { -
publicForm2Elcectro() { -
InitializeComponent(); -
} -
privatevoidbutton1_Click(objectsender, EventArgs e) { -
//вычисляем сумму -
Counter C =newCounter(textBox1.Text, textBox2.Text, textBox3.Text); -
textBox4.Text = C.Sum().ToString(); -
//создаем переменную класса Form1 и присваиваем ей значение через ссылку -
Form1 F1 = (Form1)this.Owner; -
//текстовому полю родительской формы присваиваем значение суммы через свойство -
F1.TextBox1SumElectro.Text = C.Sum().ToString(); -
} -
privatevoidbutton2_Click(objectsender, EventArgs e) { -
//кнопка Очистить очищает все текстовые поля -
foreach(Control iinControls) { -
if(i.GetType() ==typeof(TextBox)) { -
i.Text =string.Empty; -
} -
} -
} -
} }
Form3Water.cs
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespace_0020 {-
publicpartialclassForm3Water : Form { -
publicForm3Water() { -
InitializeComponent(); -
} -
privatevoidbutton1_Click(objectsender, EventArgs e) { -
//вычисляем сумму -
Counter C =newCounter(textBox1.Text, textBox2.Text, textBox3.Text); -
textBox4.Text = C.Sum().ToString(); -
//создаем переменную класса Form1 и присваиваем ей значение через ссылку -
Form1 F1 = (Form1)this.Owner; -
//текстовому полю родительской формы присваиваем значение суммы через свойство -
F1.TextBox1SumWater.Text = C.Sum().ToString(); -
} -
privatevoidbutton2_Click(objectsender, EventArgs e) { -
//кнопка Очистить очищает все текстовые поля -
foreach(Control iinControls) { -
if(i.GetType() ==typeof(TextBox)) { -
i.Text =string.Empty; -
} -
} -
} -
} }
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespace_0020 {-
publicpartialclassForm3Water : Form { -
publicForm3Water() { -
InitializeComponent(); -
} -
privatevoidbutton1_Click(objectsender, EventArgs e) { -
//вычисляем сумму -
Counter C =newCounter(textBox1.Text, textBox2.Text, textBox3.Text); -
textBox4.Text = C.Sum().ToString(); -
//создаем переменную класса Form1 и присваиваем ей значение через ссылку -
Form1 F1 = (Form1)this.Owner; -
//текстовому полю родительской формы присваиваем значение суммы через свойство -
F1.TextBox1SumWater.Text = C.Sum().ToString(); -
} -
privatevoidbutton2_Click(objectsender, EventArgs e) { -
//кнопка Очистить очищает все текстовые поля -
foreach(Control iinControls) { -
if(i.GetType() ==typeof(TextBox)) { -
i.Text =string.Empty; -
} -
} -
} -
} }
Form1.cs
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespace_0020 {-
publicpartialclassForm1 : Form { -
publicForm1() { -
InitializeComponent(); -
} -
//создаем свойство для доступа к полю из дочерней формы через ссылку -
publicTextBox TextBox1SumElectro { -
set{ -
textBox1 = value; -
} -
get{ -
returntextBox1; -
} -
} -
//создаем свойство для доступа к полю из дочерней формы через ссылку -
publicTextBox TextBox1SumWater { -
set{ -
textBox2 = value; -
} -
get{ -
returntextBox2; -
} -
} -
privatevoidForm1_Activated(objectsender, EventArgs e) { -
//при активации формы вычисляется общая сумма -
textBox3.Text = GeneralSum.Add(textBox1.Text, textBox2.Text).ToString(); -
} -
privatevoidbutton3_Click(objectsender, EventArgs e) { -
//кнопка Очистить очищает все текстовые поля -
foreach(Control iinControls) { -
if(i.GetType() ==typeof(TextBox)) { -
i.Text =string.Empty; -
} -
} -
} -
privatevoidbutton1_Click(objectsender, EventArgs e) { -
//открываем дочернюю форму и передаем ей ссылку -
newForm2Elcectro().ShowDialog(this); -
} -
privatevoidbutton2_Click(objectsender, EventArgs e) { -
//открываем дочернюю форму и передаем ей ссылку -
newForm3Water().ShowDialog(this); -
} -
} }
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespace_0020 {-
publicpartialclassForm1 : Form { -
publicForm1() { -
InitializeComponent(); -
} -
//создаем свойство для доступа к полю из дочерней формы через ссылку -
publicTextBox TextBox1SumElectro { -
set{ -
textBox1 = value; -
} -
get{ -
returntextBox1; -
} -
} -
//создаем свойство для доступа к полю из дочерней формы через ссылку -
publicTextBox TextBox1SumWater { -
set{ -
textBox2 = value; -
} -
get{ -
returntextBox2; -
} -
} -
privatevoidForm1_Activated(objectsender, EventArgs e) { -
//при активации формы вычисляется общая сумма -
textBox3.Text = GeneralSum.Add(textBox1.Text, textBox2.Text).ToString(); -
} -
privatevoidbutton3_Click(objectsender, EventArgs e) { -
//кнопка Очистить очищает все текстовые поля -
foreach(Control iinControls) { -
if(i.GetType() ==typeof(TextBox)) { -
i.Text =string.Empty; -
} -
} -
} -
privatevoidbutton1_Click(objectsender, EventArgs e) { -
//открываем дочернюю форму и передаем ей ссылку -
newForm2Elcectro().ShowDialog(this); -
} -
privatevoidbutton2_Click(objectsender, EventArgs e) { -
//открываем дочернюю форму и передаем ей ссылку -
newForm3Water().ShowDialog(this); -
} -
} }