В дочерней форме переопределяем метод Show()/ShowDialog(), который будет принимать аргумент, присваивающий значение текстовому полю.
В родительской форме, при создании объекта дочерней формы, в метод Show()/ShowDialog() передадим текстовое поле, значение которого будет присвоено текстовому полю дочерней формы.
Передать данные можно только один раз, в момент создания дочерней формы.
В родительской форме, при создании объекта дочерней формы, в метод Show()/ShowDialog() передадим текстовое поле, значение которого будет присвоено текстовому полю дочерней формы.
Передать данные можно только один раз, в момент создания дочерней формы.
Counter.cs
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespace_0023 {-
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_0023 {-
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); -
} -
} }
Form2Child.cs
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespace_0023 {-
publicpartialclassForm2Child : Form { -
publicForm2Child() { -
InitializeComponent(); -
} -
//перегрузка метода Show()/ShowDialog(), через него присваиваем значение полю -
publicDialogResult ShowDialog(stringarg) { -
textBox3.Text = arg; -
returnShowDialog(); -
} -
privatevoidbutton1_Click(objectsender, EventArgs e) { -
//вычисляем сумму -
Counter C =newCounter(textBox1.Text, textBox2.Text, textBox3.Text); -
textBox4.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_0023 {-
publicpartialclassForm2Child : Form { -
publicForm2Child() { -
InitializeComponent(); -
} -
//перегрузка метода Show()/ShowDialog(), через него присваиваем значение полю -
publicDialogResult ShowDialog(stringarg) { -
textBox3.Text = arg; -
returnShowDialog(); -
} -
privatevoidbutton1_Click(objectsender, EventArgs e) { -
//вычисляем сумму -
Counter C =newCounter(textBox1.Text, textBox2.Text, textBox3.Text); -
textBox4.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_0023 {-
publicpartialclassForm1 : Form { -
publicForm1() { -
InitializeComponent(); -
} -
privatevoidbutton1_Click(objectsender, EventArgs e) { -
//вызываем дочернюю форму -
//в конструкторе передаем значение текстового поля -
newForm2Child().ShowDialog(textBox1.Text); -
} -
privatevoidbutton2_Click(objectsender, EventArgs e) { -
//кнопка Очистить очищает все текстовые поля -
textBox1.Clear(); -
} -
} }
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespace_0023 {-
publicpartialclassForm1 : Form { -
publicForm1() { -
InitializeComponent(); -
} -
privatevoidbutton1_Click(objectsender, EventArgs e) { -
//вызываем дочернюю форму -
//в конструкторе передаем значение текстового поля -
newForm2Child().ShowDialog(textBox1.Text); -
} -
privatevoidbutton2_Click(objectsender, EventArgs e) { -
//кнопка Очистить очищает все текстовые поля -
textBox1.Clear(); -
} -
} }