- This topic helps to draw border to any controls in C#.
- Here we show how to draw border around the TextBox control in C#.
C#.NET – Draw border around any controls in C#
- C# is an Object Oriented Programming language from Microsoft that mainly combines the computing power of C++ with the programming ease of Visual Basic.
- C# is mainly based on C++.
- The most recent version is C#4.0.
- C# language is simple, modern, general-purpose, object-oriented language.
Graphics Class – Draw border around any controls in C#
- Graphics class encapsulates a GDI+ drawing surface.
- This class cannot be inherited.
Namespace: System.Drawing
Assembly: System.Drawing (in System.Drawing.dll)
- Syntax for Graphics Class
|
1 2 |
public sealed class Graphics : MarshalByRefObject, IDeviceContext, IDisposable |
Draw border around the Textbox control or any controls in C# – Working
- First you have to add a TextBox control to the form for drawing border around the TextBox in C#.
- Then we draw border around the TextBox control using graphics class, this is done by drawing rectangle around the border of the TextBox controls in C#.
Draw border around the Textbox control or any controls in C# – Complete Code
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Paint(object sender, PaintEventArgs e) { using (Graphics g = e.Graphics) { using (Pen pen = new Pen(Color.Red, 2)) { foreach (Control ctr in this.Controls) { if (ctr is TextBox) { g.DrawRectangle(pen, new Rectangle(ctr.Location, ctr.Size)); } } } } } } } |
Note: Draw borders around other controls, just change name ‘TextBox’
If you have any doubts in how to create border around any controls in C#, please feel free to contact us… our mail id: codingangel@gmail.com
Draw border around any controls in C#






