1. 程式人生 > 實用技巧 >Form遍歷所有控制元件

Form遍歷所有控制元件

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]; } } } } } }