C#动态生成控件,并添加事件处理,获取控件值并保存

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

        private void Test_Load(object sender, EventArgs e)
        {
            int m = 7;
            int n = 2;

            //if (m * n < 14)
            //{ 
            //   label(m*n).Text  =
            //}

            for (int i = 1; i <= m * n; i++)
            {
                //动态生成label
                Label lbl = new Label();
                lbl.Size = new Size(80, 20);
                lbl.Location = new Point(5, 30 * i);
                lbl.Text = "标签条码" + i+":";
                this.Controls.Add(lbl);
                
                //动态生成文本框
                TextBox txt = new TextBox();
                txt.Size = new Size(222, 20);
                txt.Margin = new System.Windows.Forms.Padding(0);
                txt.Location = new Point(90, 30 * i);
                //txt.TabIndex = i;
                txt.Name = "txtSN" + i;
                //txt.Focus();
                //txt.SelectAll();
                this.Controls.Add(txt);
                txt.KeyPress += new KeyPressEventHandler(txtSN_KeyPress); //添加动态生成控件事件

            }

            Button btn = new Button();
            btn.Size = new Size(60, 40);
            btn.Location = new Point(45, 30 * m * n + 35);
            btn.Text = "保存";
            btn.Name = "BtnSave";
            //btn.Click(sender,e);
            this.Controls.Add(btn);
            btn.Click += new EventHandler(BtnSave_Click);

            Button btn1 = new Button();
            btn1.Size = new Size(60, 40);
            btn1.Location = new Point(195, 30 * m * n + 35);
            btn1.Text = "退出";
            btn1.Name = "BtnExit";
            this.Controls.Add(btn1);
            btn1 .Click +=new EventHandler(btn1_Click);
            //Test.ActiveForm.Height = 800;
            this.Width  = 350;
            this.Height = 32 * m * n + 100;
        }

        //将触发此事
        private void txtSN_KeyPress(object sender, KeyPressEventArgs e) 
        {
            if (e.KeyChar == '\r')
            {
                Control ctrl = this.GetNextControl(this.ActiveControl, true);
                while (ctrl is TextBox == false)
                {
                    ctrl = this.GetNextControl(ctrl, true);
                }
                ctrl.Focus();
            }
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            this.Dispose(); //释放资源
            this.Close(); //关闭程序(窗口)
        }
        private void BtnSave_Click(object sender, EventArgs e)
        {
            int m = 7;
            int n = 2;
            int i = 1;
            List<string> SN = new List<string>();
            //获取数据
            foreach (Control c in this.Controls)
            {
                if (c.Name.Equals("txtSN"+i)) //if (c.Name.Equals("txtSN1"))
                {
                    if (c.Text == "")
                    {
                        MessageBox.Show("条码标签" + i + "为空!请检查!");
                        return ;
                    }
                    //将数据存入数组SN
                    SN.Add(c.Text);
                    i = i + 1;
                }
            }
            string str = SN[1].ToString ();
            //HashSet检查数组是否重复
            HashSet<string> hs = new HashSet<string>();
            for (int j = 1; j <= SN.Count; j++)
            {
                if (hs.Contains(SN[j-1]))
                {
                    MessageBox.Show("条码标签" + j + "数据重复!请检查!");
                    break;
                }
                else
                {
                   hs.Add(SN[j-1]);
                }
            }

        }

    }
}

 

posted @ 2021-03-02 11:17  它的眼角开过光  阅读(955)  评论(0编辑  收藏  举报