Skip to content

Added fixes provided on the issue section #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
95 changes: 72 additions & 23 deletions RJTextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,13 +32,25 @@ 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
public RJTextBox()
{
//Created by designer
InitializeComponent();
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
}

#region -> Properties
Expand Down Expand Up @@ -68,6 +81,7 @@ public int BorderSize
if (value >= 1)
{
borderSize = value;
layoutChanged = true;
this.Invalidate();
}
}
Expand All @@ -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
{
Expand All @@ -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; }
}
}

Expand Down Expand Up @@ -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;
}
}
}

Expand All @@ -162,6 +195,7 @@ public int BorderRadius
if (value >= 0)
{
borderRadius = value;
layoutChanged = true;
this.Invalidate();//Redraw control
}
}
Expand Down Expand Up @@ -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();
}
Expand All @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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();
Expand All @@ -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)
Expand All @@ -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);
}
Expand All @@ -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;
Expand Down