diff --git a/RJTextBox.cs b/RJTextBox.cs index d6bf27a..ed4f066 100644 --- a/RJTextBox.cs +++ b/RJTextBox.cs @@ -21,6 +21,7 @@ public partial class RJTextBox : UserControl private int borderSize = 2; private bool underlinedStyle = false; private bool isFocused = false; + private bool removeNewline = false; private int borderRadius = 0; private Color placeholderColor = Color.DarkGray; @@ -31,6 +32,15 @@ public partial class RJTextBox : UserControl //Events public event EventHandler _TextChanged; + // Cached render point + private Rectangle rectBorderSmooth; + private Rectangle rectBorder; + private int smoothSize; + + private GraphicsPath pathBorderSmooth; + private GraphicsPath pathBorder; + private GraphicsPath pathTxt; + #endregion //-> Constructor @@ -38,6 +48,9 @@ public RJTextBox() { //Created by designer InitializeComponent(); + SetStyle(ControlStyles.AllPaintingInWmPaint, true); + SetStyle(ControlStyles.UserPaint, true); + SetStyle(ControlStyles.DoubleBuffer, true); } #region -> Properties @@ -68,6 +81,7 @@ public int BorderSize if (value >= 1) { borderSize = value; + layoutChanged = true; this.Invalidate(); } } @@ -84,6 +98,16 @@ public bool UnderlinedStyle } } + [Category("RJ Code Advance")] + public bool RemoveNewline + { + get { return removeNewline; } + set + { + removeNewline = value; + } + } + [Category("RJ Code Advance")] public bool PasswordChar { @@ -109,8 +133,12 @@ public override Color BackColor get { return base.BackColor; } set { - base.BackColor = value; - textBox1.BackColor = value; + try + { + base.BackColor = value; + textBox1.BackColor = value; + } + catch (ArgumentException) { return; } } } @@ -148,8 +176,13 @@ public string Texts } set { - textBox1.Text = value; - SetPlaceholder(); + if (!string.IsNullOrWhiteSpace(value)) + { + RemovePlaceholder(); + string val = value; + if (removeNewline) { val = val.Replace("\n", "").Replace("\r", ""); ; } + textBox1.Text = val; + } } } @@ -162,6 +195,7 @@ public int BorderRadius if (value >= 0) { borderRadius = value; + layoutChanged = true; this.Invalidate();//Redraw control } } @@ -191,14 +225,14 @@ public string PlaceholderText } } - - #endregion #region -> Overridden methods protected override void OnResize(EventArgs e) { base.OnResize(e); + layoutChanged = true; + Invalidate(); if (this.DesignMode) UpdateControlHeight(); } @@ -215,18 +249,17 @@ protected override void OnPaint(PaintEventArgs e) if (borderRadius > 1)//Rounded TextBox { //-Fields - 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)) + if (layoutChanged) + { + RefreshDataPoints(); + this.Region = new Region(pathBorderSmooth); // Set the rounded region of UserControl + } using (Pen penBorderSmooth = new Pen(this.Parent.BackColor, smoothSize)) using (Pen penBorder = new Pen(borderColor, borderSize)) { //-Drawing - this.Region = new Region(pathBorderSmooth);//Set the rounded region of UserControl if (borderRadius > 15) SetTextBoxRoundedRegion();//Set the rounded region of TextBox component + if (layoutChanged) { layoutChanged = false; } graph.SmoothingMode = SmoothingMode.AntiAlias; penBorder.Alignment = System.Drawing.Drawing2D.PenAlignment.Center; if (isFocused) penBorder.Color = borderFocusColor; @@ -253,7 +286,11 @@ protected override void OnPaint(PaintEventArgs e) //Draw border using (Pen penBorder = new Pen(borderColor, borderSize)) { - this.Region = new Region(this.ClientRectangle); + if (layoutChanged) + { + this.Region = new Region(this.ClientRectangle); + layoutChanged = false; + } penBorder.Alignment = System.Drawing.Drawing2D.PenAlignment.Inset; if (isFocused) penBorder.Color = borderFocusColor; @@ -289,6 +326,18 @@ private void RemovePlaceholder() textBox1.UseSystemPasswordChar = true; } } + + private void RefreshDataPoints() + { + rectBorderSmooth = this.ClientRectangle; + rectBorder = Rectangle.Inflate(rectBorderSmooth, -borderSize, -borderSize); + smoothSize = borderSize > 0 ? borderSize : 1; + pathBorderSmooth?.Dispose(); + pathBorderSmooth = GetFigurePath(rectBorderSmooth, borderRadius); + pathBorder?.Dispose(); + pathBorder = GetFigurePath(rectBorder, borderRadius - borderSize); + } + private GraphicsPath GetFigurePath(Rectangle rect, int radius) { GraphicsPath path = new GraphicsPath(); @@ -304,19 +353,17 @@ private GraphicsPath GetFigurePath(Rectangle rect, int radius) } private void SetTextBoxRoundedRegion() { - GraphicsPath pathTxt; - if (Multiline) - { - pathTxt = GetFigurePath(textBox1.ClientRectangle, borderRadius - borderSize); - textBox1.Region = new Region(pathTxt); - } - else + if (layoutChanged) { - pathTxt = GetFigurePath(textBox1.ClientRectangle, borderSize * 2); + pathTxt?.Dispose(); + int effectiveBorderRadius = Multiline + ? borderRadius - borderSize + : borderSize * 2; + pathTxt = GetFigurePath(textBox1.ClientRectangle, effectiveBorderRadius); textBox1.Region = new Region(pathTxt); } - pathTxt.Dispose(); } + private void UpdateControlHeight() { if (textBox1.Multiline == false) @@ -334,6 +381,7 @@ private void UpdateControlHeight() #region -> TextBox events private void textBox1_TextChanged(object sender, EventArgs e) { + if (isPlaceholder) return; if (_TextChanged != null) _TextChanged.Invoke(sender, e); } @@ -360,6 +408,7 @@ private void textBox1_Enter(object sender, EventArgs e) this.Invalidate(); RemovePlaceholder(); } + private void textBox1_Leave(object sender, EventArgs e) { isFocused = false;