• 用户控件、动态创建添加


    使用用户控件:

    项目右键添加,用户控件,选择用户控件cs

    在生成的项目中用设计编辑,点击菜单栏生成,生成解决方案,然后用户控件就可以使用了

    制作简单的qq窗口,根据数据库自动添加数据

    制作用户控件:

    实体类:(qq,haoyou)

    qq:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace QQ.App_Code
    {
        public class qq
        {
            public string qqnumber { get; set; }
            public string password { get; set; }
            public string nickname { get; set; }
            public string content { get; set; }
    
        }
    }

    haoyou:包含两个属性扩展,使显示昵称和个性签名

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.SqlClient;
    
    namespace QQ.App_Code
    {
        public class haoyou
        {
            SqlConnection conn = null;
            SqlCommand cmd = null;
            public haoyou()
            {
                conn = new SqlConnection("server=.;database=lianxi0928;user=sa;pwd=123");
                cmd = conn.CreateCommand();        
            }
    
            public string qqnumber { get; set; }
            public string friendqqnumber { get; set; }
    
            public string fnickname
            {
                get
                {
                    string end = "null";
                    cmd.CommandText = "select*from qq where qqnumber=@a";
                    cmd.Parameters.Clear();
                    cmd.Parameters.AddWithValue("@a",friendqqnumber);
                    conn.Open();
                    SqlDataReader dr = cmd.ExecuteReader();
                    if (dr.HasRows)
                    {
                        dr.Read();
                        end = dr["nickname"].ToString();
                        
                    }
                    conn.Close();
                    return end;
                }       
            }
    
            public string fcontent
            {
                get
                {
                    string end = "null";
                    cmd.CommandText = "select*from qq where qqnumber=@a";
                    cmd.Parameters.Clear();
                    cmd.Parameters.AddWithValue("@a", friendqqnumber);
                    conn.Open();
                    SqlDataReader dr = cmd.ExecuteReader();
                    if (dr.HasRows)
                    {
                        dr.Read();
                        end = dr["content"].ToString();
    
                    }
                    conn.Close();
                    return end;
                }
            }
        }
    }

    数据访问类:haoyoudata

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.SqlClient;
    
    namespace QQ.App_Code
    {
        public class haoyoudata
        {
            SqlConnection conn = null;
            SqlCommand cmd = null;
    
            public haoyoudata()
            {
                conn = new SqlConnection("server=.;database=lianxi0928;user=sa;pwd=123");
                cmd = conn.CreateCommand();       
            }
            public List<haoyou> select(string qqnb)
            {
                List<haoyou> hlist = new List<haoyou>();
                cmd.CommandText = "select*from haoyou  where qqnumber=@a";
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@a",qqnb);
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        haoyou hy = new haoyou();
                        hy.qqnumber = dr["qqnumber"].ToString();
                        hy.friendqqnumber = dr["friendqqnumber"].ToString();
    
                        hlist.Add(hy);
                    }
                }
    
                conn.Close();
                return hlist;
            }    
        }
    }

    form1中:

    using QQ.App_Code;
    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 QQ
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                //将当前登录账号的全部好友信息全部取出来
                List<haoyou> hlist = new haoyoudata().select(label1.Text);
                //动态创建好友用户控件,添加到容器中去
                for (int i = 0; i < hlist.Count; i++)
                {
                    qqhaoyu hy = new qqhaoyu();
                    hy.label1.Text = hlist[i].fnickname;
                    hy.label2.Text = hlist[i].fcontent;
    
                    flowLayoutPanel1.Controls.Add(hy);
                }
    
            }
        }
    }

    生成显示:

  • 相关阅读:
    JAVA动态添加枚举值
    maven仓库配置
    AWS S3 上传下载文件
    golang http client的MaxConnsPerHost限制
    redis cli的特殊用法
    go调度: 第三部分-并发
    tcmalloc的memory heap profiler
    【流数据处理】MySql/PG/Oracle+Kafka+Flink(CDC捕获) 部署及实时计算
    【概念】详解MapReduce原理
    一次失败的创业经历
  • 原文地址:https://www.cnblogs.com/wy1992/p/6165696.html
Copyright © 2020-2023  润新知