C# 自定义控件,日期时间选择输入插件

using System;
using System.ComponentModel;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
namespace pictureAnalyse
{
/// <summary>
/// 此类用于实现一个日期时间辅助输入插件,调用逻辑:
/// new DateTimeChoser(textBox1); //即可为textBox1绑定一个日期时间输入控件
/// </summary>
public partial class DateTimeChoser : UserControl
{
public static bool showConfirmButton = true; // 日期时间选择时,是否显示确定按钮

/// <summary>
/// 为textBoox添加一个日期时间选择控件,辅助日期时间的输入
/// </summary>
public static void AddTo(TextBox textBox)
{
try
{
DateTime time = DateTime.Parse(textBox.Text);
}
catch (Exception ex)
{
textBox.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}

textBox.MouseClick += textBoox_MouseClick;
}

/// <summary>
/// 为textBoox添加一个日期时间选择控件,辅助日期时间的输入,并设置初始时显示的时间
/// </summary>
public static void AddTo(TextBox textBoox, DateTime dateTime)
{
textBoox.Text = dateTime.ToString("yyyy-MM-dd HH:mm:ss");
textBoox.MouseClick += textBoox_MouseClick;
}

private static void textBoox_MouseClick(object sender, MouseEventArgs e)
{
TextBox textBox = sender as TextBox;

// 创建一个关联到textBox的日期时间选择控件
DateTimeChoser choser = new DateTimeChoser();
choser.showOn(textBox);

// 设置显示的时间为文本框中的日期时间
try
{
DateTime time = DateTime.Parse(textBox.Text);
choser.setDateTime(time);
}
catch (Exception ex) { }
}

public DateTimeChoser()
{
InitializeComponent();
init();
}

private void init()
{
// 时分秒设置
for (int i = 0; i < 24; i++) comboBox_hour.Items.Add((i < 10 ? "0" : "") + i);
for (int i = 0; i < 60; i = i + 1) comboBox_minite.Items.Add((i < 10 ? "0" : "") + i);
for (int i = 0; i < 60; i++) comboBox_second.Items.Add((i < 10 ? "0" : "") + i);

comboBox_hour.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox_minite.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox_second.DropDownStyle = ComboBoxStyle.DropDownList;

//设置显示的日期时间
setDateTime(DateTime.Now);
}

public delegate void DateTimeChanged_Handle(object sender, EventArgs e);
[Description("当选择日期或时间变动时,调用此事件"), Category("自定义事件")]
public event DateTimeChanged_Handle DateTimeChanged;

// 选择日期变动
private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
{
DateTime S = monthCalendar1.SelectionStart;
string date = S.ToString("yyyy-MM-dd");
if (!date.Equals(label_date.Text))
{
label_date.Text = date;
if (DateTimeChanged != null) DateTimeChanged(this, new EventArgs());
}
}

//选择的时间变动
private void TimeChanged(object sender, EventArgs e)
{
string time = comboBox_hour.Text + ":" + comboBox_minite.Text + ":" + comboBox_second.Text;
if (!time.Equals(label_time.Text))
{
label_time.Text = time;
if (DateTimeChanged != null) DateTimeChanged(this, new EventArgs());
}
}

// 设置显示到指定的日期时间
public void setDateTime(DateTime now)
{
// 初始时界面显示当前的日期时间
label_date.Text = now.ToString("yyyy-MM-dd");
monthCalendar1.SetDate(now);

// 设置时间
label_time.Text = now.ToString("HH:mm:ss");
comboBox_hour.SelectedIndex = now.Hour;
comboBox_minite.SelectedIndex = now.Minute;
comboBox_second.SelectedIndex = now.Second;
}

// 获取当前选择的日期时间
public string getDateTime()
{
return label_date.Text + " " + label_time.Text;
}

# region 自定义控件输入绑定逻辑,将当前日期时间控件绑定到指定的TextBox

private Form form;
TextBox textbox;
private Delegate[] textboxEvents;

// 在指定的TextBox中,显示当前日期时间选择控件,进行日期时间的输入
public void showOn(TextBox textBox, int offX = 0, int offY = 0)
{
Point P = getLocation(textBox);
P = new Point(P.X, P.Y + textBox.Height);

show(textBox, P.X + offX, P.Y + offY, showConfirmButton);
}

// 在TextBox点击时,调用DateTimeChoser进行日期时间的选择,当再次点击时,关闭之前的日期选择状态
private void show(TextBox textbox, int L, int T, bool showButton)
{
this.textbox = textbox;
textboxEvents = getEvents(textbox, "MouseClick"); // 获取TextBox原有事件处理逻辑
ClearEvent(textbox, "MouseClick"); // 移除TextBox原有MouseClick事件处理逻辑

// 新建一个窗体
form = new Form();
form.Width = this.Width;
form.Height = this.Height;
if (showButton) form.Height = this.Height + 40;
form.FormBorderStyle = FormBorderStyle.None; // 无边框
form.ShowInTaskbar = false; // 不在任务栏中显示
form.BackColor = Color.White; //

form.Location = new Point(L, T);

// 将当前日期时间选择控件添加到form中
this.Left = 0; this.Top = 0;
form.Controls.Add(this);

if (showButton)
{
Button button = new Button();
button.Text = "确定";
button.ForeColor = Color.Blue;
button.Left = (this.Width - button.Width) / 2;
button.Top = this.Height + (40 - button.Height) / 2;
form.Controls.Add(button);

button.Click += button_Click;
}

form.Show(); // 显示日期时间选择
form.Location = new Point(L, T);
form.TopMost = true;
form.Activate(); // 当前界面获取到焦点

Form Parent = getParentForm(textbox); // 获取TextBox的父窗体
if (Parent != null) Parent.FormClosed += Parent_FormClosed;

textbox.MouseClick += textbox_MouseClick;
}

// 添加
private void button_Click(object sender, EventArgs e)
{
textbox_MouseClick(textbox, null);
}

// 关闭当前form
private void Parent_FormClosed(object sender, FormClosedEventArgs e)
{
if (form != null)
{
form.Close();
form = null;
}
}

private void textbox_MouseClick(object sender, MouseEventArgs e)
{
TextBox textBox = sender as TextBox;
textBox.Text = getDateTime();

if (form != null)
{
form.Close();
form = null;
}

textBox.MouseClick -= textbox_MouseClick; // 移除当前事件处理逻辑
addEvents(textBox, "MouseClick", textboxEvents); // 还原TextBox原有事件处理逻辑
}

# endregion

// 获取给定控件的父窗体
public static Form getParentForm(Control control)
{
if (control is Form) return control as Form;

Control C = control;
while (C.Parent != null)
{
if (C.Parent is Form) return C.Parent as Form;
else C = C.Parent;
}

return null;
}

#region 获取控件的坐标信息

/// <summary>
/// 获取任意控件相对于屏幕的坐标
/// </summary>
public static Point getLocation(Control control)
{
Point P;
if (control is Form) P = getFormClientLocation(control as Form);
else P = control.Location;
if (control.Parent != null)
{
Control parent = control.Parent;
Point P2 = getLocation(parent);

P = new Point(P.X + P2.X, P.Y + P2.Y);
}

return P;
}

/// <summary>
/// 获取Form窗体有效显示区域的起点,相对于屏幕的坐标
/// </summary>
public static Point getFormClientLocation(Form form)
{
Rectangle rect = form.ClientRectangle;

int offx = 0, offy = 0;
if (form.FormBorderStyle != FormBorderStyle.None)
{
offx = (form.Width - rect.Width) / 2;
offy = (form.Height - rect.Height) - 8;
}

Point P = new Point(form.Location.X + offx, form.Location.Y + offy);

return P;
}

# endregion

# region 动态修改控件对应的事件处理逻辑

// ClearEvent(button1,"Click");//就会清除button1对象的Click事件的所有挂接事件。
private void ClearEvent(Control control, string eventname)
{
if (control == null) return;
if (string.IsNullOrEmpty(eventname)) return;
BindingFlags mPropertyFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic;
BindingFlags mFieldFlags = BindingFlags.Static | BindingFlags.NonPublic;
Type controlType = typeof(System.Windows.Forms.Control);
PropertyInfo propertyInfo = controlType.GetProperty("Events", mPropertyFlags);
EventHandlerList eventHandlerList = (EventHandlerList)propertyInfo.GetValue(control, null);
FieldInfo fieldInfo = (typeof(Control)).GetField("Event" + eventname, mFieldFlags);
Delegate d = eventHandlerList[fieldInfo.GetValue(control)];
if (d == null) return;
EventInfo eventInfo = controlType.GetEvent(eventname);
foreach (Delegate dx in d.GetInvocationList())
eventInfo.RemoveEventHandler(control, dx);
}

// getEvent(button1,"Click"); //就会获取到button1对象的Click事件的所有挂接事件。
private Delegate[] getEvents(Control control, string eventname)
{
if (control == null) return null;
if (string.IsNullOrEmpty(eventname)) return null;
BindingFlags mPropertyFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic;
BindingFlags mFieldFlags = BindingFlags.Static | BindingFlags.NonPublic;
Type controlType = typeof(System.Windows.Forms.Control);
PropertyInfo propertyInfo = controlType.GetProperty("Events", mPropertyFlags);
EventHandlerList eventHandlerList = (EventHandlerList)propertyInfo.GetValue(control, null);
FieldInfo fieldInfo = (typeof(Control)).GetField("Event" + eventname, mFieldFlags);
Delegate d = eventHandlerList[fieldInfo.GetValue(control)];
if (d == null) return null;

Delegate[] events = new Delegate[d.GetInvocationList().Length];
int i = 0;
foreach (Delegate dx in d.GetInvocationList()) events[i++] = dx;

return events;
}

// addEvents(button1,"Click"); // 为button1对象的Click事件挂接事件
private void addEvents(Control control, string eventname, Delegate[] evenets)
{
if (control == null) return;
if (string.IsNullOrEmpty(eventname)) return;

Type controlType = typeof(System.Windows.Forms.Control);
EventInfo eventInfo = controlType.GetEvent(eventname);

foreach (Delegate e in evenets)
eventInfo.AddEventHandler(control, e);
}

# endregion

}

}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace pictureAnalyse
{
partial class DateTimeChoser
{
/// <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.monthCalendar1 = new System.Windows.Forms.MonthCalendar();
this.panel_consume = new System.Windows.Forms.Panel();
this.label6 = new System.Windows.Forms.Label();
this.comboBox_second = new System.Windows.Forms.ComboBox();
this.label5 = new System.Windows.Forms.Label();
this.comboBox_minite = new System.Windows.Forms.ComboBox();
this.label4 = new System.Windows.Forms.Label();
this.comboBox_hour = new System.Windows.Forms.ComboBox();
this.panel1 = new System.Windows.Forms.Panel();
this.label_date = new System.Windows.Forms.Label();
this.label_time = new System.Windows.Forms.Label();
this.panel2 = new System.Windows.Forms.Panel();
this.panel_consume.SuspendLayout();
this.panel1.SuspendLayout();
this.panel2.SuspendLayout();
this.SuspendLayout();
//
// monthCalendar1
//
this.monthCalendar1.AllowDrop = true;
this.monthCalendar1.Location = new System.Drawing.Point(-3, 15);
this.monthCalendar1.Name = "monthCalendar1";
this.monthCalendar1.TabIndex = 0;
this.monthCalendar1.DateSelected += new System.Windows.Forms.DateRangeEventHandler(this.monthCalendar1_DateSelected);
//
// panel_consume
//
this.panel_consume.BackColor = System.Drawing.Color.White;
this.panel_consume.Controls.Add(this.label6);
this.panel_consume.Controls.Add(this.comboBox_second);
this.panel_consume.Controls.Add(this.label5);
this.panel_consume.Controls.Add(this.comboBox_minite);
this.panel_consume.Controls.Add(this.label4);
this.panel_consume.Controls.Add(this.comboBox_hour);
this.panel_consume.Location = new System.Drawing.Point(-2, 173);
this.panel_consume.Name = "panel_consume";
this.panel_consume.Size = new System.Drawing.Size(222, 30);
this.panel_consume.TabIndex = 23;
//
// label6
//
this.label6.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.label6.AutoSize = true;
this.label6.Location = new System.Drawing.Point(195, 10);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(17, 12);
this.label6.TabIndex = 15;
this.label6.Text = "秒";
//
// comboBox_second
//
this.comboBox_second.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.comboBox_second.FormattingEnabled = true;
this.comboBox_second.Location = new System.Drawing.Point(149, 6);
this.comboBox_second.Name = "comboBox_second";
this.comboBox_second.Size = new System.Drawing.Size(40, 20);
this.comboBox_second.TabIndex = 14;
this.comboBox_second.Text = "0";
this.comboBox_second.SelectedIndexChanged += new System.EventHandler(this.TimeChanged);
//
// label5
//
this.label5.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(126, 10);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(17, 12);
this.label5.TabIndex = 13;
this.label5.Text = "分";
//
// comboBox_minite
//
this.comboBox_minite.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.comboBox_minite.FormattingEnabled = true;
this.comboBox_minite.Location = new System.Drawing.Point(80, 6);
this.comboBox_minite.Name = "comboBox_minite";
this.comboBox_minite.Size = new System.Drawing.Size(40, 20);
this.comboBox_minite.TabIndex = 12;
this.comboBox_minite.Text = "0";
this.comboBox_minite.SelectedIndexChanged += new System.EventHandler(this.TimeChanged);
//
// label4
//
this.label4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(57, 10);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(17, 12);
this.label4.TabIndex = 11;
this.label4.Text = "时";
//
// comboBox_hour
//
this.comboBox_hour.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.comboBox_hour.FormattingEnabled = true;
this.comboBox_hour.Location = new System.Drawing.Point(9, 6);
this.comboBox_hour.Name = "comboBox_hour";
this.comboBox_hour.Size = new System.Drawing.Size(42, 20);
this.comboBox_hour.TabIndex = 10;
this.comboBox_hour.Text = "0";
this.comboBox_hour.SelectedIndexChanged += new System.EventHandler(this.TimeChanged);
//
// panel1
//
this.panel1.BackColor = System.Drawing.Color.White;
this.panel1.Controls.Add(this.label_date);
this.panel1.Controls.Add(this.label_time);
this.panel1.Location = new System.Drawing.Point(-3, -1);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(223, 23);
this.panel1.TabIndex = 25;
//
// label_date
//
this.label_date.AutoSize = true;
this.label_date.BackColor = System.Drawing.Color.White;
this.label_date.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label_date.Location = new System.Drawing.Point(18, 3);
this.label_date.Name = "label_date";
this.label_date.Size = new System.Drawing.Size(98, 16);
this.label_date.TabIndex = 26;
this.label_date.Text = "2016-06-12";
//
// label_time
//
this.label_time.AutoSize = true;
this.label_time.BackColor = System.Drawing.Color.White;
this.label_time.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.label_time.Location = new System.Drawing.Point(118, 3);
this.label_time.Name = "label_time";
this.label_time.Size = new System.Drawing.Size(80, 16);
this.label_time.TabIndex = 25;
this.label_time.Text = "12:23:35";
//
// panel2
//
this.panel2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel2.Controls.Add(this.panel1);
this.panel2.Controls.Add(this.panel_consume);
this.panel2.Controls.Add(this.monthCalendar1);
this.panel2.Location = new System.Drawing.Point(0, 0);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(215, 202);
this.panel2.TabIndex = 26;
//
// DateTimeChoser
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.panel2);
this.Name = "DateTimeChoser";
this.Size = new System.Drawing.Size(215, 202);
this.panel_consume.ResumeLayout(false);
this.panel_consume.PerformLayout();
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.panel2.ResumeLayout(false);
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.MonthCalendar monthCalendar1;
private System.Windows.Forms.Panel panel_consume;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.ComboBox comboBox_second;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.ComboBox comboBox_minite;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.ComboBox comboBox_hour;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Label label_date;
private System.Windows.Forms.Label label_time;
private System.Windows.Forms.Panel panel2;
}
}

