官网

http://www.hzhcontrols.com

前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

GitHub:https://github.com/kwwwvagaa/NetWinformControl

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

如果觉得写的还行,请点个 star 支持一下吧

欢迎前来交流探讨: 企鹅群568015492 

idkey=6e08741ef16fe53bf0314c1c9e336c4f626047943a8b76bac062361bab6b4f8d">

目录

https://www.cnblogs.com/bfyx/p/11364884.html

准备工作

梳理一下需求,我们需要一个横向的节点列表控件,可以进行左右翻页

根据上面所写的需求,我们需要分为2步操作,1:创建项控件,2:创建列表控件

开始

首先我们创建项控件,添加一个用户控件,命名UCHorizontalListItem

代码量并不多,我们看下完整代码

 // 版权所有  黄正辉  交流群:568015492   QQ:623128629
// 文件名称:UCHorizontalListItem.cs
// 创建日期:2019-08-15 16:01:13
// 功能描述:HorizontalList
// 项目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace HZH_Controls.Controls
{
[ToolboxItem(false)]
public partial class UCHorizontalListItem : UserControl
{
public event EventHandler SelectedItem;
private KeyValuePair<string, string> _DataSource = new KeyValuePair<string, string>();
public KeyValuePair<string, string> DataSource
{
get { return _DataSource; }
set
{
_DataSource = value;
int intWidth = ControlHelper.GetStringWidth(value.Value, lblTitle.CreateGraphics(), lblTitle.Font);
if (intWidth < )
intWidth = ;
this.Width = intWidth + ;
lblTitle.Text = value.Value;
SetSelect(false);
}
}
public UCHorizontalListItem()
{
InitializeComponent();
this.Dock = DockStyle.Right;
this.MouseDown += Item_MouseDown;
this.lblTitle.MouseDown += Item_MouseDown;
this.ucSplitLine_H1.MouseDown += Item_MouseDown;
} void Item_MouseDown(object sender, MouseEventArgs e)
{
if (SelectedItem != null)
SelectedItem(this, e);
} public void SetSelect(bool bln)
{
if (bln)
{
lblTitle.ForeColor = Color.FromArgb(, , );
ucSplitLine_H1.Visible = true;
this.lblTitle.Padding = new Padding(, , , );
}
else
{
lblTitle.ForeColor = Color.FromArgb(, , );
ucSplitLine_H1.Visible = false;
this.lblTitle.Padding = new Padding(, , , );
}
}
}
}
 namespace HZH_Controls.Controls
{
partial class UCHorizontalListItem
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region 组件设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.lblTitle = new System.Windows.Forms.Label();
this.ucSplitLine_H1 = new HZH_Controls.Controls.UCSplitLine_H();
this.SuspendLayout();
//
// lblTitle
//
this.lblTitle.Dock = System.Windows.Forms.DockStyle.Fill;
this.lblTitle.Font = new System.Drawing.Font("微软雅黑", 10F);
this.lblTitle.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.lblTitle.Location = new System.Drawing.Point(, );
this.lblTitle.Name = "lblTitle";
this.lblTitle.Padding = new System.Windows.Forms.Padding(, , , );
this.lblTitle.Size = new System.Drawing.Size(, );
this.lblTitle.TabIndex = ;
this.lblTitle.Text = "分类名称\r\n分类名称";
this.lblTitle.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// ucSplitLine_H1
//
this.ucSplitLine_H1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.ucSplitLine_H1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.ucSplitLine_H1.Location = new System.Drawing.Point(, );
this.ucSplitLine_H1.Name = "ucSplitLine_H1";
this.ucSplitLine_H1.Size = new System.Drawing.Size(, );
this.ucSplitLine_H1.TabIndex = ;
this.ucSplitLine_H1.TabStop = false;
//
// UCHorizontalListItem
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.Controls.Add(this.lblTitle);
this.Controls.Add(this.ucSplitLine_H1);
this.Name = "UCHorizontalListItem";
this.Padding = new System.Windows.Forms.Padding(, , , );
this.Size = new System.Drawing.Size(, );
this.ResumeLayout(false); } #endregion private UCSplitLine_H ucSplitLine_H1;
private System.Windows.Forms.Label lblTitle;
}
}

设计效果如图

接着我们来创建列表控件,添加一个用户控件,命名UCHorizontalList

我们看下需要提供哪些属性

public UCHorizontalListItem SelectedItem { get; set; }
public event EventHandler SelectedItemEvent;
private int m_startItemIndex = ;
private bool isAutoSelectFirst = true; public bool IsAutoSelectFirst
{
get { return isAutoSelectFirst; }
set { isAutoSelectFirst = value; }
} private List<KeyValuePair<string, string>> dataSource = null; public List<KeyValuePair<string, string>> DataSource
{
get { return dataSource; }
set
{
dataSource = value;
ReloadSource();
}
}

