开发者

How do I close a form in the date_changed event of MonthCalendar?

开发者 https://www.devze.com 2023-02-27 01:15 出处:网络
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 fir

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?

ED开发者_C百科IT:

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();
        }
    }


  1. 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 DateChanged event even explains that it:

    Occurs when the range of dates changes due to user selection, or through next/previous month navigation.

    Have you considered handling the DateSelected event, instead? I suspect that this will come closer to doing what you want. Its description says that it:

    Occurs when the user selects a date or a range of dates.

  2. And like magic, when I handle the DateSelected event instead, closing the form works just fine:

    public class DatePickerForm : Form
    {
        public DatePickerForm()
        {
            InitializeComponent();
        }
    
        private void calendar_DateSelected(object sender, DateRangeEventArgs e)
        {
            this.Close();
        }
    }
    
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号