https://blog.csdn.net/scimence/article/details/51673731

原文地址:https://www.cnblogs.com/bjchaofan/p/9665349.html

时间: 2024-10-11 03:36:15

C# 自定义控件,日期时间选择输入插件的相关文章

Java+Selenium操作日期时间选择框插件

在自动化测试的时候我们经常会碰到下面的时间日期插件(这个时候这个文本框是不运行我们输入时间的), 我们可以用java获取当前日期,然后用Selenium结合JS代码就可以直接往文本框输入内容. 像这种选择时间的input标签都会有一个readonly=""标签,这个时候我们就只能选择时间,不能手动输入,解决办法如下: Date date = new Date();//先获取当前日期 String startDate = new SimpleDateFormat("yyyy-M

Jquery mobiscroll 移动设备(手机)wap日期时间选择插件以及滑动、滚动插件

Jquery mobiscroll 移动设备(手机)wap日期时间选择插件以及滑动.滚动插件 : http://www.cnblogs.com/linJie1930906722/p/6072984.htm

flatpickr功能强大的日期时间选择器插件

flatpickr日期时间选择器支持移动手机,提供多种内置的主题效果,并且提供对中文的支持.它的特点还有: 使用SVG作为界面的图标. 兼容jQuery. 支持对各种日期格式的解析. 轻量级,高性能,压缩后的版本仅6K大小. 对手机原生日期格式的支持. 下图说明了使用jQuery UI.Bootstrap.packadate.js和flatpickr拉齐制作一个日期时间选择器时,所需要的文件尺寸大小: 下图是flatpickr日期时间选择器的各种主题效果: 配置参数 在配置参数中,所有的类型为s

