323 lines
9.8 KiB
C#
323 lines
9.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Drawing.Drawing2D;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace ControllsCustom.NT_MASKTEXTBOX
|
|
{
|
|
public partial class LV_MASKTEDTEXTBOX : UserControl
|
|
{
|
|
private Color borderColor = Color.MediumSlateBlue;
|
|
private Color borderFocusColor = Color.HotPink;
|
|
private int borderSize = 2;
|
|
private bool underlinedStyle = false;
|
|
private bool isFocused = false;
|
|
private int borderRadius = 0;
|
|
private Color placeholderColor = Color.DarkGray;
|
|
private string placeholderText = "";
|
|
private bool isPlaceholder = false;
|
|
public LV_MASKTEDTEXTBOX()
|
|
{
|
|
InitializeComponent();
|
|
SetPlaceholder();
|
|
}
|
|
[Category("levelcode")]
|
|
public Color BorderColor
|
|
{
|
|
get { return borderColor; }
|
|
set
|
|
{
|
|
borderColor = value;
|
|
this.Invalidate();
|
|
}
|
|
}
|
|
|
|
[Category("levelcode")]
|
|
public Color BorderFocusColor
|
|
{
|
|
get { return borderFocusColor; }
|
|
set { borderFocusColor = value; }
|
|
}
|
|
|
|
[Category("levelcode")]
|
|
public int BorderSize
|
|
{
|
|
get { return borderSize; }
|
|
set
|
|
{
|
|
if (value >= 1)
|
|
{
|
|
borderSize = value;
|
|
this.Invalidate();
|
|
}
|
|
}
|
|
}
|
|
|
|
[Category("levelcode")]
|
|
public bool UnderlinedStyle
|
|
{
|
|
get { return underlinedStyle; }
|
|
set
|
|
{
|
|
underlinedStyle = value;
|
|
this.Invalidate();
|
|
}
|
|
}
|
|
|
|
[Category("levelcode")]
|
|
public override Color BackColor
|
|
{
|
|
get { return base.BackColor; }
|
|
set
|
|
{
|
|
base.BackColor = value;
|
|
maskedTextBox1.BackColor = value;
|
|
}
|
|
}
|
|
|
|
[Category("levelcode")]
|
|
public override Color ForeColor
|
|
{
|
|
get { return base.ForeColor; }
|
|
set
|
|
{
|
|
base.ForeColor = value;
|
|
maskedTextBox1.ForeColor = value;
|
|
}
|
|
}
|
|
|
|
[Category("levelcode")]
|
|
public override Font Font
|
|
{
|
|
get { return base.Font; }
|
|
set
|
|
{
|
|
base.Font = value;
|
|
maskedTextBox1.Font = value;
|
|
if (this.DesignMode)
|
|
UpdateControlHeight();
|
|
}
|
|
}
|
|
|
|
[Category("levelcode")]
|
|
public string Texts
|
|
{
|
|
get
|
|
{
|
|
if (isPlaceholder) return "";
|
|
else return maskedTextBox1.Text;
|
|
}
|
|
set
|
|
{
|
|
maskedTextBox1.Text = value;
|
|
SetPlaceholder();
|
|
}
|
|
}
|
|
|
|
[Category("levelcode")]
|
|
public int BorderRadius
|
|
{
|
|
get { return borderRadius; }
|
|
set
|
|
{
|
|
if (value >= 0)
|
|
{
|
|
borderRadius = value;
|
|
this.Invalidate();//Redraw control
|
|
}
|
|
}
|
|
}
|
|
|
|
[Category("levelcode")]
|
|
public Color PlaceholderColor
|
|
{
|
|
get { return placeholderColor; }
|
|
set
|
|
{
|
|
placeholderColor = value;
|
|
if (isPlaceholder)
|
|
maskedTextBox1.ForeColor = value;
|
|
}
|
|
}
|
|
|
|
[Category("levelcode")]
|
|
public string PlaceholderText
|
|
{
|
|
get { return placeholderText; }
|
|
set
|
|
{
|
|
placeholderText = value;
|
|
maskedTextBox1.Text = "";
|
|
SetPlaceholder();
|
|
}
|
|
}
|
|
[Category("levelcode")]
|
|
public string Mask
|
|
{
|
|
get { return maskedTextBox1.Mask; }
|
|
set { maskedTextBox1.Mask = value; }
|
|
}
|
|
|
|
|
|
protected override void OnResize(EventArgs e)
|
|
{
|
|
base.OnResize(e);
|
|
if (this.DesignMode)
|
|
UpdateControlHeight();
|
|
}
|
|
|
|
protected override void OnLoad(EventArgs e)
|
|
{
|
|
base.OnLoad(e);
|
|
UpdateControlHeight();
|
|
}
|
|
|
|
protected override void OnPaint(PaintEventArgs e)
|
|
{
|
|
base.OnPaint(e);
|
|
Graphics graph = e.Graphics;
|
|
|
|
if (borderRadius > 1)//Rounded MaskedTextBox
|
|
{
|
|
var rectBorderSmooth = this.ClientRectangle;
|
|
var rectBorder = Rectangle.Inflate(rectBorderSmooth, -borderSize, -borderSize);
|
|
int smoothSize = borderSize > 0 ? borderSize : 1;
|
|
|
|
using (GraphicsPath pathBorderSmooth = GetFigurePath(rectBorderSmooth, borderRadius))
|
|
using (GraphicsPath pathBorder = GetFigurePath(rectBorder, borderRadius - borderSize))
|
|
using (Pen penBorderSmooth = new Pen(this.Parent.BackColor, smoothSize))
|
|
using (Pen penBorder = new Pen(borderColor, borderSize))
|
|
{
|
|
this.Region = new Region(pathBorderSmooth);//Set the rounded region of UserControl
|
|
if (borderRadius > 15) SetMaskedTextBoxRoundedRegion();//Set the rounded region of MaskedTextBox component
|
|
graph.SmoothingMode = SmoothingMode.AntiAlias;
|
|
penBorder.Alignment = PenAlignment.Center;
|
|
if (isFocused) penBorder.Color = borderFocusColor;
|
|
|
|
if (underlinedStyle) //Line Style
|
|
{
|
|
graph.DrawPath(penBorderSmooth, pathBorderSmooth);
|
|
graph.SmoothingMode = SmoothingMode.None;
|
|
graph.DrawLine(penBorder, 0, this.Height - 1, this.Width, this.Height - 1);
|
|
}
|
|
else //Normal Style
|
|
{
|
|
graph.DrawPath(penBorderSmooth, pathBorderSmooth);
|
|
graph.DrawPath(penBorder, pathBorder);
|
|
}
|
|
}
|
|
}
|
|
else //Square/Normal MaskedTextBox
|
|
{
|
|
using (Pen penBorder = new Pen(borderColor, borderSize))
|
|
{
|
|
this.Region = new Region(this.ClientRectangle);
|
|
penBorder.Alignment = PenAlignment.Inset;
|
|
if (isFocused) penBorder.Color = borderFocusColor;
|
|
|
|
if (underlinedStyle) //Line Style
|
|
graph.DrawLine(penBorder, 0, this.Height - 1, this.Width, this.Height - 1);
|
|
else //Normal Style
|
|
graph.DrawRectangle(penBorder, 0, 0, this.Width - 0.5F, this.Height - 0.5F);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void SetPlaceholder()
|
|
{
|
|
if (string.IsNullOrWhiteSpace(maskedTextBox1.Text) && placeholderText != "")
|
|
{
|
|
isPlaceholder = true;
|
|
maskedTextBox1.Text = placeholderText;
|
|
maskedTextBox1.ForeColor = placeholderColor;
|
|
}
|
|
}
|
|
|
|
private void RemovePlaceholder()
|
|
{
|
|
if (isPlaceholder && placeholderText != "")
|
|
{
|
|
isPlaceholder = false;
|
|
maskedTextBox1.Text = "";
|
|
maskedTextBox1.ForeColor = this.ForeColor;
|
|
}
|
|
}
|
|
|
|
private GraphicsPath GetFigurePath(Rectangle rect, int radius)
|
|
{
|
|
GraphicsPath path = new GraphicsPath();
|
|
float curveSize = radius * 2F;
|
|
|
|
path.StartFigure();
|
|
path.AddArc(rect.X, rect.Y, curveSize, curveSize, 180, 90);
|
|
path.AddArc(rect.Right - curveSize, rect.Y, curveSize, curveSize, 270, 90);
|
|
path.AddArc(rect.Right - curveSize, rect.Bottom - curveSize, curveSize, curveSize, 0, 90);
|
|
path.AddArc(rect.X, rect.Bottom - curveSize, curveSize, curveSize, 90, 90);
|
|
path.CloseFigure();
|
|
return path;
|
|
}
|
|
|
|
private void SetMaskedTextBoxRoundedRegion()
|
|
{
|
|
GraphicsPath pathTxt;
|
|
if (maskedTextBox1.Multiline)
|
|
{
|
|
pathTxt = GetFigurePath(maskedTextBox1.ClientRectangle, borderRadius - borderSize);
|
|
maskedTextBox1.Region = new Region(pathTxt);
|
|
}
|
|
else
|
|
{
|
|
pathTxt = GetFigurePath(maskedTextBox1.ClientRectangle, borderSize * 2);
|
|
maskedTextBox1.Region = new Region(pathTxt);
|
|
}
|
|
pathTxt.Dispose();
|
|
}
|
|
|
|
private void UpdateControlHeight()
|
|
{
|
|
if (!maskedTextBox1.Multiline)
|
|
{
|
|
int txtHeight = TextRenderer.MeasureText("Text", this.Font).Height + 1;
|
|
maskedTextBox1.Multiline = true;
|
|
maskedTextBox1.MinimumSize = new Size(0, txtHeight);
|
|
maskedTextBox1.Multiline = false;
|
|
|
|
this.Height = maskedTextBox1.Height + this.Padding.Top + this.Padding.Bottom;
|
|
}
|
|
}
|
|
|
|
private void maskedTextBox1_Enter(object sender, EventArgs e)
|
|
{
|
|
isFocused = true;
|
|
this.Invalidate();
|
|
RemovePlaceholder();
|
|
}
|
|
|
|
private void maskedTextBox1_Leave(object sender, EventArgs e)
|
|
{
|
|
isFocused = false;
|
|
this.Invalidate();
|
|
SetPlaceholder();
|
|
}
|
|
|
|
private void maskedTextBox1_TextChanged(object sender, EventArgs e)
|
|
{
|
|
if (isPlaceholder)
|
|
{
|
|
maskedTextBox1.ForeColor = placeholderColor;
|
|
}
|
|
else
|
|
{
|
|
maskedTextBox1.ForeColor = this.ForeColor;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|