单击窗口窗体的按钮后,我想在文本框中使用月历日期。表单打开时日历应隐藏,单击按钮后日历可见。
我在 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;
}