├── LICENSE ├── README.md ├── RJTextBox.Designer.cs └── RJTextBox.cs /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Custom-TextBox 2 | Custom TextBox - Border, Focus Color & Underlined Style - Custom Controls WinForm C# 3 |

VIDEO:

4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /RJTextBox.Designer.cs: -------------------------------------------------------------------------------- 1 | namespace CustomControls.RJControls 2 | { 3 | partial class RJTextBox 4 | { 5 | /// 6 | /// Required designer variable. 7 | /// 8 | private System.ComponentModel.IContainer components = null; 9 | 10 | /// 11 | /// Clean up any resources being used. 12 | /// 13 | /// true if managed resources should be disposed; otherwise, false. 14 | protected override void Dispose(bool disposing) 15 | { 16 | if (disposing && (components != null)) 17 | { 18 | components.Dispose(); 19 | } 20 | base.Dispose(disposing); 21 | } 22 | 23 | #region Component Designer generated code 24 | 25 | /// 26 | /// Required method for Designer support - do not modify 27 | /// the contents of this method with the code editor. 28 | /// 29 | private void InitializeComponent() 30 | { 31 | this.textBox1 = new System.Windows.Forms.TextBox(); 32 | this.SuspendLayout(); 33 | // 34 | // textBox1 35 | // 36 | this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.None; 37 | this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill; 38 | this.textBox1.Location = new System.Drawing.Point(7, 7); 39 | this.textBox1.Name = "textBox1"; 40 | this.textBox1.Size = new System.Drawing.Size(236, 15); 41 | this.textBox1.TabIndex = 0; 42 | this.textBox1.Click += new System.EventHandler(this.textBox1_Click); 43 | this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged); 44 | this.textBox1.Enter += new System.EventHandler(this.textBox1_Enter); 45 | this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress); 46 | this.textBox1.Leave += new System.EventHandler(this.textBox1_Leave); 47 | this.textBox1.MouseEnter += new System.EventHandler(this.textBox1_MouseEnter); 48 | this.textBox1.MouseLeave += new System.EventHandler(this.textBox1_MouseLeave); 49 | // 50 | // RJTextBox 51 | // 52 | this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; 53 | this.BackColor = System.Drawing.SystemColors.Window; 54 | this.Controls.Add(this.textBox1); 55 | this.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 56 | this.ForeColor = System.Drawing.Color.DimGray; 57 | this.Margin = new System.Windows.Forms.Padding(4); 58 | this.Name = "RJTextBox"; 59 | this.Padding = new System.Windows.Forms.Padding(7); 60 | this.Size = new System.Drawing.Size(250, 30); 61 | this.ResumeLayout(false); 62 | this.PerformLayout(); 63 | 64 | } 65 | 66 | #endregion 67 | 68 | private System.Windows.Forms.TextBox textBox1; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /RJTextBox.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel; 4 | using System.Drawing; 5 | using System.Data; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | using System.Windows.Forms; 10 | 11 | namespace CustomControls.RJControls 12 | { 13 | [DefaultEvent("_TextChanged")] 14 | public partial class RJTextBox : UserControl 15 | { 16 | //Fields 17 | private Color borderColor = Color.MediumSlateBlue; 18 | private int borderSize = 2; 19 | private bool underlinedStyle = false; 20 | private Color borderFocusColor = Color.HotPink; 21 | private bool isFocused = false; 22 | 23 | //Constructor 24 | public RJTextBox() 25 | { 26 | InitializeComponent(); 27 | } 28 | 29 | //Events 30 | public event EventHandler _TextChanged; 31 | 32 | //Properties 33 | [Category("RJ Code Advance")] 34 | public Color BorderColor 35 | { 36 | get { return borderColor; } 37 | set 38 | { 39 | borderColor = value; 40 | this.Invalidate(); 41 | } 42 | } 43 | [Category("RJ Code Advance")] 44 | public int BorderSize 45 | { 46 | get { return borderSize; } 47 | set 48 | { 49 | borderSize = value; 50 | this.Invalidate(); 51 | } 52 | } 53 | 54 | [Category("RJ Code Advance")] 55 | public bool UnderlinedStyle 56 | { 57 | get { return underlinedStyle; } 58 | set 59 | { 60 | underlinedStyle = value; 61 | this.Invalidate(); 62 | } 63 | } 64 | 65 | [Category("RJ Code Advance")] 66 | public bool PasswordChar 67 | { 68 | get { return textBox1.UseSystemPasswordChar; } 69 | set { textBox1.UseSystemPasswordChar = value; } 70 | } 71 | 72 | [Category("RJ Code Advance")] 73 | public bool Multiline 74 | { 75 | get { return textBox1.Multiline; } 76 | set { textBox1.Multiline = value; } 77 | } 78 | 79 | [Category("RJ Code Advance")] 80 | public override Color BackColor 81 | { 82 | get { return base.BackColor; } 83 | set 84 | { 85 | base.BackColor = value; 86 | textBox1.BackColor = value; 87 | } 88 | } 89 | 90 | [Category("RJ Code Advance")] 91 | public override Color ForeColor 92 | { 93 | get { return base.ForeColor; } 94 | set 95 | { 96 | base.ForeColor = value; 97 | textBox1.ForeColor = value; 98 | } 99 | } 100 | 101 | [Category("RJ Code Advance")] 102 | public override Font Font 103 | { 104 | get { return base.Font; } 105 | set 106 | { 107 | base.Font = value; 108 | textBox1.Font = value; 109 | if (this.DesignMode) 110 | UpdateControlHeight(); 111 | } 112 | } 113 | 114 | [Category("RJ Code Advance")] 115 | public string Texts 116 | { 117 | get { return textBox1.Text; } 118 | set { textBox1.Text = value; } 119 | } 120 | 121 | [Category("RJ Code Advance")] 122 | public Color BorderFocusColor 123 | { 124 | get { return borderFocusColor; } 125 | set { borderFocusColor = value; } 126 | } 127 | 128 | //Overridden methods 129 | 130 | protected override void OnPaint(PaintEventArgs e) 131 | { 132 | base.OnPaint(e); 133 | Graphics graph = e.Graphics; 134 | 135 | //Draw border 136 | using (Pen penBorder = new Pen(borderColor, borderSize)) 137 | { 138 | penBorder.Alignment = System.Drawing.Drawing2D.PenAlignment.Inset; 139 | if (isFocused) penBorder.Color = borderFocusColor; 140 | 141 | if (underlinedStyle) //Line Style 142 | graph.DrawLine(penBorder, 0, this.Height - 1, this.Width, this.Height - 1); 143 | else //Normal Style 144 | graph.DrawRectangle(penBorder, 0, 0, this.Width - 0.5F, this.Height - 0.5F); 145 | } 146 | } 147 | 148 | protected override void OnResize(EventArgs e) 149 | { 150 | base.OnResize(e); 151 | if (this.DesignMode) 152 | UpdateControlHeight(); 153 | } 154 | 155 | protected override void OnLoad(EventArgs e) 156 | { 157 | base.OnLoad(e); 158 | UpdateControlHeight(); 159 | } 160 | 161 | //Private methods 162 | private void UpdateControlHeight() 163 | { 164 | if (textBox1.Multiline == false) 165 | { 166 | int txtHeight = TextRenderer.MeasureText("Text", this.Font).Height + 1; 167 | textBox1.Multiline = true; 168 | textBox1.MinimumSize = new Size(0, txtHeight); 169 | textBox1.Multiline = false; 170 | 171 | this.Height = textBox1.Height + this.Padding.Top + this.Padding.Bottom; 172 | } 173 | } 174 | 175 | //TextBox events 176 | private void textBox1_TextChanged(object sender, EventArgs e) 177 | { 178 | if (_TextChanged != null) 179 | _TextChanged.Invoke(sender, e); 180 | } 181 | 182 | private void textBox1_Click(object sender, EventArgs e) 183 | { 184 | this.OnClick(e); 185 | } 186 | 187 | private void textBox1_MouseEnter(object sender, EventArgs e) 188 | { 189 | this.OnMouseEnter(e); 190 | } 191 | 192 | private void textBox1_MouseLeave(object sender, EventArgs e) 193 | { 194 | this.OnMouseLeave(e); 195 | } 196 | 197 | private void textBox1_KeyPress(object sender, KeyPressEventArgs e) 198 | { 199 | this.OnKeyPress(e); 200 | } 201 | 202 | private void textBox1_Enter(object sender, EventArgs e) 203 | { 204 | isFocused = true; 205 | this.Invalidate(); 206 | } 207 | 208 | private void textBox1_Leave(object sender, EventArgs e) 209 | { 210 | isFocused = false; 211 | this.Invalidate(); 212 | } 213 | 214 | ///::::+ 215 | } 216 | } 217 | --------------------------------------------------------------------------------