using DevExpress.XtraEditors; using DevExpress.XtraEditors.Controls; using System; using System.Drawing; using System.Windows.Forms; namespace Lskj.Control.SpecialDate { public class SpecialDatePopup : UserControl { private const string Morning = "\u4e0a\u5348"; private const string Afternoon = "\u4e0b\u5348"; private readonly CalendarControl calendar; private readonly RadioGroup halfDayRadio; private readonly SimpleButton btnOk; private readonly SimpleButton btnClear; private readonly SimpleButton btnCancel; // 弹出层不直接操作表格,只把按钮动作通知给 GridControlEx。 public event EventHandler ConfirmClick; public event EventHandler ClearClick; public event EventHandler CancelClick; // 当前正在编辑的表格下拉编辑器,由 GridControlEx 在 QueryPopUp 时赋值。 public PopupContainerEdit OwnerEdit { get; set; } public SpecialDatePopup() { this.calendar = new CalendarControl(); this.halfDayRadio = new RadioGroup(); this.btnOk = new SimpleButton(); this.btnClear = new SimpleButton(); this.btnCancel = new SimpleButton(); InitializeComponent(); } public string SelectedValue { get { string halfDay = (this.halfDayRadio.EditValue + "").Equals(Afternoon) ? Afternoon : Morning; return this.calendar.DateTime.ToString("yyyy-MM-dd") + " " + halfDay; } } // 打开下拉框时,根据当前单元格值回填日期和上午/下午。 public void SetValue(string value) { DateTime date = DateTime.Today; string halfDay = Morning; if (!string.IsNullOrWhiteSpace(value)) { string text = value.Trim(); if (text.Contains(Afternoon)) { halfDay = Afternoon; } else if (text.Contains(Morning)) { halfDay = Morning; } string dateText = text.Replace(Morning, "").Replace(Afternoon, "").Trim(); DateTime parsedDate; if (DateTime.TryParse(dateText, out parsedDate) || DateTime.TryParse(text, out parsedDate)) { date = parsedDate.Date; // 兼容历史时间值,例如 2026-05-17 15:00:00 会默认识别为下午。 if (!text.Contains(Morning) && !text.Contains(Afternoon)) { halfDay = parsedDate.Hour >= 12 ? Afternoon : Morning; } } } this.calendar.DateTime = date; this.halfDayRadio.EditValue = halfDay; } private void InitializeComponent() { PanelControl rightPanel = new PanelControl(); PanelControl bottomPanel = new PanelControl(); LabelControl halfDayLabel = new LabelControl(); ((System.ComponentModel.ISupportInitialize)(rightPanel)).BeginInit(); rightPanel.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(bottomPanel)).BeginInit(); bottomPanel.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.halfDayRadio.Properties)).BeginInit(); this.SuspendLayout(); this.Size = new Size(430, 260); this.MinimumSize = this.Size; this.calendar.Dock = DockStyle.Fill; // 这里不要 DevExpress 自带的时分秒编辑,只保留日期选择。 this.calendar.CalendarTimeEditing = DevExpress.Utils.DefaultBoolean.False; this.calendar.ShowTodayButton = true; this.calendar.ShowClearButton = false; rightPanel.Dock = DockStyle.Right; rightPanel.BorderStyle = BorderStyles.NoBorder; rightPanel.Width = 110; halfDayLabel.Text = "\u65f6\u6bb5"; halfDayLabel.Location = new Point(14, 18); // 右侧只提供上午/下午两个固定选项,保证保存字符串格式稳定。 this.halfDayRadio.Location = new Point(12, 46); this.halfDayRadio.Size = new Size(86, 70); this.halfDayRadio.Properties.BorderStyle = BorderStyles.NoBorder; this.halfDayRadio.Properties.Items.Add(new RadioGroupItem(Morning, Morning)); this.halfDayRadio.Properties.Items.Add(new RadioGroupItem(Afternoon, Afternoon)); this.halfDayRadio.EditValue = Morning; rightPanel.Controls.Add(this.halfDayRadio); rightPanel.Controls.Add(halfDayLabel); bottomPanel.Dock = DockStyle.Bottom; bottomPanel.BorderStyle = BorderStyles.NoBorder; bottomPanel.Height = 36; this.btnOk.Text = "\u786e\u5b9a(&O)"; this.btnOk.Size = new Size(72, 24); this.btnOk.Location = new Point(180, 6); this.btnOk.Click += OnOkClick; this.btnClear.Text = "\u6e05\u9664"; this.btnClear.Size = new Size(72, 24); this.btnClear.Location = new Point(260, 6); this.btnClear.Click += OnClearClick; this.btnCancel.Text = "\u53d6\u6d88"; this.btnCancel.Size = new Size(72, 24); this.btnCancel.Location = new Point(340, 6); this.btnCancel.Click += OnCancelClick; bottomPanel.Controls.Add(this.btnOk); bottomPanel.Controls.Add(this.btnClear); bottomPanel.Controls.Add(this.btnCancel); this.Controls.Add(this.calendar); this.Controls.Add(rightPanel); this.Controls.Add(bottomPanel); ((System.ComponentModel.ISupportInitialize)(this.halfDayRadio.Properties)).EndInit(); ((System.ComponentModel.ISupportInitialize)(bottomPanel)).EndInit(); bottomPanel.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(rightPanel)).EndInit(); rightPanel.ResumeLayout(false); rightPanel.PerformLayout(); this.ResumeLayout(false); } private void OnOkClick(object sender, EventArgs e) { // 通知 GridControlEx 写入 SelectedValue 并关闭下拉框。 if (ConfirmClick != null) { ConfirmClick(this, EventArgs.Empty); } } private void OnClearClick(object sender, EventArgs e) { // 通知 GridControlEx 清空当前单元格。 if (ClearClick != null) { ClearClick(this, EventArgs.Empty); } } private void OnCancelClick(object sender, EventArgs e) { // 通知 GridControlEx 只关闭下拉框,不改单元格值。 if (CancelClick != null) { CancelClick(this, EventArgs.Empty); } } } }