我们有时需要刷新列表

  public void ReloadSource()
{
try
{
ControlHelper.FreezeControl(this, true);
this.panList.SuspendLayout();
this.panList.Controls.Clear();
this.panList.Width = this.panMain.Width;
if (DataSource != null)
{
foreach (var item in DataSource)
{
UCHorizontalListItem uc = new UCHorizontalListItem();
uc.DataSource = item;
uc.SelectedItem += uc_SelectItem;
this.panList.Controls.Add(uc);
}
}
this.panList.ResumeLayout(true);
if (this.panList.Controls.Count > )
this.panList.Width = panMain.Width + this.panList.Controls[].Location.X * -;
this.panList.Location = new Point(, );
m_startItemIndex = ;
if (this.panList.Width > panMain.Width)
panRight.Visible = true;
else
panRight.Visible = false;
panLeft.Visible = false;
panList.SendToBack();
panRight.SendToBack();
if (isAutoSelectFirst && DataSource != null && DataSource.Count > )
{
SelectItem((UCHorizontalListItem)this.panList.Controls[]);
}
}
finally
{
ControlHelper.FreezeControl(this, false);
}
}

还需要处理选中处理

  private void SelectItem(UCHorizontalListItem item)
{
if (SelectedItem != null && !SelectedItem.IsDisposed)
SelectedItem.SetSelect(false);
SelectedItem = item;
SelectedItem.SetSelect(true);
if (SelectedItemEvent != null)
SelectedItemEvent(item, null);
}

然后就是翻页功能的处理了

  private void panLeft_MouseDown(object sender, MouseEventArgs e)
{
if (this.panList.Location.X >= )
{
this.panList.Location = new Point(, );
return;
} for (int i = m_startItemIndex; i >= ; i--)
{
if (this.panList.Controls[i].Location.X < this.panList.Controls[m_startItemIndex].Location.X - panMain.Width)
{
m_startItemIndex = i + ;
break; ;
}
if (i == )
{
m_startItemIndex = ;
}
} ResetListLocation();
panRight.Visible = true;
if (this.panList.Location.X >= )
{
panLeft.Visible = false;
}
else
{
panLeft.Visible = true;
}
panList.SendToBack();
panRight.SendToBack();
} private void panRight_MouseDown(object sender, MouseEventArgs e)
{
if (this.panList.Location.X + this.panList.Width <= this.panMain.Width)
return;
if (this.panList.Controls.Count <= )
return;
for (int i = m_startItemIndex; i < this.panList.Controls.Count; i++)
{
if (this.panList.Location.X + this.panList.Controls[i].Location.X + this.panList.Controls[i].Width > panMain.Width)
{
m_startItemIndex = i;
break;
}
}
ResetListLocation();
panLeft.Visible = true;
if (panList.Width + panList.Location.X <= panMain.Width)
panRight.Visible = false;
else
panRight.Visible = true;
panList.SendToBack();
panRight.SendToBack();
} private void ResetListLocation()
{
if (this.panList.Controls.Count > )
{
this.panList.Location = new Point(this.panList.Controls[m_startItemIndex].Location.X * -, );
}
}

最后向外暴露一个设置选中的功能

 public void SetSelect(string strKey)
{
foreach (UCHorizontalListItem item in this.panList.Controls)
{
if (item.DataSource.Key == strKey)
{
SelectItem(item);
return;
}
}
}

以上就是主要东西了,再看下完整代码

 // 版权所有  黄正辉  交流群:568015492   QQ:623128629
