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

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

时间:2022-11-10作者:未知来源:三度网教程人气:


本站电脑知识提供电脑入门知识,计算机基础知识,计算机网络应用基础知识,电脑配置知识,电脑故障排除和电脑常识大全,帮助您更好的学习电脑!不为别的,只因有共同的爱好,为中国互联网发展出一分力!

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.3du8.cn) .All Rights Reserved 网站地图 友情链接

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

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