官网

http://www.hzhcontrols.com

前提

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

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

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

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

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

麻烦博客下方点个【推荐】,谢谢

NuGet

Install-Package HZH_Controls

目录

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

用处及效果

准备工作

这个控件将继承自(三)c#Winform自定义控件-有图标的按钮,如不了解,请移步查看

开始

添加一个用户控件UCDropDownBtn,继承自UCBtnImg

处理一些属性

 Forms.FrmAnchor _frmAnchor;
private int _dropPanelHeight = -;
public new event EventHandler BtnClick;
[Description("下拉框高度"), Category("自定义")]
public int DropPanelHeight
{
get { return _dropPanelHeight; }
set { _dropPanelHeight = value; }
}
private string[] btns ;
[Description("按钮"), Category("自定义")]
public string[] Btns
{
get { return btns; }
set { btns = value; }
}
[Obsolete("不再可用的属性")]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public override Image Image
{
get;
set;
}
[Obsolete("不再可用的属性")]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public override ContentAlignment ImageAlign
{
get;
set;
}

点击时候显示下拉框

 void UCDropDownBtn_BtnClick(object sender, EventArgs e)
{
if (_frmAnchor == null || _frmAnchor.IsDisposed || _frmAnchor.Visible == false)
{ if (Btns != null && Btns.Length > )
{
int intRow = ;
int intCom = ;
var p = this.PointToScreen(this.Location);
while (true)
{
int intScreenHeight = Screen.PrimaryScreen.Bounds.Height;
if ((p.Y + this.Height + Btns.Length / intCom * < intScreenHeight || p.Y - Btns.Length / intCom * > )
&& (_dropPanelHeight <= ? true : (Btns.Length / intCom * <= _dropPanelHeight)))
{
intRow = Btns.Length / intCom + (Btns.Length % intCom != ? : );
break;
}
intCom++;
}
UCTimePanel ucTime = new UCTimePanel();
ucTime.IsShowBorder = true;
int intWidth = this.Width / intCom; Size size = new Size(intCom * intWidth, intRow * );
ucTime.Size = size;
ucTime.FirstEvent = true;
ucTime.SelectSourceEvent += ucTime_SelectSourceEvent;
ucTime.Row = intRow;
ucTime.Column = intCom; List<KeyValuePair<string, string>> lst = new List<KeyValuePair<string, string>>();
foreach (var item in Btns)
{
lst.Add(new KeyValuePair<string, string>(item, item));
}
ucTime.Source = lst; _frmAnchor = new Forms.FrmAnchor(this, ucTime);
_frmAnchor.Load += (a, b) => { (a as Form).Size = size; }; _frmAnchor.Show(this.FindForm()); }
}
else
{
_frmAnchor.Close();
}
}

处理一下按钮事件

 void ucTime_SelectSourceEvent(object sender, EventArgs e)
{
if (_frmAnchor != null && !_frmAnchor.IsDisposed && _frmAnchor.Visible)
{
_frmAnchor.Close(); if (BtnClick != null)
{
BtnClick(sender.ToString(), e);
}
}
}

