官网

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. 题解 P5016 【龙虎斗】

    首先祝各位大佬noip有个好成绩吧 当时比赛有个大数据,蒟蒻我暴力居然过了,好激动 这题一定要注意开long long (那个大数据就是我开long long才过的) 还有刚开始应设置答案为m(见解析 ...

  2. 创建RDD

    RDD创建 在Spark中创建RDD的创建方式大概可以分为三种:从集合中创建RDD:从外部存储创建RDD:从其他RDD创建. 由一个已经存在的Scala集合创建,集合并行化,而从集合中创建RDD,Sp ...

  3. PTA L2-031 深入虎穴 非dfs的一点想法

    著名的王牌间谍 007 需要执行一次任务,获取敌方的机密情报.已知情报藏在一个地下迷宫里,迷宫只有一个入口,里面有很多条通路,每条路通向一扇门.每一扇门背后或者是一个房间,或者又有很多条路,同样是每条 ...

  4. java多线程调用run和不调用run的区别

    当在java程序中创建一个线程的时候,会三种情况: 1,只运行run方法 2,先执行start,再执行run方法 3,只运行start方法 三者的区别如下: 运行方式 区别 只运行run 只运行run ...

  5. git commit规范化实践

    最近从svn转到git进行代码版本控制,今天了解了git commit规范化的一些知识后,写此文章记录下配置过程. 环境 编辑器使用的是vscode,项目框架是vue3.0 规范化工具 规范化git ...

  6. 第三章 jsp数据交互(二)

    Application:当前服务器(可以包含多个会话):当服务器启动后就会创建一个application对象,被所有用户共享page.request.session.application四个作用域对 ...

  7. 实现跳转的jsp小例子

    <%@page import="java.io.UnsupportedEncodingException"%> <%@ page language="j ...

  8. 异步请求xhr、ajax、axios与fetch的区别比较

    目录 1. XMLHttpRequest对象 2. jQuery ajax 3. axios 4. fetch 参考 why: 为什么会出现不同的方法呢? what: 这些都是异步请求数据的方法.在不 ...

  9. How to check all timestamps of a file

    A friend of mine she asked me how to check all timestamps of a file on an NTFS volume. She did not h ...

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

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