官网

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. F#周报2019年第29期

    新闻 ML.NET 1.2发布,包含Model Builder升级 NuGet.org上现在显示GitHub的使用情况 微基准测试设计准则 为线程添加mono.wasm支持 Haskell--经验总结 ...

  2. Redis持久化背后的故事

    Redis持久化 Redis提供了不同的持久化选项: RDB持久化以指定的时间间隔保存那个时间点的数据快照. AOF持久化方法则会记录每一个服务器收到的写操作.在服务器启动时,这些记录的操作会逐条执行 ...

  3. [leetcode] 110. Balanced Binary Tree (easy)

    原题链接 水题 深度搜索每一节点的左右深度,左右深度差大于1就返回false. class Solution { public: bool isBalanced(TreeNode *root) { b ...

  4. 《C#从入门到精通(第3版)》目录

    C#从入门到精通(第3版)pdf+源码 一.基础知识 1.初识C#及其开发环境 2.开始C#之旅 3.变量与常量 4.表达式与运算符 5.字符与字符串 6.流程控制语句 7.数组与集合 8.属性和方法 ...

  5. Pytest进阶之参数化

    前言 unittest单元测试框架使用DDT进行数据驱动测试,那么身为功能更加强大且更加灵活的Pytest框架怎么可能没有数据驱动的概念呢?其实Pytest是使用@pytest.mark.parame ...

  6. nginx文件名逻辑漏洞_CVE-2013-4547漏洞复现

    nginx文件名逻辑漏洞_CVE-2013-4547漏洞复现 一.漏洞描述 这个漏洞其实和代码执行没有太大的关系,主要原因是错误地解析了请求的URL,错误地获取到用户请求的文件名,导致出现权限绕过.代 ...

  7. Integrating Thymeleaf with Spring

    这个是基于注解的配置方式,基于配置文件的http://www.cnblogs.com/honger/p/6875148.html 一.整体结构图 二.web.xml文件,这里使用了注解的方式 < ...

  8. C#async/await心得

    结论: 异步方法的方法签名要加 async,否则就算返回 Task 也是普通方法. 调用异步方法,可以加 await 或不加 await,两者方式都是马上返回,不加 await 得到的是 Task 对 ...

  9. 快速安装k8s,版本为1.13.8

    利用rpm快速部署k8s #!/bin/bash #快速安装k8s #by love19791126 107420988@qq.com pwd=$(pwd) masteripaddr=#(ip a s ...

  10. js实现3D切换效果

    今天分享一个3d翻转动画效果,js+css3+h5实现,没有框架. 先看下html部分: <div class="box"> <ul> <li> ...