完整代码

 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.Btn
{
[DefaultEvent("BtnClick")]
public partial class UCDropDownBtn : UCBtnImg
{
Forms.FrmAnchor _frmAnchor;
private int _dropPanelHeight = -;
public new event EventHandler BtnClick;
[Description("下拉框高度"), Category("自定义")]
public int DropPanelHeight
{
get { return _dropPanelHeight; }
set { _dropPanelHeight = value; }
}
private string[] btns ;
[Description("按钮"), Category("自定义")]
public string[] Btns
{
get { return btns; }
set { btns = value; }
}
[Obsolete("不再可用的属性")]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public override Image Image
{
get;
set;
}
[Obsolete("不再可用的属性")]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public override ContentAlignment ImageAlign
{
get;
set;
} public UCDropDownBtn()
{
InitializeComponent();
IsShowTips = false;
this.lbl.Image=Properties.Resources.ComboBox;
this.lbl.ImageAlign = ContentAlignment.MiddleRight;
base.BtnClick += UCDropDownBtn_BtnClick;
} void UCDropDownBtn_BtnClick(object sender, EventArgs e)
{
if (_frmAnchor == null || _frmAnchor.IsDisposed || _frmAnchor.Visible == false)
{ if (Btns != null && Btns.Length > )
{
int intRow = ;
int intCom = ;
var p = this.PointToScreen(this.Location);
while (true)
{
int intScreenHeight = Screen.PrimaryScreen.Bounds.Height;
if ((p.Y + this.Height + Btns.Length / intCom * < intScreenHeight || p.Y - Btns.Length / intCom * > )
&& (_dropPanelHeight <= ? true : (Btns.Length / intCom * <= _dropPanelHeight)))
{
intRow = Btns.Length / intCom + (Btns.Length % intCom != ? : );
break;
}
intCom++;
}
UCTimePanel ucTime = new UCTimePanel();
ucTime.IsShowBorder = true;
int intWidth = this.Width / intCom; Size size = new Size(intCom * intWidth, intRow * );
ucTime.Size = size;
ucTime.FirstEvent = true;
ucTime.SelectSourceEvent += ucTime_SelectSourceEvent;
ucTime.Row = intRow;
ucTime.Column = intCom; List<KeyValuePair<string, string>> lst = new List<KeyValuePair<string, string>>();
foreach (var item in Btns)
{
lst.Add(new KeyValuePair<string, string>(item, item));
}
ucTime.Source = lst; _frmAnchor = new Forms.FrmAnchor(this, ucTime);
_frmAnchor.Load += (a, b) => { (a as Form).Size = size; }; _frmAnchor.Show(this.FindForm()); }
}
else
{
_frmAnchor.Close();
}
}
void ucTime_SelectSourceEvent(object sender, EventArgs e)
{
if (_frmAnchor != null && !_frmAnchor.IsDisposed && _frmAnchor.Visible)
{
_frmAnchor.Close(); if (BtnClick != null)
{
BtnClick(sender.ToString(), e);
}
}
}
}
}
 namespace HZH_Controls.Controls.Btn
{
partial class UCDropDownBtn
{
/// <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()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(UCDropDownBtn));
this.SuspendLayout();
//
// lbl
//
this.lbl.Font = new System.Drawing.Font("微软雅黑", 14F);
this.lbl.ForeColor = System.Drawing.Color.White;
this.lbl.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
this.lbl.ImageList = null;
this.lbl.Text = "自定义按钮";
this.lbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// UCDropDownBtn
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.BtnFont = new System.Drawing.Font("微软雅黑", 14F);
this.BtnForeColor = System.Drawing.Color.White;
this.ForeColor = System.Drawing.Color.White;
this.Image = ((System.Drawing.Image)(resources.GetObject("$this.Image")));
this.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
this.Margin = new System.Windows.Forms.Padding();
this.Name = "UCDropDownBtn";
this.ResumeLayout(false); } #endregion
}
}

最后的话

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

(四十八)c#Winform自定义控件-下拉按钮的更多相关文章

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

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

  2. (三十五)c#Winform自定义控件-下拉框

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

  3. (四十)c#Winform自定义控件-开关-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

  4. NeHe OpenGL教程 第四十八课:轨迹球

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  5. SQL注入之Sqli-labs系列第四十七关,第四十八关,第四十九关(ORDER BY注入)

    0x1 源码区别点 将id变为字符型:$sql = "SELECT * FROM users ORDER BY '$id'"; 0x2实例测试 (1)and rand相结合的方式 ...

  6. 《手把手教你》系列技巧篇(四十八)-java+ selenium自动化测试-判断元素是否可操作(详解教程)

    1.简介 webdriver有三种判断元素状态的方法,分别是isEnabled,isSelected 和 isDisplayed,其中isSelected在前面的内容中已经简单的介绍了,isSelec ...

  7. 第四十八个知识点:TPM的目的和使用方法

    第四十八个知识点:TPM的目的和使用方法 在检查TPM目的之前,值得去尝试理解TPM设计出来的目的是为了克服什么样的问题.真正的问题是信任.信任什么?首先内存和软件运行在电脑上.这些东西能直接的通过操 ...

  8. Qt中QComboBox中自定义界面使用stylesheet实现下拉按钮独立效果

    使用QSS自定义控件界面时,QT中控件QCombobox含有两个子控件drop-down和down-arrow.一般而言,当改变QCombox时,很多效果都会出来,但是,针对下拉按钮和下拉图标的自定义 ...

  9. 修改select下拉框的下拉按钮

    ie上的下拉框下拉按钮真是太丑了,如何把他自定义一下呢? 首先,把浏览器自带的下拉框去掉:  select::-ms-expand { display: none; } 接下来,用自己喜欢的下拉图片去 ...

