Как обратиться ко всем текстовым полям формы одновременно? - C#
Формулировка задачи:
Есть кнопка CLEAR для очистки всех текстовых полей. Как их все обнулить?
Програмка прилагается
Спасибо
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Lin_i_kvadr_uravneniya
{
public partial class Form_square : Form
{
public Form_square()
{
InitializeComponent();
}
private void label7_Click(object sender, EventArgs e)
{
}
private double Discrim(double a, double b, double c)
{
return b * b - 4 * a * c;
}
private void button_solution_Click(object sender, EventArgs e)
{
try
{
double a = Convert.ToDouble(textBox_a.Text);
double b = Convert.ToDouble(textBox_b.Text);
double c = Convert.ToDouble(textBox_c.Text);
double d = Discrim(a,b,c);
if (d < 0)
{
textBox_d.Text = d.ToString();
label_count.Text = "no solution";
label_count.ForeColor = Color.Red;
}
else
{
label_count.Text = "two solutions";
label_count.ForeColor = Color.Black;
textBox_d.Text = d.ToString();
textBox_x1.Text =String.Format("{0:0.00}", (-b + Math.Sqrt(d)) / (2 * a));
textBox_x2.Text = String.Format("{0:0.00}", (-b - Math.Sqrt(d)) / (2 * a));
}
}
catch (FormatException)
{
MessageBox.Show("enter numeric values", "attention!!!!!!!!!");
}
}
// как обратиться ко всем текстовым полям одновременно? Чтобы не писать несколько раз одно и то же.
private void button1_Click(object sender, EventArgs e)
{
textBox_a.Text = null;
textBox_b.Text = null;
textBox_c.Text = null;
textBox_x1.Text = null;
textBox_x2.Text = null;
label_count.Text = null;
textBox_d.Text = null;
}
}
}Решение задачи: «Как обратиться ко всем текстовым полям формы одновременно?»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication
{
public partial class MainForm : Form
{
private List<Control> AllControls;
public MainForm() {
InitializeComponent();
AllControls = new List<Control>();
}
private void GetControlRecursiveInternal(Control root) {
foreach (Control c in root.Controls) {
AllControls.Add(c);
if (c.Controls != null)
GetControlRecursiveInternal(c);
}
}
/// <summary>
/// Находит все контролы, вложенные в контролы корневого элемента
/// вне зависимости от уровня вложенности.
/// </summary>
/// <param name="root">Корневой элемента, например, форма</param>
public IEnumerable<Control> GetControlRecursive(Control root) {
if (root == null || root.Controls == null)
return Enumerable.Empty<Control>();
AllControls.Clear();
GetControlRecursiveInternal(root);
return AllControls;
}
private void button1_Click(object sender, EventArgs e) {
var sb = new StringBuilder();
foreach (Control ctrl in GetControlRecursive(this))
sb.AppendFormat("{0} (of type {1})\r\n",
ctrl.Name, ctrl.GetType().ToString());
MessageBox.Show(sb.ToString());
}
}
}