Change Border Color in Textbox C#

Change the borderColor of the TextBox

try this

bool focus = false;
private void Form1_Paint(object sender, PaintEventArgs e)
{
if (focus)
{
textBox1.BorderStyle = BorderStyle.None;
Pen p = new Pen(Color.Red);
Graphics g = e.Graphics;
int variance = 3;
g.DrawRectangle(p, new Rectangle(textBox1.Location.X - variance, textBox1.Location.Y - variance, textBox1.Width + variance, textBox1.Height +variance ));
}
else
{
textBox1.BorderStyle = BorderStyle.FixedSingle;
}
}

private void textBox1_Enter(object sender, EventArgs e)
{
focus = true;
this.Refresh();
}

private void textBox1_Leave(object sender, EventArgs e)
{
focus = false;
this.Refresh();
}

Change the border color of textbox in c# winforms - Newbie

Try creating your own textBox using the UserControl. Here is a sample that will get you started:

public partial class UserControl1 : UserControl
{
private string text;

public string Text

{
get { return textBox.Text; }

set { textBox.Text = value; }
}

TextBox textBox = new TextBox();

public UserControl1()

{
InitializeComponent();

this.Paint += new PaintEventHandler(UserControl1_Paint);

this.Resize += new EventHandler(UserControl1_Resize);

textBox.Multiline = true;

textBox.BorderStyle = BorderStyle.None;

this.Controls.Add(textBox);
}

private void UserControl1_Resize(object sender, EventArgs e)

{
textBox.Size = new Size(this.Width - 3, this.Height - 2);

textBox.Location = new Point(2, 1);
}

private void UserControl1_Paint(object sender, PaintEventArgs e)

{
ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, Color.Red, ButtonBorderStyle.Solid);
}
}

Change border color in TextBox C#

You have to draw text manually as well.

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Pen penBorder = new Pen(Color.Gray, 1);
Rectangle rectBorder = new Rectangle(e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width - 1, e.ClipRectangle.Height - 1);
e.Graphics.DrawRectangle(penBorder, rectBorder);

Rectangle textRec = new Rectangle(e.ClipRectangle.X + 1, e.ClipRectangle.Y + 1, e.ClipRectangle.Width - 1, e.ClipRectangle.Height - 1);
TextRenderer.DrawText(e.Graphics, Text, this.Font, textRec, this.ForeColor, this.BackColor, TextFormatFlags.Default);
}

Alternatively you can try to use e.Graphics.DrawString() method if TextRenderer is not giving you desired results (I always have better results with this approach thou).

How to change TextBox border color and Style while typing?

First of all, whenever you ask something in SO, you have to put some effort and show what you´ve tried so that people will be willing to help you. Take that as a note for future questions.

Having said that, you have to add a handler to the TextChanged event of your TextBox control. Then you have to change the properties as you desire:

private void textBox1_TextChanged(object sender, EventArgs e)
{
//PUT THE BUSINESS LOGIC IN HERE
if(textBox1.Text = "")
{
textBox1.BorderStyle = BorderStyle.None;
Pen p = new Pen(Color.Red);
Graphics g = e.Graphics;
int variance = 3;
g.DrawRectangle(p, new Rectangle(textBox1.Location.X - variance, textBox1.Location.Y - variance, textBox1.Width + variance, textBox1.Height +variance ));
}
else
{
textBox1.BorderStyle = BorderStyle.FixedSingle;
}
}

Customizing Borders on a TextBox

I was able to create a design near the one in your image but not in the way you want it.

Here's the one I managed to make:

Search

What I used:

  • textbox for the search input
  • label for clearing the input,
  • line shape (found in Visual Basic PowerPacks in the toolbox) for the border effect
    OR
  • another label having a long underscore ( _ ).
  • and a picturebox for the search icon

Procedure:

For the textbox, set these properties:

  • BorderStyle : None
  • BackColor: 0, 188, 212 (or the color of your form's background) but that's the exact color based on the image you provided
  • ForeColor: White

For the clear button, I just used a label (it still has a click event), set the text to: "✖" and the BackColor to Transparent.

For the border effect, just draw a line shape below the textbox then set:

  • BorderColor: White
  • BorderWidth to 3

If you are using the label with underscores, just place it under the textbox.

Sorry got bored that I even included everything even though you're just asking for the border (still wondering why did I answer this on the first place). I hope this can still be of any help.

Cannot change textbox border color on button click

private void registracija_Btn_Click(object sender, EventArgs e) 
{
.
.
.
errorProvider2.SetError(RegistracijaUporabnisko_txt, "Username already exists!");
RegistracijaUporabnisko_txt.Invalidate();
RegistracijaUporabnisko_txt.Border.Color = Color.Red;
RegistracijaUporabnisko_txt.Border.Thickness = 3;
.
.
.
}

RegistracijaUporabnisko_txt.Invalidate(); - Solution! Thanks to DonBoitnott



Related Topics



Leave a reply



Submit