WinForms中的水印文本框
public class WatermarkTextBox:TextBox{ public string WatermarkText { get; set; } public Color WatermarkColor { get; set; } private Color TextColor { get; set; } private bool isInTransition; public WatermarkTextBox() { WatermarkColor = SystemColors.GrayText; } private bool HasText { get { return Text.IsNotNullOrBlankOr(WatermarkText); }} protected override void OnEnter(EventArgs e) { base.OnEnter(e); if (HasText) return; isInTransition = true; ForeColor = TextColor; Text = String.Empty; isInTransition = false; } protected override void OnForeColorChanged(EventArgs e) { base.OnForeColorChanged(e); if (!isInTransition) //the change came from outside TextColor = ForeColor; } protected override void OnLeave(EventArgs e) { base.OnLeave(e); if (HasText) return; isInTransition = true; ForeColor = WatermarkColor; Text = WatermarkText.EmptyIfNull(); isInTransition = false; }}
public class WatermarkTextBox:TextBox{ private string watermarkText; public string WatermarkText { get { return watermarkText; } set { watermarkText = value; if (watermarkText.IsNullOrBlank()) CueProvider.ClearCue(this); else CueProvider.SetCue(this, watermarkText); } }}