C#自定义列表控件1(搜索功能列表)

效果展示

在这里插入图片描述

分析结构

在这里插入图片描述
由结构图知道
外面应该有一个大的panel
然后里上方是一个小的panel
下方是一个flowlayoutpanel

所有代码

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;
using System.IO;
using System.Text.RegularExpressions;
using System.Net.Http;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            //this.BackColor = Color.White;
            //this.BackColor = Color.FromArgb(224, 226, 229);


            //给窗体设置颜色
            this.BackColor = SystemColors.Control;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            List<string[]> vs = new List<string[]>()
            {
                new string[] { "12", "23", "34", "45", "56" },
                new string[] { "12", "33", "34", "45", "56" },
                new string[] { "12", "43", "34", "45", "56" },
                new string[] { "12", "543", "34", "45", "56" },
                new string[] { "12", "2342", "34", "45", "56" },
                new string[] { "12", "2213", "34", "45", "56" },
                new string[] { "12", "2322", "34", "45", "56" },
                new string[] { "12", "2333", "34", "45", "56" },
                new string[] { "12", "2663", "34", "45", "56" },
                new string[] { "12", "293", "34", "45", "56" },
                new string[] { "12", "2113", "34", "45", "56" },
                new string[] { "12", "23", "34", "45", "56" },
            };
            
            //新建一个自定义的列表控件 添加上去窗口中
            his_Mypanel his_Panel = new his_Mypanel(vs);
            his_Panel.Location = new Point(150, 50);
            this.Controls.Add(his_Panel);

        }

    }
	//his_Mypanel就是自定义的控件
    public class his_Mypanel: Panel
    {

        //最大的panel所需要的控件
        Panel Panel1 = new Panel();
        TextBox TextBox1 = new TextBox();
        Label label1 = new Label();
        his_Flowpanel his_Flowpanel1;

        //初始化最大的panel
        public his_Mypanel(List<string[]> names)
        {
            this.AutoSize = true;
            this.MaximumSize = new Size(1000, 600);
            this.BackColor = SystemColors.Control;


            ///初始化上方的搜索框的panel 开始
            Panel1.Width = 998;
            Panel1.Height = 80;
            Panel1.BackColor = Color.White;
            Panel1.Location = new Point(1, 1);
            this.Controls.Add(Panel1);
            Panel1.BackColor = SystemColors.Control;

            TextBox1.Height = 28;
            TextBox1.Width = 700;
            
            TextBox1.Location = new Point(120, 1);
            TextBox1.Font = new Font("微软雅黑", 12);
            TextBox1.TextChanged += TextBox1_TextChanged;


            label1.AutoSize = false;
            label1.Size = new Size(50, 24);
            label1.Text = "查询";
            label1.BackColor = Color.White;
            label1.Font = new Font("微软雅黑", 10);
            label1.ForeColor = Color.Black;
            label1.TextAlign = ContentAlignment.MiddleCenter;
            label1.Click += Button1_Click;
            label1.Location = new Point(TextBox1.Location.X + TextBox1.Width - label1.Width-2, TextBox1.Location.Y +2);

            Panel1.Controls.Add(TextBox1);
            Panel1.Controls.Add(label1);
            label1.BringToFront();
            ///初始化上方的搜索框的panel 结束




            ///初始化下方的flowpanel 开始
            his_Flowpanel1 = new his_Flowpanel(names);
            his_Flowpanel1.Location = new Point(Panel1.Location.X, Panel1.Location.Y + Panel1.Height + 1);
            this.Controls.Add(his_Flowpanel1);
            ///初始化下方的flowpanel 结束
            

        }

        private void TextBox1_TextChanged(object sender, EventArgs e)
        {
            his_Flowpanel1.selectHide(TextBox1.Text.ToString());
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            his_Flowpanel1.selectHide(TextBox1.Text.ToString());
        }
    }

    public class his_Flowpanel : FlowLayoutPanel
    {
        //该函数用于获取两个字符串的相同字符的数量
        public decimal GetSimilarityWith(string sourceString, string str)
        {
            //decimal Kq = 2;
            //decimal Kr = 1;
            //decimal Ks = 1;
            char[] ss = sourceString.ToCharArray();
            char[] st = str.ToCharArray();

            //获取交集数量
            int q = ss.Intersect(st).Count();
            //int s = ss.Length - q;
            //int r = st.Length - q;

            //return Kq * q / (Kq * q + Kr * r + Ks * s);
            return q;
        }

        //点击事件 
        public void selectHide(string str)
        {
            List<his_tip> his_Tips_Sele = new List<his_tip>();
            int lenght = his_Tips.Count;
            if (str.Trim() != "")
            {
                this.Controls.Clear();
                for (int i = 0; i < lenght; i++)
                {
                    //当重复字符的数量大于0那么就隐藏
                    if (GetSimilarityWith(str, his_Tips[i].labels[1].Text.ToString()) >0)
                    {
                        //his_Tips[i].Hide();
                        his_Tips_Sele.Add(his_Tips[i]);
                    }
                    //if (his_Tips[i].labels[1].Text.ToString() == str)
                    //{
                    //    his_Tips[i].Hide();
                    //}
                }
                this.Controls.AddRange(his_Tips_Sele.ToArray());
            }
            else
            {
                this.Controls.Clear();
                this.Controls.AddRange(his_Tips.ToArray());
            }
       
        }
        List<his_tip> his_Tips = new List<his_tip>();
        public his_Flowpanel(List<string[]> names)
        {
            this.AutoSize = true;
            this.BackColor = SystemColors.Control;
            this.AutoScroll = true;
            this.MaximumSize = new Size(998, 535);
            this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            
            this.Margin = new Padding(3);


            his_tip his_Tip;
            for (int i = 0; i < names.Count; i++)
            {
                his_Tip = new his_tip(names[i]);
                his_Tips.Add(his_Tip);
            }
            this.Controls.AddRange(his_Tips.ToArray());
            
        }
    }

    public class his_tip : Panel
    {
        public Label[] labels = new Label[5];
        public his_tip(string[] name)
        {
            this.Width = 998;
            this.Height = 110;
            this.BackColor = Color.White;
            this.Margin = new Padding(0, 0, 0, 0);
            this.Paint += His_tip_Paint;

            for (int i = 0; i < 5; i++)
            {
                labels[i] = new Label();

                labels[i].AutoSize = true;
                labels[i].Font = new Font("微软雅黑", 10);
                labels[i].ForeColor = Color.FromArgb(147, 150, 153);
                if (i == 0)
                {
                    labels[i].Text = name[0];
                    labels[i].Location = new Point(10, 10);
                    
                }
                if (i == 1)
                {
                    labels[i].Text = name[1];
                    labels[i].Location = new Point(labels[i-1].Location.X - 2, labels[i-1].Location.Y + 30);
                    labels[i].ForeColor = Color.Black;
                    labels[i].Font = new Font("微软雅黑", 16);
                }
                if (i == 2)
                {
                    labels[i].Text = name[2];
                    labels[i].Location = new Point(labels[0].Location.X, labels[i - 1].Location.Y + labels[i - 1].Height + 10);
                   

                }
                if (i == 3)
                {
                    labels[i].Text = name[3];
                    labels[i].Location = new Point(labels[i - 1].Location.X + labels[i - 1].Width + 15, labels[i - 1].Location.Y);
                  
                }
                if (i == 4)
                {
                    labels[i].Text = name[4];
                    labels[i].Location = new Point(labels[i - 1].Location.X + labels[i-1].Width + 15, labels[i - 1].Location.Y);
                  
                }
                this.Controls.Add(labels[i]);
            }

        }

        private void His_tip_Paint(object sender, PaintEventArgs e)
        {
            //画分割线
            Graphics g3 = e.Graphics;								//新建一个画布
            Color c3 = SystemColors.Control;			//声明一个 颜色
            //Color c3 = Common_Example.themeColor3;
            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);
        }
    }

}

