I actually have two questions. When the user clicks the arrows to change months, the 1. of the given month is automatically selected. Is it possible to prevent this behavior, so date_changed first fires when the user clicks on an actual date?
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
{
this.Close();
}
}
I’ve placed my MonthCalendar in a seperate form. When clicking a button this form is opened, and the user can select a date. On the date_changed event I want the form to close so I did a this.Close(), but this makes the application crash and I get an ObjectDisposedException:
Cannot access a disposed object.
Object name: ‘MonthCalendar’
How do I close the form?
EDIT:
public partial class Form1 : Form
{
Form2 frm2;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
frm2 = new Form2();
frm2.theForm = this;
frm2.Show();
}
public void closeAform()
{
frm2.Close();
}
}
public partial class Form2 : Form
{
public Form1 theForm { get; set; }
public Form2()
{
InitializeComponent();
}
private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
{
theForm.closeAform();
}
}
No, it’s not possible to prevent this behavior. When the user navigates to a new month, that’s the same thing as changing the date. An alternative behavior doesn’t make much sense: when a new month is selected, some day in that month has to be selected, and the first day of the month is as good a candidate as any. In fact, the description for the
DateChangedevent even explains that it:Have you considered handling the
DateSelectedevent, instead? I suspect that this will come closer to doing what you want. Its description says that it:And like magic, when I handle the
DateSelectedevent instead, closing the form works just fine: