Home > other >  Show blank date range on winforms but populate upon selecting date
Show blank date range on winforms but populate upon selecting date

Time:08-30

I've got the first part working which is I want a date selection box to pop up with initial blanks in the date box. After selecting the dates I want them to be populated in the blank spaces. So far when I select the dates it still remains blanks

what I've tried so far is below; To be more specific the button click is where I'm expecting the date range to reappear.

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

CodePudding user response:

I changed the format in the ValueChanged event and it worked. I took your code as it was, added the new handler. That's it.

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

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

You are expecting to appear the dates on the button click, but you close the frm form in your button click handler.

  • Related