Form遍歷所有控制元件
阿新 • • 發佈:2020-08-07
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); }private void Form1_Load(object sender, EventArgs e) { Control.ControlCollection t = this.Controls;//Controls foreach (Control item in t) { if (item.GetType().Name.Equals("TextBox")) { string[] str = item.Name.Split('_'); if (str.Length > 1) { item.Text = str[1]; } } if (item is TextBox) { string[] str = item.Name.Split('_'); if (str.Length > 1) { item.Text = str[1]; } } if (item is Button) { string[] str = item.Name.Split('_'); if (str.Length > 1) { item.Text = str[1]; } } } } } }