盘绰网教程:是一个免费提供流行视频软件教程、在线学习分享的学习平台!

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

时间:2023/6/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);
                    }
                }
            }              
            }   
        }   
    }    
}

 


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

关键词:  c#  动态生成控件代码  c#  动态生成Label与TextBox





Copyright © 2012-2018 盘绰网教程(http://www.panchuo.com) .All Rights Reserved 网站地图 友情链接

免责声明:本站资源均来自互联网收集 如有侵犯到您利益的地方请及时联系管理删除,敬请见谅!

QQ:1006262270   邮箱:kfyvi376850063@126.com   手机版