C# 自定义时间段选择器

1.需求分析:

  我们实现一个时间段选择器,类似原生的DateTimePicker,但是需要设置两个时间点。该控件由一个textBox加一个图标按钮组成:

时间段选择器.png

但点击图标按钮时,弹出下拉两个时间点设置页面:
C# 自定义时间段选择器_第1张图片
控件下拉.png

点击弹出下拉页面使用Simple-Popup-Control来实现,Simple-Popup-Control源码地址如下:
https://www.codeproject.com/Articles/17502/Simple-Popup-Control

2. 实现

1. 创建项目

在VS2010中新建窗体应用程序DateTimeIntervelControlTest,添加类库DateTimeIntervelControl,我们把自定义的控件做成库也是方便我们的调用。在DateTimeIntervelControl类库中添加Simple-Popup-Control的.dll引用,并新建DateTimeIntervelPicker.cs和DateTimeIntervelPopup.cs,DateTimeIntervelPicker为时间段选择器,DateTimeIntervelPopup为下拉弹窗页面。

2. 页面实现

DateTimeIntervelPicker.png
timeSetting.png
C# 自定义时间段选择器_第2张图片
DateTimeIntervelPopup.png

3. 功能实现

DateTimeIntervelPicker弹出DateTimeIntervelPopup

public DateTimeIntervelPicker()
{
    InitializeComponent();
    DateTimeIntervelPopup content = new DateTimeIntervelPopup();
    popup = new Popup(content);
    popup.Dock = DockStyle.Bottom;
}

Popup popup;

private void btnPopup_Click(object sender, EventArgs e)
{
    popup.Show(txtBoxDisplay);
}

添加timeSetting成员变量用于获取设定的时间

public String MyTime
{
    get
    {
        return numHour.Text + ":" + numMin.Text + ":" + numSecond.Text;
    }
}

实现DateTimeIntervelPopup确定按钮事件,点击返回开始和结束的DateTime

public delegate void BtnClickHandle(object sender, EventArgs e, DateTime start, DateTime end);

public event BtnClickHandle SureClick;

private void btnSure_Click(object sender, EventArgs e)
{
    //String sStart = "2014-7-5 3:0:20";
    DateTime start = calendarStart.SelectionStart;
    String sStart = start.Year + "-" + start.Month + "-" + start.Day + " " + timeStart.MyTime;
    start = Convert.ToDateTime(sStart);

    DateTime end = calendarEnd.SelectionStart;
    String sEnd = start.Year + "-" + start.Month + "-" + start.Day + " " + timeStart.MyTime;
    end = Convert.ToDateTime(sEnd);

    if (SureClick != null)
    {
        SureClick(sender, e, start, end);  //把日期时间传给DateTimeIntervelPicker
    }
}

在DateTimeIntervelPicker实现content.SureClick事件

public DateTimeIntervelPicker()
{
    InitializeComponent();
    DateTimeIntervelPopup content = new DateTimeIntervelPopup();
    popup = new Popup(content);
    popup.Dock = DockStyle.Bottom;
    //添加DateTimeIntervelPopup点击按钮事件
    content.SureClick +=new DateTimeIntervelPopup.BtnClickHandle(content_SureClick);
}

Popup popup;

private void btnPopup_Click(object sender, EventArgs e)
{
    popup.Show(txtBoxDisplay);
}

private void content_SureClick(object sender, EventArgs e, DateTime start, DateTime end)
{
    startTime = start; //属性赋值
    endTime = end;

    StringBuilder sb = new StringBuilder();
    //sb.Append(start.Year + ".");
    sb.Append(start.Month + ".");
    sb.Append(start.Day + " ");
    sb.Append(start.Hour + ":");
    sb.Append(start.Minute + ":");
    sb.Append(start.Second);

    sb.Append("-");

    //sb.Append(end.Year + ".");
    sb.Append(end.Month + ".");
    sb.Append(end.Day + " ");
    sb.Append(end.Hour + ":");
    sb.Append(end.Minute + ":");
    sb.Append(end.Second);

    txtBoxDisplay.Text = sb.ToString();
    popup.Hide();
}

private DateTime startTime;
private DateTime endTime;

public DateTime StartTime {
    get
    {
        return startTime;
    }
}

public DateTime EndTime
{
    get
    {
        return endTime;
    }
}

源码下载:
https://github.com/XiaoYiHao/DateTimeIntervelControl/tree/master#datetimeintervelcontrol

你可能感兴趣的:(C# 自定义时间段选择器)