// 文件名称:UCHorizontalList.cs
// 创建日期:2019-08-15 16:01:06
// 功能描述:HorizontalList
// 项目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace HZH_Controls.Controls
{
public partial class UCHorizontalList : UserControl
{
public UCHorizontalListItem SelectedItem { get; set; }
public event EventHandler SelectedItemEvent;
private int m_startItemIndex = ;
private bool isAutoSelectFirst = true; public bool IsAutoSelectFirst
{
get { return isAutoSelectFirst; }
set { isAutoSelectFirst = value; }
} private List<KeyValuePair<string, string>> dataSource = null; public List<KeyValuePair<string, string>> DataSource
{
get { return dataSource; }
set
{
dataSource = value;
ReloadSource();
}
} public UCHorizontalList()
{
InitializeComponent();
} public void ReloadSource()
{
try
{
ControlHelper.FreezeControl(this, true);
this.panList.SuspendLayout();
this.panList.Controls.Clear();
this.panList.Width = this.panMain.Width;
if (DataSource != null)
{
foreach (var item in DataSource)
{
UCHorizontalListItem uc = new UCHorizontalListItem();
uc.DataSource = item;
uc.SelectedItem += uc_SelectItem;
this.panList.Controls.Add(uc);
}
}
this.panList.ResumeLayout(true);
if (this.panList.Controls.Count > )
this.panList.Width = panMain.Width + this.panList.Controls[].Location.X * -;
this.panList.Location = new Point(, );
m_startItemIndex = ;
if (this.panList.Width > panMain.Width)
panRight.Visible = true;
else
panRight.Visible = false;
panLeft.Visible = false;
panList.SendToBack();
panRight.SendToBack();
if (isAutoSelectFirst && DataSource != null && DataSource.Count > )
{
SelectItem((UCHorizontalListItem)this.panList.Controls[]);
}
}
finally
{
ControlHelper.FreezeControl(this, false);
}
} void uc_SelectItem(object sender, EventArgs e)
{
SelectItem(sender as UCHorizontalListItem);
} private void SelectItem(UCHorizontalListItem item)
{
if (SelectedItem != null && !SelectedItem.IsDisposed)
SelectedItem.SetSelect(false);
SelectedItem = item;
SelectedItem.SetSelect(true);
if (SelectedItemEvent != null)
SelectedItemEvent(item, null);
} private void panLeft_MouseDown(object sender, MouseEventArgs e)
{
if (this.panList.Location.X >= )
{
this.panList.Location = new Point(, );
return;
} for (int i = m_startItemIndex; i >= ; i--)
{
if (this.panList.Controls[i].Location.X < this.panList.Controls[m_startItemIndex].Location.X - panMain.Width)
{
m_startItemIndex = i + ;
break; ;
}
if (i == )
{
m_startItemIndex = ;
}
} ResetListLocation();
panRight.Visible = true;
if (this.panList.Location.X >= )
{
panLeft.Visible = false;
}
else
{
panLeft.Visible = true;
}
panList.SendToBack();
panRight.SendToBack();
} private void panRight_MouseDown(object sender, MouseEventArgs e)
{
if (this.panList.Location.X + this.panList.Width <= this.panMain.Width)
return;
if (this.panList.Controls.Count <= )
return;
for (int i = m_startItemIndex; i < this.panList.Controls.Count; i++)
{
if (this.panList.Location.X + this.panList.Controls[i].Location.X + this.panList.Controls[i].Width > panMain.Width)
{
m_startItemIndex = i;
break;
}
}
ResetListLocation();
panLeft.Visible = true;
if (panList.Width + panList.Location.X <= panMain.Width)
panRight.Visible = false;
else
panRight.Visible = true;
panList.SendToBack();
panRight.SendToBack();
} private void ResetListLocation()
{
if (this.panList.Controls.Count > )
{
this.panList.Location = new Point(this.panList.Controls[m_startItemIndex].Location.X * -, );
}
} public void SetSelect(string strKey)
{
foreach (UCHorizontalListItem item in this.panList.Controls)
{
if (item.DataSource.Key == strKey)
{
SelectItem(item);
return;
}
}
}
}
}
 namespace HZH_Controls.Controls
{
partial class UCHorizontalList
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region 组件设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.panMain = new System.Windows.Forms.Panel();
this.panList = new System.Windows.Forms.Panel();
this.panRight = new System.Windows.Forms.Panel();
this.panLeft = new System.Windows.Forms.Panel();
this.panMain.SuspendLayout();
this.SuspendLayout();
//
// panMain
//
this.panMain.Controls.Add(this.panList);
this.panMain.Dock = System.Windows.Forms.DockStyle.Fill;
this.panMain.Location = new System.Drawing.Point(, );
this.panMain.Name = "panMain";
this.panMain.Size = new System.Drawing.Size(, );
this.panMain.TabIndex = ;
//
// panList
//
this.panList.Anchor = System.Windows.Forms.AnchorStyles.Left;
this.panList.BackColor = System.Drawing.Color.Transparent;
this.panList.Location = new System.Drawing.Point(, );
this.panList.Name = "panList";
this.panList.Size = new System.Drawing.Size(, );
this.panList.TabIndex = ;
//
// panRight
//
this.panRight.BackgroundImage = global::HZH_Controls.Properties.Resources.chevron_right;
this.panRight.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
this.panRight.Dock = System.Windows.Forms.DockStyle.Right;
this.panRight.Location = new System.Drawing.Point(, );
this.panRight.Name = "panRight";
this.panRight.Size = new System.Drawing.Size(, );
this.panRight.TabIndex = ;
this.panRight.Visible = false;
this.panRight.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panRight_MouseDown);
//
// panLeft
//
this.panLeft.BackgroundImage = global::HZH_Controls.Properties.Resources.chevron_left;
this.panLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
this.panLeft.Dock = System.Windows.Forms.DockStyle.Left;
this.panLeft.Location = new System.Drawing.Point(, );
this.panLeft.Name = "panLeft";
this.panLeft.Size = new System.Drawing.Size(, );
this.panLeft.TabIndex = ;
this.panLeft.Visible = false;
this.panLeft.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panLeft_MouseDown);
//
// UCHorizontalList
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.Controls.Add(this.panMain);
this.Controls.Add(this.panRight);
this.Controls.Add(this.panLeft);
this.Name = "UCHorizontalList";
this.Size = new System.Drawing.Size(, );
this.panMain.ResumeLayout(false);
this.ResumeLayout(false); } #endregion private System.Windows.Forms.Panel panLeft;
private System.Windows.Forms.Panel panRight;
private System.Windows.Forms.Panel panMain;
private System.Windows.Forms.Panel panList;
}
}

