繁体   English   中英

使用C#中的按钮从月份日历中获取文本框中的日期

[英]Get Date in Text Box from month calendar with button in c#

我想在单击窗口表单的按钮后在文本框中使用月份日历日期。 打开表单时,日历应隐藏,单击按钮后,日历将可见。

我在Visual Studio 2008中使用c#,因此请根据此工具给我答复。

为此使用DateTimePicker类。 它完全具有您描述的行为。

尝试使用Control.Show()方法和Control.Visible属性:

private MonthCalendar _monthCalendar;

public Form1()
{
    InitializeComponent();
    // invisible on startup
    _monthCalendar.Visible = false;
    _monthCalendar.MaxSelectionCount = 1;
}

private void button1_Click(object sender, EventArgs e)
{
    //show when needed
    _monthCalendar.Show();
}   

private void textBox1_Enter(object sender, EventArgs e)
{
    _monthCalendar.Show();
}

private void textBox1_Leave(object sender, EventArgs e)
{
    if (!_monthCalendar.Focused)
        _monthCalendar.Visible = false;
}

private void monthCalendar_DateSelected(object sender, DateRangeEventArgs e)
{
    var monthCalendar = sender as MonthCalendar;
    textBox1.Text = monthCalendar.SelectionStart.ToString();
}

private void monthCalendar_Leave(object sender, EventArgs e)
{
    var monthCalendar = sender as MonthCalendar;
    monthCalendar.Visible = false;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM