winform 代码生成textbox ,checkbox

参考地址:https://jingyan.baidu.com/article/380abd0a6b80701d90192cde.html

 

首先搭建好Winform项目框架后,创建窗体页面后自行布局

这里我放置了1个TextBox,3个Button

 

 

  1 using System.ComponentModel;
  2 using System.Data;
  3 using System.Drawing;
  4 using System.Linq;
  5 using System.Text;
  6 using System.Text.RegularExpressions;
  7 using System.Threading.Tasks;
  8 using System.Windows.Forms;
  9 
 10 namespace WinAutoControl_C
 11 {
 12     public partial class Form1 : Form
 13     {
 14         public Form1()
 15         {
 16             InitializeComponent();
 17         }
 18 
 19         private void Form1_Load(object sender, EventArgs e)
 20         {
 21 
 22         }
 23 
 24         private void 生成_Click(object sender, EventArgs e)
 25         {
 26 
 27             Regex regex = new Regex("^[0-9]*$");
 28             if (!regex.IsMatch(txt_num.Text))
 29             {
 30                 txt_num.BackColor = System.Drawing.Color.Red;
 31                 txt_num.Text = string.Empty;
 32                 MessageBox.Show("输入的值不是数字");
 33                 return;
 34             }
 35             txt_num.BackColor = System.Drawing.Color.White;
 36             int n = int.Parse(txt_num.Text);
 37             int row = 0;
 38             for (int i = 0; i < n; i++)
 39             {
 40                 if (i % 5 == 0 && i != 0)
 41                 {
 42                     row++;
 43                 }
 44                 //参数
 45                 TextBox box = new TextBox();
 46                 box.Name = "arryTextBox" + i.ToString();
 47                 box.Multiline = true;
 48                 box.Text = string.Format("文本框{0}", i + 1);
 49                 box.Size = new Size(129, 124);
 50                 box.Location = new Point(65 + i % 5 * 280, 65 + row * 170);
 51                 panel6.Controls.Add(box);
 52             }
 53         }
 54 
 55         private void button1_Click(object sender, EventArgs e)
 56         {
 57             int row1 = 0;
 58             int c = int.Parse(txt_num.Text);
 59             for (int i = 0; i < c; i++)
 60             {
 61                 if (i % 5 == 0 && i != 0)
 62                 {
 63                     row1++;
 64                 }
 65                 //参数名称
 66                 TextBox box1 = new TextBox();
 67                 box1.Name = "nameTextBox" + i.ToString();
 68                 box1.Multiline = true;
 69                 box1.Text = string.Format("名称{0}", i + 1);
 70                 box1.Size = new Size(115, 25);
 71                 box1.Location = new Point(210 + i % 5 * 280, 65 + row1 * 170/*209 + row *50*/);
 72                 panel6.Controls.Add(box1);
 73 
 74             }
 75         }
 76         private void button2_Click(object sender, EventArgs e)
 77         {
 78             int row2 = 0;
 79 
 80             int d = int.Parse(txt_num.Text);
 81             for (int i = 0; i < d; i++)
 82             {
 83                 if (i % 5 == 0 && i != 0)
 84                 {
 85                     row2++;
 86                 }
 87                 //参数名称
 88                 CheckBox chk = new CheckBox();
 89                 chk.Name = "nameCheckBox" + i.ToString();
 90                 chk.Text = string.Format("Chk{0}", i + 1);
 91                 chk.Size = new Size(100, 20);
 92                 chk.Location = new Point(210 + i % 5 * 280, 170 + row2 * 170 /*210 + row * 50*/);
 93                 panel6.Controls.Add(chk);
 94 
 95             }
 96         }
 97 
 98         private void txt_num_KeyUp(object sender, KeyEventArgs e)
 99         {
100 
101         }
102     }
103 }
View Code

 

效果

 

 重点还是Location的位置的计算,自己慢慢找规律,实在不行就找好,规律慢慢修改调整位置。

参考

 

posted on 2020-12-29 10:21  独行者*  阅读(254)  评论(0编辑  收藏  举报

导航