Files
lserp_cs_6.0/插件库/Lskj.Control/LabelTimeEdit.cs
T

253 lines
8.2 KiB
C#

using System;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraEditors;
namespace Lskj.Control
{
/// <summary>
/// 带标题的时间输入控件,使用上下按钮调整时间。
/// </summary>
public class LabelTimeEdit : BaseUserControl
{
private readonly Panel plLeft;
private readonly Panel plRight;
private readonly Label lblText;
private readonly TimeEdit txtEdit;
private string formatString = "HH:mm:ss";
public LabelTimeEdit()
{
plLeft = new Panel();
plRight = new Panel();
lblText = new Label();
txtEdit = new TimeEdit();
SuspendLayout();
plLeft.SuspendLayout();
plRight.SuspendLayout();
((System.ComponentModel.ISupportInitialize)txtEdit.Properties).BeginInit();
plLeft.BackColor = Color.Transparent;
plLeft.Controls.Add(lblText);
plLeft.Dock = DockStyle.Left;
plLeft.Size = new Size(40, 21);
lblText.AutoSize = true;
lblText.Location = new Point(5, 4);
lblText.Text = "名称";
plRight.Controls.Add(txtEdit);
plRight.Dock = DockStyle.Fill;
plRight.Location = new Point(40, 0);
txtEdit.Dock = DockStyle.Fill;
txtEdit.EditValue = null;
txtEdit.Properties.Buttons.Clear();
txtEdit.Properties.Buttons.Add(new DevExpress.XtraEditors.Controls.EditorButton(
DevExpress.XtraEditors.Controls.ButtonPredefines.Combo));
txtEdit.Properties.TimeEditStyle = DevExpress.XtraEditors.Repository.TimeEditStyle.SpinButtons;
AutoScaleMode = AutoScaleMode.None;
BackColor = Color.Transparent;
Controls.Add(plRight);
Controls.Add(plLeft);
Size = new Size(162, 21);
((System.ComponentModel.ISupportInitialize)txtEdit.Properties).EndInit();
plRight.ResumeLayout(false);
plLeft.ResumeLayout(false);
plLeft.PerformLayout();
ResumeLayout(false);
}
/// <summary>
/// 时间编辑器。
/// </summary>
public TimeEdit TextEdit
{
get { return txtEdit; }
}
public override string LabelText
{
get { return lblText.Text; }
set
{
lblText.Text = value;
if (FontSize > 0)
{
plLeft.AutoSize = false;
lblText.AutoSize = false;
lblText.Dock = DockStyle.Fill;
lblText.Location = new Point(0, 0);
lblText.TextAlign = ContentAlignment.MiddleLeft;
txtEdit.Properties.AutoHeight = false;
plLeft.Width = value.Length * GetCharWidth();
GetCharWidthMultilingual(lblText, lblText.Text, plLeft);
}
else
{
plLeft.Width = lblText.Width + PaddingLeft;
}
}
}
public override float FontSize
{
get { return base.FontSize; }
set
{
base.FontSize = value;
if (value > 0)
{
lblText.Font = new Font(lblText.Font.FontFamily, value);
txtEdit.Font = new Font(txtEdit.Font.FontFamily, value);
}
}
}
public override string EditText
{
get { return txtEdit.EditValue == null ? string.Empty : txtEdit.Text; }
set
{
TimeSpan timeValue;
txtEdit.EditValue = TryGetTime(value, out timeValue)
? (object)DateTime.Today.Add(timeValue)
: null;
}
}
public override string NullText
{
get { return txtEdit.Properties.NullValuePrompt; }
set
{
if (!string.IsNullOrEmpty(value))
{
txtEdit.Properties.NullValuePromptShowForEmptyValue = true;
txtEdit.Properties.NullValuePrompt = value;
}
base.NullText = value;
}
}
public override bool ReadOnly
{
get { return base.ReadOnly; }
set
{
base.ReadOnly = value;
txtEdit.Properties.ReadOnly = value;
txtEdit.Enabled = !value;
lblText.ForeColor = value ? ReadOnlyLabelForceColor : Required ? RequiredLabelForceColor : DefaultLabelForceColor;
}
}
public override bool Required
{
get { return base.Required; }
set
{
base.Required = value;
if (value)
{
lblText.ForeColor = RequiredLabelForceColor;
}
}
}
public override Color BackgroundColor
{
get { return txtEdit.Properties.Appearance.BackColor; }
set { txtEdit.Properties.Appearance.BackColor = value; }
}
public override Color ForeColor
{
get { return txtEdit.Properties.Appearance.ForeColor; }
set { txtEdit.Properties.Appearance.ForeColor = value; }
}
public override bool ContentBold
{
get { return txtEdit.Properties.Appearance.Font.Bold; }
set
{
Font oldFont = txtEdit.Properties.Appearance.Font;
txtEdit.Properties.Appearance.Font = new Font(oldFont.FontFamily, oldFont.Size, value ? FontStyle.Bold : FontStyle.Regular);
}
}
/// <summary>
/// 设置时间的显示和输入格式。
/// </summary>
public void TimeFormat(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return;
}
formatString = value;
txtEdit.Properties.DisplayFormat.FormatString = value;
txtEdit.Properties.EditFormat.FormatString = value;
txtEdit.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
txtEdit.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.Custom;
txtEdit.Properties.Mask.EditMask = value;
txtEdit.Properties.Mask.UseMaskAsDisplayFormat = true;
txtEdit.Properties.TimeFormat = value.IndexOf("ss", StringComparison.OrdinalIgnoreCase) >= 0
? DevExpress.XtraEditors.Controls.TimeFormat.HourMinSec
: DevExpress.XtraEditors.Controls.TimeFormat.HourMin;
}
/// <summary>
/// 按控件显示精度比较当前值和原值,避免 DateTime/TimeSpan 字符串格式差异造成误判。
/// </summary>
public bool IsSameTime(string value)
{
TimeSpan oldTime;
TimeSpan currentTime;
bool hasOldTime = TryGetTime(value, out oldTime);
bool hasCurrentTime = TryGetTime(EditText, out currentTime);
if (!hasOldTime || !hasCurrentTime)
{
return !hasOldTime && !hasCurrentTime;
}
if (formatString.IndexOf("ss", StringComparison.OrdinalIgnoreCase) >= 0)
{
return oldTime.Hours == currentTime.Hours &&
oldTime.Minutes == currentTime.Minutes &&
oldTime.Seconds == currentTime.Seconds;
}
return oldTime.Hours == currentTime.Hours && oldTime.Minutes == currentTime.Minutes;
}
private static bool TryGetTime(string value, out TimeSpan timeValue)
{
timeValue = TimeSpan.Zero;
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
if (TimeSpan.TryParse(value, out timeValue))
{
return true;
}
DateTime dateValue;
if (DateTime.TryParse(value, out dateValue))
{
timeValue = dateValue.TimeOfDay;
return true;
}
return false;
}
}
}