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


    使用用户控件:

    项目右键添加,用户控件,选择用户控件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);
                }
    
            }
        }
    }

    生成显示:

  • 相关阅读:
    按照指定的字符串拆分字符串,split()方法。
    charAt()取出指定位置的字符 .length()得到一个字符串的长度 indexOf()查找一个指定的字符是否存在并返回其位置 trim()去掉字符串的左右空格 substring()字符串的截取 str.replace(" ", ""); 去掉所有空格,包括首尾、中间
    字符串与字符数组的多种转换方式。
    匿名对象。
    构造方法。
    递归的练习,1.统计文件夹大小 2.删除文件夹及文件夹下的文件
    jquery零散小饼干
    jQuery review
    git解决冲突
    url、href、src
  • 原文地址:https://www.cnblogs.com/wy1992/p/6165696.html
Copyright © 2020-2023  润新知