日期时间选择器插件flatpickr

前言:在网页上需要输入时间的时候,我们可以用HTML5的inputl中的date类型.但是如下入所示,有些浏览器不支持.flatpickr这个小插件可以解决这个问题. 1.flatpickr日期时间选择器插件的github地址为:https://chmln.github.io/flatpickr/. 2.里面有很多例子,告诉我们呢怎么设置,不过太多很容易让人眼花.我这里做一个最简单的例子. 2.1引用人家的css和js //路径一定要写对 <link rel="stylesheet&quo

日期时间范围选择插件:daterangepicker使用总结

---恢复内容开始--- 分享说明: 项目中要使用日期时间范围选择对数据进行筛选;精确到年月日 时分秒;起初,使用了layui的时间日期选择插件;但是在IIE8第一次点击会报设置格式错误;研究了很久没解决,但能确定不是layui的问题;因为自己写的demo可以在IE8运行;只是在我的项目环境下某些代码冲突了;所以换用了bootstrap插件daterangepicker;看了很多资料;结合官网了文档;基本算是搞定了;把我的总结代码分享给大家;希望对使用daterangepicker插件的初学者有

日志监控_ElasticStack-0003.Logstash输入插件及实际生产案例应用?

新版插件: 说明: 从5.0开始,插件都独立拆分成gem包,每个插件可独立更新,无需等待Logstash自身整体更新,具体管理命令可参考./bin/logstash-plugin --help帮助信息../bin/logstash-plugin list其实所有的插件就位于本地./vendor/bundle/jruby/1.9/gems/目录下 扩展: 如果GitHub上面(https://github.com/logstash-plugins/)发布了扩展插件,可通过./bin/logstas

第三章 logstash - 输入插件之tcp与redis

常用的输入插件: tcp redis 一.tcp 1.用法 1 input { 2 tcp { 3 port => 4560 4 codec => json_lines 5 mode => server 6 host => 0.0.0.0 7 add_field => {"xxx":"xxx"} 8 ssl_cert => /xxx 9 ssl_enable => false 10 ssl_extra_chain_certs

[转]ASP.NET MVC HtmlHelper扩展之Calendar日期时间选择

本文转自:http://blog.bossma.cn/asp_net_mvc/asp-net-mvc-htmlhelper-calendar-datetime-select/ 这里我们扩展HtmlHelper,就像它包含在ASP.NET MVC中一样,扩展方法使我们能为已有的类添加方法.这里使用了一个日期时间选择控件:My97DatePicker,需要添加到网站中,并在页面中引用. 先看看是怎么扩展的: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

重写的HTML5酒店入住日期选择日历插件

<!DOCTYPE HTML><html lang="zh-CN"><head><title>接触角测定仪</title><script src="http://www.codefans.net/ajaxjs/jquery-1.6.2.min.js" type="application/javascript" language="javascript">&