首页/电脑学习/内容

c# 动态生成控件代码 c# 动态生成Label与TextBox

电脑学习2023-06-10 阅读()
[摘要]c# 动态生成控件 c# 动态生成Label和TextBox using System;using System.Collections.Generic;using System.Collectio...

c# 动态生成控件 c# 动态生成Label和TextBox

 

using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace testTextBox
{
    public partial class Form1 : Form
    {
     public Form1()
        {
            InitializeComponent();
        }

        public SqlConnection getcon()
        {

            SqlConnection myCon = new SqlConnection("server=.;database=master;uid=sa;pwd=123");
            return myCon;

        }
        public void getcom(string sqlstr)
        {
            SqlConnection sqlcon = this.getcon();
            sqlcon.Open();
            SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);
            sqlcom.ExecuteNonQuery();
            sqlcom.Dispose();
            sqlcon.Close();
            sqlcon.Dispose();
        }
        
        public DataSet getds(string sqlstr, string strtable)
        {
            SqlConnection sqlcon = this.getcon();
            SqlDataAdapter sqlda = new SqlDataAdapter(sqlstr, sqlcon);
            DataSet myds = new DataSet();
            sqlda.Fill(myds, strtable);
            return myds;
        }

        public int getCount(string sqlstr)
        {
            SqlConnection con = this.getcon();
            con.Open();
            SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlstr, con);
            DataTable result = new DataTable();
            dataAdapter.Fill(result);
            int num = result.Rows.Count;
            return num;
        } 
        private void Form1_Load(object sender, EventArgs e)
        {
            int num= this.getCount("Select Name from test where ID=1");
            DataSet ds = this.getds("select Name from test where ID=1", "test");
            for (int i = 0; i< num; i++)
            {
                Label lbl = new Label();
                lbl.Location = new Point(50 + 100 * i, 50);
                lbl.Size = new Size(80, 16);
                lbl.Visible = true;
                string lblText= ds.Tables[0].Rows[i][0].ToString();
                lbl.Text =lblText;
                Controls.Add(lbl);

            }
            for (int i = 0; i < num; i++)
            {
                TextBox txtbox = new TextBox();
                txtbox.Location = new Point(50 + 100 * i, 80);
                txtbox.Size = new Size(80, 16);
                txtbox.Name = "txt" + i.ToString();
                Controls.Add(txtbox);
            }

        }
        private void button1_Click(object sender, EventArgs e)
        {
            string txt;
            int n = this.getCount("Select Name from test where ID=1");
            DataSet ds = this.getds("select Name from test where ID=1", "test");
            for (int i = 0; i < n; i++)
            {
            string txtname = "txt" + i.ToString();
            foreach (Control ctl in Controls)
            {
                if (ctl.GetType().Name == "TextBox")
                {
                    if (ctl.Name ==txtname)
                    {
                        txt=((TextBox)ctl).Text;
                        string strCommand = "update test set Val='";
                        strCommand += txt + "'";
                        strCommand += "where ID=1 and Name='" + ds.Tables[0].Rows[i][0].ToString() + "'";
                        this.getcom(strCommand);
                    }
                }
            }              
            }   
        }   
    }    
}

 


学习教程快速掌握从入门到精通的电脑知识



……

相关阅读