Custom Text Box - Custom Controls WinForm C # - RJ Code Advance
Custom Text Box - Custom Controls WinForm C # - RJ Code Advance
stepup.edu.vn
https://rjcodeadvance.com/custom-text-box-custom-controls-winform-c/ 1/14
7/22/2021 Custom Text Box - Custom controls WinForm C # - RJ Code Advance
Hi :), this time we will create a custom textbox with C # and Windows Form. And thus have a
text box with a very elegant, flat and modern appearance, with customizable size and border
color, to be able to set an underlined or rectangular border style. Besides being able to set
and change the border color in focus mode, set as password field and multiline.
Doing it is very easy and fast, you don't need to have advanced programming knowledge, so
let's start with the tutorial:
https://rjcodeadvance.com/custom-text-box-custom-controls-winform-c/ 2/14
7/22/2021 Custom Text Box - Custom controls WinForm C # - RJ Code Advance
After having initialized the previous properties, it only remains to add a text box to the user
control and set the Dock property of the text box to FILL , to fit the entire user control.
And finally remove the border from the text box . In this way, the text box remains
centered on the user control, thanks to the Padding established above.
#endregion
// Fields
private Color borderColor = Color. MediumSlateBlue ;
private int borderSize = 2 ;
private bool underlinedStyle = false ;
private Color borderFocusColor = Color. HotPink ;
private bool isFocused = false ;
// Private methods
private void UpdateControlHeight ()
{
if ( textBox1. Multiline == false )
{
int txtHeight = TextRenderer. MeasureText ( "Text" , this . Font ) .
textBox1. Multiline = true ;
textBox1. MinimumSize = new Size ( 0 , txtHeight ) ;
textBox1. Multiline = false ;
https://rjcodeadvance.com/custom-text-box-custom-controls-winform-c/ 4/14
7/22/2021 Custom Text Box - Custom controls WinForm C # - RJ Code Advance
// Properties
[ Category ( "RJ Code Advance" )]
public Color BorderColor
{
get { return borderColor; }
set
{
borderColor = value ;
this . Invalidate () ;
}
}
[ Category ( "RJ Code Advance" )]
public int BorderSize
{
get { return borderSize; }
set
{
borderSize = value ;
this . Invalidate () ;
}
}
{
get { return base . BackColor ; }
set
{
base . BackColor = value ;
textBox1. BackColor = value ;
}
}
It will be necessary to override the OnPaint event method to draw the rectangular or
underlined border of the custom text box, override the OnResize event method to set the
proper height of the control each time it resizes at design time. Finally override the OnLoad
event method to give the final touch and set the proper height at runtime.
// Overridden methods
// Draw border
using ( Pen penBorder = new Pen ( borderColor, borderSize ))
{
penBorder. Alignment = System. Drawing . Drawing2D . PenAlignment . I
if ( isFocused ) penBorder. Color = borderFocusColor ; // Set Border
https://rjcodeadvance.com/custom-text-box-custom-controls-winform-c/ 7/14
7/22/2021 Custom Text Box - Custom controls WinForm C # - RJ Code Advance
{
isFocused = true ;
this . Invalidate () ;
}
Note: To know what the default event of a control is, you just have to double click on the
control and the default event method will be created.
[ DefaultEvent ( "_TextChanged" )]
public partial class RJTextBox: UserControl
{
// Default Event
public event EventHandler _TextChanged;
// TextBox events
/// <summary>
/// textbox events attached to user control events
/// </summary>
https://rjcodeadvance.com/custom-text-box-custom-controls-winform-c/ 8/14
7/22/2021 Custom Text Box - Custom controls WinForm C # - RJ Code Advance
this . OnClick ( e ) ;
}
Well that's it :), I hope you liked it and helped you understand the use of user controls
alongside other controls to make custom controls.
https://rjcodeadvance.com/custom-text-box-custom-controls-winform-c/ 9/14
7/22/2021 Custom Text Box - Custom controls WinForm C # - RJ Code Advance
Leave a reply
Your email address will not be published. Required fields are marked with *
Commentary
Name *
E-mail *
Web
Welcome to blog
Look for …
https://rjcodeadvance.com/custom-text-box-custom-controls-winform-c/ 10/14
7/22/2021 Custom Text Box - Custom controls WinForm C # - RJ Code Advance
Follow me
Category:
.NET
ASP .NET
C#
Mistakes
F#
Visual basic
Windows Forms
Layered Architecture
https://rjcodeadvance.com/custom-text-box-custom-controls-winform-c/ 11/14
7/22/2021 Custom Text Box - Custom controls WinForm C # - RJ Code Advance
Database
MySQL
SQL Server
Custom controls
courses
.NET (Course)
Create .Net Installer
Full Login C #, VB, SQL Server
Software Patterns (Course)
OOP (Course)
Desk
GUI
Software Patterns
OOP
Uncategorized
Web
Recent logins
Circular Picture Box - Border Gradient color + Styles - C # & WinForms
Custom ProgressBar - WinForms & C #
Custom TextBox Full - Rounded & Placeholder - WinForm, C #
Custom ComboBox - Icon, Back, Text & Border Color - WinForm C #
Custom Text Box - Custom controls WinForm C #
Recent comments
gustavo on Custom Button - Custom controls WinForm C #
_Nooker in Modern Form + Font Awesome Icons, WinForm, C # - VB.NET
https://rjcodeadvance.com/custom-text-box-custom-controls-winform-c/ 12/14
7/22/2021 Custom Text Box - Custom controls WinForm C # - RJ Code Advance
https://rjcodeadvance.com/custom-text-box-custom-controls-winform-c/ 13/14
7/22/2021 Custom Text Box - Custom controls WinForm C # - RJ Code Advance
https://rjcodeadvance.com/custom-text-box-custom-controls-winform-c/ 14/14