C# 两个textBox控件调用同一个monthCalendar输入日期

C# 多个textBox控件调用同一个monthCalendar输入日期:代码如下

需要添加textBox1+textBox2+monthCalendar1控件

DateTime FirstClick;
int TextBoxNumber;
private void textBox1_Click(object sender, EventArgs e)
{
this.TextBoxNumber = 1;
this.monthCalendar1.Visible = true;
}

private void textBox2_Click(object sender, EventArgs e)
{
this.TextBoxNumber = 2;
this.monthCalendar1.Visible = true;
}

/// <summary>
/// 日期控件调用函数
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
{
if (this.TextBoxNumber == 1)
{
MonthCalendarDoubleClick(this.textBox1, this.monthCalendar1);
}
else
{
MonthCalendarDoubleClick(this.textBox2, this.monthCalendar1);
}
}
/// <summary>
/// 双击日期控件调用函数
/// </summary>
/// <param name="SelectedTextBox"></param>
/// <param name="monthCalendar"></param>
private void MonthCalendarDoubleClick(TextBox SelectedTextBox, MonthCalendar monthCalendar)
{
DateTime SecondClick = DateTime.Now;//记录下第二次单击时间;
string ClickSpan = Convert.ToString(SecondClick - FirstClick);//计算两次单击的时间间隔;格式为:00:00:00.1875000
string Separator = ":.";
char[] CharClickSpan = Separator.ToCharArray();
string[] arrayClickSpan = ClickSpan.Split(CharClickSpan);
if (arrayClickSpan.Length == 4 && arrayClickSpan[0].Equals("00") && arrayClickSpan[1].Equals("00") && arrayClickSpan[2].Equals("00"))
{
if (Convert.ToInt32(arrayClickSpan[3].Substring(0, 3)) > SystemInformation.DoubleClickTime)//如果两次单击的时间间隔大于系统双击时间;
{
FirstClick = SecondClick;//赋第二次单击时间给第一次;
}
else
{
//两次单击的时间间隔小于或者等于喜欢双击时间
SelectedTextBox.Text = monthCalendar.SelectionStart.ToString("yyyy-MM-dd");//把双击选中的文本赋给TextBox;
this.monthCalendar1.Visible = false;
}
}
else
{
FirstClick = SecondClick;
}
}

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