zoukankan      html  css  js  c++  java
  • 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 == '
    ')
                {
                    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]);
                    }
                }
    
            }
    
        }
    }
  • 相关阅读:
    spring和mybatis整合报错:org.springframework.beans.MethodInvocationException: Property 'dataSource' threw exception; nested exception is java.lang.NoClassDefFoundError
    SpringMVC静态资源拦截的问题
    初识SpringMVC
    Linux打tar包排除目录中的某个目录
    Angular的forEach无法通return跳出循环问题
    shell脚本编写保存
    Chrome浏览器偶尔提示错误net::ERR_EMPTY_RESPONSE的解决方法
    SVN右键菜单不显示
    JavaScript创建对象常用的两种方法
    JavaScript调试之alert和console.log()的区别
  • 原文地址:https://www.cnblogs.com/Mandy-ZQ-Ma/p/14467973.html
Copyright © 2011-2022 走看看