C#查找动态添加修改控件



C#查找动态添加修改控件


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 BoxListDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {


        }
        private int currentHeight = 12;
        private void button1_Click(object sender, EventArgs e)
        {//在大Panel中添加控件
            Panel pz = Panel_Test;//把建好的大Panel传入Panel对象中

            Panel p = new Panel();//创建一个小的panel

            //为了防止滚动条变化后位置变化

            p.AutoScrollPosition = new Point(0, 0);

            p.Size = new Size(600, 30);
            Label l1 = new Label();//创建文本
            l1.Size = new Size(400, 25);
            l1.Text = "username:000.000.000.000:12345";
            l1.Location = new Point(0, 2);
            p.Controls.Add(l1);
            CheckBox ch1 = new CheckBox();//创建多选框
            ch1.Size = new Size(200, 25);
            DateTime d = DateTime.Now;
            //设置时间以区分不同的多选框
            ch1.Text = d.ToLongTimeString();
            //设置相对容器的位置
            ch1.Location = new Point(400, 2);
            
            
            p.Controls.Add(ch1);
            p.Location = new Point(12, currentHeight);
            pz.Controls.Add(p);
            currentHeight += p.Size.Height + 10;


        }


        private void button2_Click(object sender, EventArgs e)
        {//找到选中的行
            List<CheckBox> arrs = new List<CheckBox>();
            foreach(object o in Panel_Test.Controls)//寻找大Panel下的控件
            {
                //Console.WriteLine(o.ToString());
                Panel p = null;//由于嵌套了一层找到是小Panel
                if((p = o as Panel) != null)
                {
                    CheckBox ch = null;
                    ch = p.Controls[1] as CheckBox;//在小Panel可以找到多选框
                    if (ch != null && ch.Checked)
                    {
                        arrs.Add(ch);
                        Console.WriteLine(ch.Text);
                    }
                }
                
            }
        }


       
        private void button3_Click(object sender, EventArgs e)//删除后添加会有问题一般是全删后刷新
        {//删除选中的行
            List<CheckBox> arrs = new List<CheckBox>();
            Panel[] rel = new Panel[Panel_Test.Controls.Count];
            int i = 0;
            foreach (object o in Panel_Test.Controls)//注意在循环时删除是有问题会改变总长度ch有相邻会有问题
            {
                //Console.WriteLine(o.ToString());
                Panel p = null;//由于嵌套了一层
                if ((p = o as Panel) != null)
                {
                    CheckBox ch = null;
                    ch = p.Controls[1] as CheckBox;
                    if (ch != null && ch.Checked)
                    {
                        arrs.Add(ch);
                        Console.WriteLine(ch.Text);
                        rel[i] = p;
                    }
                }
                i++;
            }
            foreach(Panel iss in rel)
            {
                if(iss != null)
                {
                    Panel_Test.Controls.Remove(iss);
                }


            }

        }

private void button4_Click(object sender, EventArgs e)
        {//简单的刷新
            int k = Panel_Test.Controls.Count;
            for(int i = 0; i < k; i++)
            {
                Panel_Test.Controls.RemoveAt(0);
            }
            currentHeight = 12;
        }

    }
}