我已經完成了第一部分作業,我希望在日期框中彈出一個帶有初始空白的日期選擇框。選擇日期后,我希望將它們填充在空白處。到目前為止,當我選擇日期時它仍然是空白
到目前為止我嘗試過的如下;更具體地說,單擊按鈕是我期望日期范圍重新出現的地方。
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();
uj5u.com熱心網友回復:
我更改了 ValueChanged 事件中的格式并且它起作用了。我按原樣使用您的代碼,添加了新的處理程式。而已。
{
...
dtpStart.ValueChanged = DtpStartValueChanged;
...
}
private void DtpStartValueChanged(object? sender, EventArgs e)
{
var dtpStart = (DateTimePicker)sender;
dtpStart.Format = DateTimePickerFormat.Short;
}
您希望在按鈕單擊時顯示日期,但您frm在按鈕單擊處理程式中關閉了表單。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/506817.html
