自定义控件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class TestControl : UserControl
    {
        private string UserName="";
        public TestControl()
        {
            InitializeComponent();
        }
        public TestControl(string uid)
        {
            InitializeComponent();
            UserName = uid;
        }

        private void TestControl_Load(object sender, EventArgs e)
        {
            YongHuDA da = new YongHuDA();
            YongHu data = da.Select(UserName);

            pictureBox1.BackgroundImage = Image.FromFile(data.Pic);
            pictureBox1.BackgroundImageLayout = ImageLayout.Zoom;

            label1.Text = data.Nick;
            label2.Text = data.Qian;

            button1.Tag = data.Uid;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            //label1.Text;
            this.ParentForm.Controls["label1"].Text = label1.Text;

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

       



    }
}

  

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            YongHuDA da = new YongHuDA();
            List<YongHu> list = da.Select();

            foreach (YongHu data in list)
            {
                TestControl ctrl = new TestControl(data.Uid);
                flowLayoutPanel1.Controls.Add(ctrl);
            }
        }
    }
}
原文地址:https://www.cnblogs.com/kun-boke/p/5885531.html