This question has already been answered:

How to make TextBox with rounded corners in WinForms? Tried by analogy as in the example with button. No errors, the corners do not round out. With other controls it turns out. What's wrong?

public class RoundTextBox : TextBox { public Color BackColor2 { get; set; } public Color TextBoxBorderColor { get; set; } public int TextBoxRoundRadius { get; set; } public Color TextBoxHighlightColor { get; set; } public Color TextBoxHighlightColor2 { get; set; } public Color TextBoxHighlightForeColor { get; set; } public Color TextBoxPressedColor { get; set; } public Color TextBoxPressedColor2 { get; set; } public Color TextBoxPressedForeColor { get; set; } private bool IsHighlighted; private bool IsPressed; public RoundTextBox() { Size = new Size(100, 40); TextBoxRoundRadius = 30; BackColor = Color.Gainsboro; BackColor2 = Color.Silver; TextBoxBorderColor = Color.Black; TextBoxHighlightColor = Color.Orange; TextBoxHighlightColor2 = Color.OrangeRed; TextBoxHighlightForeColor = Color.Black; TextBoxPressedColor = Color.Red; TextBoxPressedColor2 = Color.Maroon; TextBoxPressedForeColor = Color.White; } protected override CreateParams CreateParams { get { CreateParams createParams = base.CreateParams; createParams.ExStyle |= 0x00000020; // WS_EX_TRANSPARENT return createParams; } } protected override void OnPaint(PaintEventArgs e) { e.Graphics.SmoothingMode = SmoothingMode.HighQuality; var foreColor = IsPressed ? TextBoxPressedForeColor : IsHighlighted ? TextBoxHighlightForeColor : ForeColor; var backColor = IsPressed ? TextBoxPressedColor : IsHighlighted ? TextBoxHighlightColor : BackColor; var backColor2 = IsPressed ? TextBoxPressedColor2 : IsHighlighted ? TextBoxHighlightColor2 : BackColor2; using (var pen = new Pen(TextBoxBorderColor, 1)) e.Graphics.DrawPath(pen, Path); using (var brush = new LinearGradientBrush(ClientRectangle, backColor, backColor2, LinearGradientMode.Vertical)) e.Graphics.FillPath(brush, Path); using (var brush = new SolidBrush(foreColor)) { var sf = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center }; var rect = ClientRectangle; rect.Inflate(-4, -4); e.Graphics.DrawString(Text, Font, brush, rect, sf); } base.OnPaint(e); } protected override void OnPaintBackground(PaintEventArgs e) { } protected override void OnMouseEnter(EventArgs e) { base.OnMouseEnter(e); IsHighlighted = true; Parent.Invalidate(Bounds, false); Invalidate(); } protected override void OnMouseLeave(EventArgs e) { base.OnMouseLeave(e); IsHighlighted = false; IsPressed = false; Parent.Invalidate(Bounds, false); Invalidate(); } protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); Parent.Invalidate(Bounds, false); Invalidate(); IsPressed = true; } protected override void OnMouseUp(MouseEventArgs e) { base.OnMouseUp(e); Parent.Invalidate(Bounds, false); Invalidate(); IsPressed = false; } protected GraphicsPath Path { get { var rect = ClientRectangle; rect.Inflate(-1, -1); return GetRoundedRectangle(rect, TextBoxRoundRadius); } } public static GraphicsPath GetRoundedRectangle(Rectangle rect, int d) { var gp = new GraphicsPath(); gp.AddArc(rect.X, rect.Y, d, d, 180, 90); gp.AddArc(rect.X + rect.Width - d, rect.Y, d, d, 270, 90); gp.AddArc(rect.X + rect.Width - d, rect.Y + rect.Height - d, d, d, 0, 90); gp.AddArc(rect.X, rect.Y + rect.Height - d, d, d, 90, 90); gp.CloseFigure(); return gp; } } 

Reported as a duplicate by members pavel , user194374, aleksandr barakin , Bald , Denis on Dec 13 '16 at 6:42 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • You have already asked and received an answer to this question? - koks_rs
  • @koks_rs here! About this question about the button, I said in my answer) - xSx
  • one
    My advice to you: you and your users don't need all these rounded buttons and other controls. Look, for example, at this site and find at least one such element. Draw conclusions. - Alexander Petrov
  • All designers to send to this site? - Alexander Puzanov
  • one
    Most send to the bioreactor. - Alexander Petrov

2 answers 2

You can use the Graphics path, Calling after initializing the components TextBoxElipse (textbox1);

  void TextBoxElipse(TextBox t) { System.Drawing.Drawing2D.GraphicsPath gPath = new System.Drawing.Drawing2D.GraphicsPath(); gPath.AddEllipse(0, 0, t.Width, t.Height); t.Region = new Region(gPath); gPath.Dispose(); } 

But ugly happened

enter image description here

If you add checks, add the necessary spaces and the like, you can make it beautifully.

If I’m too lazy, I advise you to search for any other control, for example, the rounding of the button was recently a question , they answered him very qualitatively, you might take the logic from there.

  • How to do it using the AddArc method so that the corners are more beautifully rounded? - Alexander Puzanov
 using System; using System.Windows.Forms; using System.Runtime.InteropServices; namespace WindowsFormsApplication1 { public partial class Form1 : Form { [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")] private static extern IntPtr CreateRoundRectRgn ( int nLeftRect, // x-coordinate of upper-left corner int nTopRect, // y-coordinate of upper-left corner int nRightRect, // x-coordinate of lower-right corner int nBottomRect, // y-coordinate of lower-right corner int nWidthEllipse, // height of ellipse int nHeightEllipse // width of ellipse ); public Form1() { InitializeComponent(); this.FormBorderStyle = FormBorderStyle.None; Region = System.Drawing.Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20)); } } } 

Look here .

  • 2
    Is WF worth it to give up WPF?) - isnullxbh
  • I wildly apologize for the ignorance in WPF, but the grid killed me there when I tried to programmatically insert into the column-combobox, I had to do, as in 1c ^^ when I clicked to open a separate window for editing the record. So, what I think, the author decided to leave it for good reason)) If you do not need to frequently change the graphics and load it dynamically, it is very reasonable. - xSx