随机推荐

  1. jquery3和layui冲突导,致使用layui.layer.full弹出全屏iframe窗口时高度152px问题

    项目中使用的jquery版本是jquery-3.2.1,在使用layui弹出全屏iframe窗口时,iframe窗口顶部总是出现一个152px高的滚动窗口无法实现真正全屏,代码如下: <!DOC ...

  2. http的无状态

    无状态协议是指协议对务处理没有记忆能力.缺少状态意味着如果后续处理需要前面的信息,则它必须重传,这样可能导致每次连接传送的数据量增大.另一方面,在服务器不需要先前信息时它的应答就较快. Http协议不 ...

  3. TCP加速机制是如何加速的?

    一.什么是TCP加速?   TCP加速就是在高时延链路提高吞吐量的一系列解决方案.   二.为什么需要对TCP进行加速?   1.传统的TCP拥塞控制算法并不适用于高时延.高误码的链路. 2.随着we ...

  4. Linux下安装配置Jmeter5.1,并执行jmx文件

    Windows下的jmeter是GUI模式,可查看操作,但是GUI对性能的干扰比较大,所有一般压测会在Linux上运行. 下面是Linux下安装配置Jmeter5.1,并执行jmx文件的步骤, 一.安 ...

  5. .Net Core DevOps -免费用Azure四步实现自动化发布(CI/CD)

    前言 linux 大行其道的今天想必大家都已经拥抱 core 了吧,通常的方案都是 gitlab+jenkins+centos,但是这样的方案不适合我这种懒人,一直在寻求简单的解决方案,在寻求方案的过 ...

  6. 我的ubuntu kylin中mentohust的使用历程

    1首先下载mentohus 最新版下载(包括源码):http://code.google.com/p/mentohust/downloads/list 2打开终端(Ctrl+Alt+T) 输入sudo ...

  7. SpringBoot Jar包瘦身 - 跟大文件说再见!

    前言 SpringBoot部署起来配置非常少,如果服务器部署在公司内网,上传速度还行,但是如果部署在公网(阿里云等云服务器上),部署起来实在头疼.就是 编译出来的 Jar 包很大,如果工程引入了许多开 ...

  8. 对于HTTP过程中POST内容加密的解决方案

    0x00前言 前几天我师傅和我提及了这件事情 正常情况下 抓包过程中遇到加密情况会很迷茫 昨天把这个都弄了一下 也感谢大佬中间的指导 我一开始看到密码的类型下意识的是base64 但是去解密发现不对 ...

  9. 基于python的Elasticsearch索引的建立和数据的上传

    这是我的第一篇博客,还请大家多多指点 Thanks ♪(・ω・)ノ         今天我想讲一讲关于Elasticsearch的索引建立,当然提前是你已经安装部署好Elasticsearch. ok ...

  10. LFS8.3BOOT引导疑点解决

    LFS系统 的BOOT引导 在LFS书中写到的BOOT引导,时直接将宿主机的BOOT分区挂载当LFS的BOOT分区中,虽然这样也是可以实现BOOT引导的,但是我并不想这样做,所以BOOT引导就变得有些 ...