用处及效果

用处:一般用着需要横向切换选项的地方,比如省份切换等

效果:

调用示例

  List<KeyValuePair<string, string>> lstHL = new List<KeyValuePair<string, string>>();
for (int i = ; i < ; i++)
{
lstHL.Add(new KeyValuePair<string, string>(i.ToString(), "选项" + i));
} this.ucHorizontalList1.DataSource = lstHL;

最后的话

如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星 星吧

(十)c#Winform自定义控件-横向列表的更多相关文章

  1. c#Winform自定义控件-目录

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  2. winform 自定义控件(高手)

    高手推荐:https://www.cnblogs.com/bfyx/p/11364884.html   c#Winform自定义控件-目录   前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件 ...

  3. (三十二)c#Winform自定义控件-表格

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  4. (七十九)c#Winform自定义控件-导航菜单

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  5. (八十二)c#Winform自定义控件-穿梭框

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  6. (三十)c#Winform自定义控件-文本框(三)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  7. (二十五)c#Winform自定义控件-有确定取消的窗体(一)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  8. (二十六)c#Winform自定义控件-有确定取消的窗体(二)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  9. (四十九)c#Winform自定义控件-下拉框(表格)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

随机推荐

  1. Java学习笔记之---构造方法

    Java学习笔记之---构造方法 (一)构造方法的特性 构造方法不能被对象单独调用 构造方法与类同名且没有返回值 构造方法只能在对象实例化的时候被调用 当没有指定构造方法时,系统会自动添加无参的构造方 ...

  2. Jenkins使用总结,2.0 新时代:从 CI 到 CD

    Jenkins近阶段使用的总结篇,只写了个引子,却一直未动手写完,今天补上. 前几篇文章提到在内网jenkins直接构建部署升级线上环境,job都是暴露在外面,很容易被误操作,需要做简单的权限控制,以 ...

  3. 上传文件不落地转Base64字符串

    1. 问题描述 因需调用第三方公司的图像识别接口,入参是:证件类型.图像类型.图片base64字符串,采用http+json格式调用. 本来采用的方式是:前端对图片做base64处理,后端组装下直接调 ...

  4. CF1194D 1-2-K Game (博弈论)

    CF1194D 1-2-K Game 一道简单的博弈论题 首先让我们考虑没有k的情况: 1. (n mod 3 =0) 因为n可以被分解成若干个3相加 而每个3可以被分解为1+2或2+1 所以无论A出 ...

  5. docker学习笔记-简介

    零.什么是Docker 是一个基于GO语言开发的开源 应用容器: 开发者可以打包应用和相关包,到一个 轻量级 . 可移植 的 容器 中,并且可以发布到 任何机器 ,实现 虚拟化: 完全使用 沙箱机制, ...

  6. 个人永久性免费-Excel催化剂功能第21波-Excel与Sqlserver零门槛交互-执行SQL语句篇

    在前两波中,已完成了Excel与Sqlserver的查询和上传功能,但难免许多临时的或更深入地操作数据库需要用Sql语句来操作,对一般用户电脑里,不可能有条件轻易安装一个数据库客户端软件,就算安装了对 ...

  7. 2019牛客多校第一场A-Equivalent Prefixes

    Equivalent Prefixes 传送门 解题思路 先用单调栈求出两个序列中每一个数左边第一个小于自己的数的下标, 存入a[], b[].然后按照1~n的顺序循环,比较 a[i]和b[i]是否相 ...

  8. Python基础之格式化输出、运算符、数字与布尔值互换以及while...else

    python是一天学一点,就这样零零碎碎…… 格式化输出 %是占位符,%s是字符串格式,%d整数格式,%f是浮点数格式 name = input('输入姓名') age = input('输入年龄') ...

  9. TestNG中DataProvider的用法二:简单的数据驱动

    @DataProvider标记的方法除了可以返回数组外,还可以返回一个Iterator,这样的好处是不用把所有的测试数据都加载到内存中,而是需要的时候就读一条. 下面的例子就使用了Iterator,然 ...

  10. SpringBoot Admin 使用指南

    什么是 SpringBoot Admin? Spring Boot Admin 是一个管理和监控你的 Spring Boot 应用程序的应用程序.这些应用程序通过 Spring Boot Admin ...