QQ好友例表控件 带实例和源码 //1. 属性列表: // SelectionMode 组件中条目的选择类型,即多选(Multiple)、单选(Single) // Rows 列表框中显示总共多少行 // Selected 检测条目是否被选中 // SelectedItem 返回的类型是ListItem,获得列表框中被选择的条目 // Count 列表框中条目的总数 // SelectedIndex 列表框中被选择项的索引值 // Items 泛指列表框中的所有项,每一项的类型都是ListItem //2. 取列表框中被选中的值 // ListBox.SelectedValue //3. 动态的添加列表框中的项: // ListBox.Items.Add("所要添加的项"); //4. 移出指定项: // //首先判断列表框中的项是否大于0 // If(ListBox.Items.Count > 0 ) // { ////移出选择的项 //ListBox.Items.Remove(ListBox.SelectedItem); // } //5. 清空所有项: // //首先判断列表框中的项是否大于0 // If(ListBox.Items.Count > 0 ) // { ////清空所有项 //ListBox.Items.Clear(); // } //6. 列表框可以一次选择多项: // 只需设置列表框的属性 SelectionMode="Multiple",按Ctrl可以多选 //7. 两个列表框联动,即两级联动菜单 // //判断第一个列表框中被选中的值 // switch(ListBox1.SelectValue) // { ////如果是"A",第二个列表框中就添加这些: //case "A" // ListBox2.Items.Clear(); // ListBox2.Items.Add("A1"); // ListBox2.Items.Add("A2"); // ListBox2.Items.Add("A3"); ////如果是"B",第二个列表框中就添加这些: //case "B" // ListBox2.Items.Clear(); // ListBox2.Items.Add("B1"); // ListBox2.Items.Add("B2"); // ListBox2.Items.Add("B3"); // } //8. 实现列表框中项的移位 // 即:向上移位、向下移位 // 具体的思路为:创建一个ListBox对象,并把要移位的项先暂放在这个对象中。 // 如果是向上移位,就是把当前选定项的的上一项的值赋给当前选定的项,然后 // 把刚才新加入的对象的值,再附给当前选定项的前一项。 // 具体代码为: // //定义一个变量,作移位用 // index = -1; // //将当前条目的文本以及值都保存到一个临时变量里面 // ListItem lt=new ListItem (ListBox.SelectedItem.Text,ListBox.SelectedValue); // //被选中的项的值等于上一条或下一条的值 // ListBox.Items[ListBox.SelectedIndex].Text=ListBox.Items[ListBox.SelectedIndex + index].Text; // //被选中的项的值等于上一条或下一条的值 // ListBox.Items[ListBox.SelectedIndex].Value=ListBox.Items[ListBox.SelectedIndex + index].Value; // //把被选中项的前一条或下一条的值用临时变量中的取代 // ListBox.Items[ListBox.SelectedIndex].Test=lt.Test; // //把被选中项的前一条或下一条的值用临时变量中的取代 // ListBox.Items[ListBox.SelectedIndex].Value=lt.Value; // //把鼠标指针放到移动后的那项上 // ListBox.Items[ListBox.SelectedIndex].Value=lt.Value; //9. 移动指针到指定位置: // (1).移至首条 // //将被选中项的索引设置为0就OK了 // ListBox.SelectIndex=0; // (2).移至尾条 // //将被选中项的索引设置为ListBox.Items.Count-1就OK了 // ListBox.SelectIndex=ListBox.Items.Count-1; // (3).上一条 // //用当前被选中的索引去减 1 // ListBox.SelectIndex=ListBox.SelectIndex - 1; // (4).下一条 // //用当前被选中的索引去加 1 // ListBox.SelectIndex=ListBox.SelectIndex + 1; //this.ListBox1.Items.Insertat(3,new ListItem("插入在第3行之后项","")); //this.ListBox1.Items.Insertat(index,ListItem) //ListBox1.Items.Insert(0,new ListItem("text","value"));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值