0

我其实有两个问题。当用户单击箭头更改月份时,将自动选择给定月份的 1。是否可以防止这种行为,所以当用户点击实际日期时 date_changed 首先触发?

public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
        {
            this.Close();
        }
    }

我已将 MonthCalendar 放在单独的表格中。当点击一个按钮时,这个表单被打开,用户可以选择一个日期。在 date_changed 事件中,我希望表单关闭,所以我执行了 this.Close(),但这会使应用程序崩溃并且我得到一个 ObjectDisposedException:

无法访问已处置的对象。对象名称:“月历”

如何关闭表格?

编辑:

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

1 回答 1

2
  1. 不,不可能阻止这种行为。当用户导航到新月份时,这与更改日期相同。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. 事实上,DateChanged事件的描述甚至解释说:

    当日期范围因用户选择或通过下/上个月导航而发生变化时发生。

    您是否考虑过处理DateSelected事件?我怀疑这将更接近于做你想做的事。它的描述说:

    在用户选择日期或日期范围时发生。

  2. 就像魔术一样,当我处理DateSelected事件时,关闭表单就可以了:

    public class DatePickerForm : Form
    {
        public DatePickerForm()
        {
            InitializeComponent();
        }
    
        private void calendar_DateSelected(object sender, DateRangeEventArgs e)
        {
            this.Close();
        }
    }
    
于 2011-04-20T10:32:48.317 回答