效果展示
分析结构
由结构图知道
外面应该有一个大的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);
}
}
}