官网

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. ASP.NET Core[源码分析篇] - Startup

    应用启动的重要类 - Startup 在ASP.NET Core - 从Program和Startup开始这篇文章里面,我们知道了Startup这个类的重要性,它主要负责了: 配置应用需要的服务(服务 ...

  2. ld: warning: directory not found for option ''

    iOS开发中经常遇到这样的警告,如图所示: 原因是存在未用到的目录. 解决方法:选择Build Settings,找到Search Paths中的Library Search Paths,如下图 删除 ...

  3. 记kepServer读写西门子PLC

    在程序开发过程中为了测试方法或者验证某个属性的值是否正确 经常通过Kepserver 的 OPC Quick Client来手动置点或者读取点位 例如 这里显示的值都是经过转化后得到的十进制值,那我们 ...

  4. Kafka学习(四)-------- Kafka核心之Producer

    通过https://www.cnblogs.com/tree1123/p/11243668.html 已经对consumer有了一定的了解.producer比consumer要简单一些. 一.旧版本p ...

  5. 夯实Java基础(六)——包装类

    1.包装类简介 我们都知道Java是面向对象编程语言,包含了8种基本数据类型,但是这8种基本数据类型并不支持面向对象的特征,它们既不是类,也不能调用方法.这在实际使用时存在很多的不便,比如int类型需 ...

  6. 用CSS来定义<p>标签,要求实现以下效果:字体颜色再IE6下为黑色,IE7下为红色,IE8下为绿色,其他浏览器下为黄色。

    <!DOCTYPE html><html><head><meta charset="utf-8"><meta name=&qu ...

  7. SQL Labs刷题补坑记录(less31-less53)

    LESS31: 双引号直接报错,那么肯定可以报错注入,并且也过滤了一些东西,^异或没有过滤,异或真香 -1" and (if(length(database())=8,1,0)) and & ...

  8. 10、二维数组的申请(test7.java)

    我个人认为,二维数组的构造就是在一位数组中存入一个地址,这个地址指向另一个一位数组,这样通过这种排列组合便构造成了二维数组. 二维数组的形状,有的时候二维数组看起来像是一个矩阵,所以一般情况下如果涉及 ...

  9. 【POJ - 3176】牛保龄球 (简单dp)

    牛保龄球 直接中文了 Descriptions 奶牛打保龄球时不使用实际的保龄球.它们各自取一个数字(在0..99范围内),然后排成一个标准的保龄球状三角形,如下所示: 7 3 8 8 1 0 2 7 ...

  10. OpenCV中图像处理

    一.颜色空间转换 1.cv2.cvtColor(input_img,flag) 参数1是要转换的图像 参数2是转换类型  例如:cv2.COLOR_BGR2HSV (RGB->HSV) cv2. ...