C#自定义列表控件2(选中功能列表)

效果展示

少项展示
在这里插入图片描述
多项展示
在这里插入图片描述

功能展示和控件的用法

功能:每个列表项的第三个信息 岗位详情可点击 点击以后可以在点击事件中获取对应项的岗位详情后面的数据 如上图的324

下图是在窗口添加自定义列表控件的代码 和 功能事件的调用
在这里插入图片描述
下面代码是 自定义控件所有代码 复制粘贴可直接使用


    public class MyflowPanel : FlowLayoutPanel
    {
        //自定义一个事件webCall 由列表项的自定义事件触发(当列表项的该事件触发时触发该事件)
        public delegate void NumManipulationHandler(string str);
        public event NumManipulationHandler webCall;
        private void button1_Click(object sender, EventArgs e)
        {
            Label label = (Label)sender;
            MyPanel str = (MyPanel)label.Parent;
            string s = str.Webstr;
            //Console.WriteLine(s);
            if (webCall != null)
            {
                webCall(s);
            }
        }

        //声明最大控件的样式
        public static Size size = new Size(955, 520);
        public static Color color = Color.White ;

        //声明需要的控件
        List<MyPanel> myPanels = new List<MyPanel>();

        //构造函数
        public MyflowPanel(List<string[]> listStr)
        {
            //最大控件的样式定义
            this.Size = size;
            this.BackColor = color;
            this.AutoScroll = true;
            this.AutoSizeMode = AutoSizeMode.GrowAndShrink;

            //用循环将列表项添加上去
            if (listStr != null)
            {
                int ListNum = listStr.Count;
                MyPanel Panels;
                for (int i = 0; i < ListNum; i++)
                {
                    
                    Panels = new MyPanel(listStr[i]);
                    Panels.webCall += button1_Click;
                    myPanels.Add(Panels);
                }
                this.Controls.AddRange(myPanels.ToArray());
            }

        }
    }

    //声明列表项控件
    public class MyPanel : Panel
    {
        //自定义一个事件webCall 由点击列表项的第三个数据 岗位详情的时候触发
        public event EventHandler webCall;
        private void button1_Click(object sender, EventArgs e)
        {
            if (webCall != null)
            {
                webCall(sender, e);
            }
        }

        //定义列表项的第三个数据 岗位详情的鼠标样式 鼠标在上方时候显示字体为黑色 不在上方时为灰色
        private void MyPanel_MouseLeave(object sender, EventArgs e)
        {
            labels[2].ForeColor = Color.FromArgb(160, 160, 160);
        }
        private void MyPanel_MouseEnter(object sender, EventArgs e)
        {
            labels[2].ForeColor = Color.Black;
        }

        //自定义属性 用于点击列表项的第三个数据 岗位详情的时候 返回岗位详情右边的数据
        private string webstr = null;
        public string Webstr
        {
            get { return webstr; }
        }

        //声明需要的控件
        Label[] labels = new Label[3];

        //列表项的构造函数
        public MyPanel(string[] strs)
        {
            //宽度等于外面最大的窗口的宽度减去 滚动条的宽度
            this.Size = new Size(938, 65);
            this.BackColor = Color.White;
            //外边距设置为0意味着 添加到父窗口时候从(0,0)开始 并且全覆盖父控件
            this.Margin = new Padding(0);
            //重写paint事件 为列表项添加灰色的边框
            this.Paint += MyPanel_Paint;
            webstr = strs[2];
            //用循环将控件添加到列表项控件中
            for (int i = 0; i < 3; i++)
            {
                labels[i] = new Label();
                labels[i].AutoSize = true;
                labels[i].Font = new Font("微软雅黑", 12);
                labels[i].ForeColor = Color.FromArgb(160, 160, 160);
                if (i == 0)
                {
                    labels[i].Location = new Point(10, 20);
                    labels[i].Text = "单位名称:" + strs[0];
                }
                if (i ==1)
                {
                    labels[i].Location = new Point(labels[i-1].Location.X + labels[i-1].Width + 50, labels[i-1].Location.Y);
                    labels[i].Text = "单位介绍:" + strs[1];
                }
                if (i == 2)
                {
                    labels[i].Location = new Point(labels[i - 1].Location.X + labels[i - 1].Width + 50, labels[i - 1].Location.Y);
                    labels[i].Text = "岗位详情:" + strs[2];
                    labels[i].Click += button1_Click;
                    labels[i].MouseEnter += MyPanel_MouseEnter;
                    labels[i].MouseLeave += MyPanel_MouseLeave;
                }
                
            }
            this.Controls.AddRange(labels);
        }

        private void MyPanel_Paint(object sender, PaintEventArgs e)
        {
            //画分割线
            Graphics g3 = e.Graphics;								//新建一个画布
            //Color c3 = SystemColors.Control;			//声明一个 颜色
            //Color c3 = Color.Black;
            //Color c3 = Common_Example.themeColor3;
            Color c3 = Color.FromArgb(224, 226, 229);
            Pen p3 = new Pen(c3);									//新建一支画笔
            g3.DrawLine(p3, 0, this.Height - 1, this.Width - 1, this.Height - 1);
            //g3.DrawLine(p3, 0, this.Height - 2, this.Width - 1, this.Height - 2);
            //g3.DrawLine(p3, 0, this.Height - 3, this.Width - 1, this.Height - 3);
            g3.DrawLine(p3, 0, 0, this.Width - 1, 0);
            //g3.DrawLine(p3, 0, 1, this.Width - 1, 1);
            g3.DrawLine(p3, 0, 0, 0, this.Height-1);
            g3.DrawLine(p3, 1, 0, 1, this.Height-1);
            g3.DrawLine(p3, this.Width-1, 0, this.Width - 1, this.Height-1);
            g3.DrawLine(p3, this.Width-2, 0, this.Width - 2, this.Height -1);
        }
    }
    

