winforms 在winform上显示空白日期范围,但在选择日期时填充

bpzcxfmw  于 2022-11-17  发布在  其他
关注(0)|答案(1)|浏览(188)

我已经完成了第一部分的工作,我希望弹出一个日期选择框,日期框中的初始空格。选择日期后,我希望它们填充在空格中。到目前为止,当我选择日期时,它仍然保持空白
到目前为止我所尝试的是下面的内容;更具体地说,按钮单击是我希望日期范围重新出现的地方。

DateTime dtpStartDate = new DateTime(1000, 1, 1);
DateTime dtpEndDate = new DateTime(1000, 1, 1);
Form frm = new Form();
frm.StartPosition = FormStartPosition.CenterScreen;
frm.Height = 160;
frm.Width = 300;
Label lbl = new Label();
lbl.Text = "Select Date Range";
lbl.AutoSize = true;
lbl.Width = 165;
lbl.Height = 13;
lbl.Font = new Font(FontFamily.GenericSansSerif, 8.25F, FontStyle.Bold);
lbl.Location = new Point(66, 10);
Label lblStartDate = new Label();
lblStartDate.Text = "Start Month/Year:";
lblStartDate.AutoSize = true;
lblStartDate.Location = new Point(13, 26);
Label lblEndDate = new Label();
lblEndDate.Text = "End Month/Year:";
lblEndDate.AutoSize = true;
lblEndDate.Location = new Point(165, 26);
DateTimePicker dtpStart = new DateTimePicker();
dtpStart.Location = new Point(12, 45);
dtpStart.Format = DateTimePickerFormat.Custom;
dtpStart.CustomFormat = " ";
dtpStart.Width = 120;
dtpStart.Value = first;
DateTimePicker dtpEnd = new DateTimePicker();
dtpEnd.Location = new Point(165, 45);
dtpEnd.Format = DateTimePickerFormat.Custom;
dtpEnd.CustomFormat = " ";
dtpEnd.Width = 120;
dtpEnd.Value = last;
Button btn = new Button();
btn.Text = "Submit";
btn.Location = new Point(100, 80);
btn.Click += (object sender, EventArgs e) =>
            {
                dtpStart.Enabled = true;
                dtpStart.CustomFormat = "MMMM yyyy";
                dtpEnd.Enabled = true;
                dtpEnd.CustomFormat = "MMMM yyyy";
                dtpStartDate = dtpStart.Value;
                dtpEndDate = dtpEnd.Value;
                frm.Close();
            };
frm.Controls.Add(lbl);
frm.Controls.Add(lblStartDate);
frm.Controls.Add(lblEndDate);
frm.Controls.Add(dtpStart);
frm.Controls.Add(dtpEnd);
frm.Controls.Add(btn);
frm.Controls.Add(checkBoxBlanks11);
frm.ShowDialog();
js81xvg6

js81xvg61#

我在ValueChanged事件中更改了格式,它起作用了。我采用了原来的代码,添加了新的处理程序。就是这样。

{
        ...
        dtpStart.ValueChanged += DtpStartValueChanged;
        ...
    }

    private void DtpStartValueChanged(object? sender, EventArgs e)
    {
        var dtpStart = (DateTimePicker)sender;
        dtpStart.Format = DateTimePickerFormat.Short;
    }

您希望在单击按钮时显示日期,但是您在按钮单击处理程序中关闭了frm窗体。

相关问题