所有代码

程序的所有代码如下

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;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

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

        private void button1_Click(object sender, EventArgs e)
        {
            List<string[]> vs = new List<string[]>()
            {
                new string[] { "12", "23", "34" },
                new string[] { "12", "33", "394" },
                new string[] { "12", "43", "324" },
                new string[] { "12", "543", "374" },
                new string[] { "12", "23", "34" },
                new string[] { "12", "33", "394" },
                new string[] { "12", "43", "324" },
                new string[] { "12", "543", "374" },
                new string[] { "12", "23", "34" },
                new string[] { "12", "33", "394" },
                new string[] { "12", "43", "324" },
                new string[] { "12", "543", "374" }
            };

            //新建一个自定义的列表控件 添加上去窗口中
            MyflowPanel his_Panel = new MyflowPanel(vs);
            his_Panel.Location = new Point(18, 53);
            this.Controls.Add(his_Panel);
            his_Panel.webCall += His_Panel_webCall;
        }

        private void His_Panel_webCall(string s)
        {
            Console.WriteLine(s);
        }
    }
    

    public class MyflowPanel : FlowLayoutPanel
    {
        //自定义一个事件webCall 由列表项的自定义事件触发(当列表项的该事件触发时触发该事件)
        public delegate void NumManipulationHandler(string str);
        public event NumManipulationHandler webCall;
        private void button1_Click(object sender, EventArgs e)
        {
            Label label = (Label)sender;
            MyPanel str = (MyPanel)label.Parent;
            string s = str.Webstr;
            //Console.WriteLine(s);
            if (webCall != null)
            {
                webCall(s);
            }
        }

        //声明最大控件的样式
        public static Size size = new Size(955, 520);
        public static Color color = Color.White ;

        //声明需要的控件
        List<MyPanel> myPanels = new List<MyPanel>();

        //构造函数
        public MyflowPanel(List<string[]> listStr)
        {
            //最大控件的样式定义
            this.Size = size;
            this.BackColor = color;
            this.AutoScroll = true;
            this.AutoSizeMode = AutoSizeMode.GrowAndShrink;

            //用循环将列表项添加上去
            if (listStr != null)
            {
                int ListNum = listStr.Count;
                MyPanel Panels;
                for (int i = 0; i < ListNum; i++)
                {
                    
                    Panels = new MyPanel(listStr[i]);
                    Panels.webCall += button1_Click;
                    myPanels.Add(Panels);
                }
                this.Controls.AddRange(myPanels.ToArray());
            }

        }
    }

    //声明列表项控件
    public class MyPanel : Panel
    {
        //自定义一个事件webCall 由点击列表项的第三个数据 岗位详情的时候触发
        public event EventHandler webCall;
        private void button1_Click(object sender, EventArgs e)
        {
            if (webCall != null)
            {
                webCall(sender, e);
            }
        }

        //定义列表项的第三个数据 岗位详情的鼠标样式 鼠标在上方时候显示字体为黑色 不在上方时为灰色
        private void MyPanel_MouseLeave(object sender, EventArgs e)
        {
            labels[2].ForeColor = Color.FromArgb(160, 160, 160);
        }
        private void MyPanel_MouseEnter(object sender, EventArgs e)
        {
            labels[2].ForeColor = Color.Black;
        }

        //自定义属性 用于点击列表项的第三个数据 岗位详情的时候 返回岗位详情右边的数据
        private string webstr = null;
        public string Webstr
        {
            get { return webstr; }
        }

        //声明需要的控件
        Label[] labels = new Label[3];

        //列表项的构造函数
        public MyPanel(string[] strs)
        {
            //宽度等于外面最大的窗口的宽度减去 滚动条的宽度
            this.Size = new Size(938, 65);
            this.BackColor = Color.White;
            //外边距设置为0意味着 添加到父窗口时候从(0,0)开始 并且全覆盖父控件
            this.Margin = new Padding(0);
            //重写paint事件 为列表项添加灰色的边框
            this.Paint += MyPanel_Paint;
            webstr = strs[2];
            //用循环将控件添加到列表项控件中
            for (int i = 0; i < 3; i++)
            {
                labels[i] = new Label();
                labels[i].AutoSize = true;
                labels[i].Font = new Font("微软雅黑", 12);
                labels[i].ForeColor = Color.FromArgb(160, 160, 160);
                if (i == 0)
                {
                    labels[i].Location = new Point(10, 20);
                    labels[i].Text = "单位名称:" + strs[0];
                }
                if (i ==1)
                {
                    labels[i].Location = new Point(labels[i-1].Location.X + labels[i-1].Width + 50, labels[i-1].Location.Y);
                    labels[i].Text = "单位介绍:" + strs[1];
                }
                if (i == 2)
                {
                    labels[i].Location = new Point(labels[i - 1].Location.X + labels[i - 1].Width + 50, labels[i - 1].Location.Y);
                    labels[i].Text = "岗位详情:" + strs[2];
                    labels[i].Click += button1_Click;
                    labels[i].MouseEnter += MyPanel_MouseEnter;
                    labels[i].MouseLeave += MyPanel_MouseLeave;
                }
                
            }
            this.Controls.AddRange(labels);
        }

        private void MyPanel_Paint(object sender, PaintEventArgs e)
        {
            //画分割线
            Graphics g3 = e.Graphics;								//新建一个画布
            //Color c3 = SystemColors.Control;			//声明一个 颜色
            //Color c3 = Color.Black;
            //Color c3 = Common_Example.themeColor3;
            Color c3 = Color.FromArgb(224, 226, 229);
            Pen p3 = new Pen(c3);									//新建一支画笔
            g3.DrawLine(p3, 0, this.Height - 1, this.Width - 1, this.Height - 1);
            //g3.DrawLine(p3, 0, this.Height - 2, this.Width - 1, this.Height - 2);
            //g3.DrawLine(p3, 0, this.Height - 3, this.Width - 1, this.Height - 3);
            g3.DrawLine(p3, 0, 0, this.Width - 1, 0);
            //g3.DrawLine(p3, 0, 1, this.Width - 1, 1);
            g3.DrawLine(p3, 0, 0, 0, this.Height-1);
            g3.DrawLine(p3, 1, 0, 1, this.Height-1);
            g3.DrawLine(p3, this.Width-1, 0, this.Width - 1, this.Height-1);
            g3.DrawLine(p3, this.Width-2, 0, this.Width - 2, this.Height -1);
        }
    }
    

}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值