C# 更改文本框的边框颜色

C# 更改文本框的边框颜色,c#,.net,winforms,textbox,border,C#,.net,Winforms,Textbox,Border,当用户单击或关注文本框时,如何更改其边框颜色?试试这个 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;

当用户单击或关注文本框时,如何更改其边框颜色?

试试这个

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();
}

这是设置文本框边框颜色的最终解决方案:

public class BorderedTextBox : UserControl
{
    TextBox textBox;

    public BorderedTextBox()
    {
        textBox = new TextBox()
        {
            BorderStyle = BorderStyle.FixedSingle,
            Location = new Point(-1, -1),
            Anchor = AnchorStyles.Top | AnchorStyles.Bottom |
                     AnchorStyles.Left | AnchorStyles.Right
        };
        Control container = new ContainerControl()
        {
            Dock = DockStyle.Fill,
            Padding = new Padding(-1)
        };
        container.Controls.Add(textBox);
        this.Controls.Add(container);

        DefaultBorderColor = SystemColors.ControlDark;
        FocusedBorderColor = Color.Red;
        BackColor = DefaultBorderColor;
        Padding = new Padding(1);
        Size = textBox.Size;
    }

    public Color DefaultBorderColor { get; set; }
    public Color FocusedBorderColor { get; set; }

    public override string Text
    {
        get { return textBox.Text; }
        set { textBox.Text = value; }
    }

    protected override void OnEnter(EventArgs e)
    {
        BackColor = FocusedBorderColor;
        base.OnEnter(e);
    }

    protected override void OnLeave(EventArgs e)
    {
        BackColor = DefaultBorderColor;
        base.OnLeave(e);
    }

    protected override void SetBoundsCore(int x, int y,
        int width, int height, BoundsSpecified specified)
    {
        base.SetBoundsCore(x, y, width, textBox.PreferredHeight, specified);
    }
}

WinForms从来都不擅长这一点,这有点痛苦

您可以尝试的一种方法是在面板中嵌入一个文本框,然后根据其中的焦点管理图形:

public class BorderTextBox : Panel {
  private Color _NormalBorderColor = Color.Gray;
  private Color _FocusBorderColor = Color.Blue;

  public TextBox EditBox;

  public BorderTextBox() {
    this.DoubleBuffered = true;
    this.Padding = new Padding(2);

    EditBox = new TextBox();
    EditBox.AutoSize = false;
    EditBox.BorderStyle = BorderStyle.None;
    EditBox.Dock = DockStyle.Fill;
    EditBox.Enter += new EventHandler(EditBox_Refresh);
    EditBox.Leave += new EventHandler(EditBox_Refresh);
    EditBox.Resize += new EventHandler(EditBox_Refresh);
    this.Controls.Add(EditBox);
  }

  private void EditBox_Refresh(object sender, EventArgs e) {
    this.Invalidate();
  }

  protected override void OnPaint(PaintEventArgs e) {
    e.Graphics.Clear(SystemColors.Window);
    using (Pen borderPen = new Pen(this.EditBox.Focused ? _FocusBorderColor : _NormalBorderColor)) {
      e.Graphics.DrawRectangle(borderPen, new Rectangle(0, 0, this.ClientSize.Width - 1, this.ClientSize.Height - 1));
    }
    base.OnPaint(e);
  }
}

使用
OnPaint
在控件上绘制自定义边框是可以的。但要知道如何使用
OnPaint
来保持效率,并将渲染时间降至最低。如果在使用自定义绘制例程时遇到laggy GUI,请阅读以下内容:

因为PraVn的公认答案看似简单,但实际上效率低下。使用自定义控件(如上面答案中的控件)要好得多


在应用程序中,性能可能不是问题,因为它很小,但对于具有大量自定义OnPaint例程的大型应用程序,使用PraVn显示的方式是错误的。

将文本框边框样式设置为“无” 然后将此代码写入容器表单“paint”事件

如果控件具有焦点,则可以处理
TextBox
的消息,并在控件的非客户端区域上绘制边框。可以使用任何颜色绘制边框:

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class ExTextBox : TextBox
{
    [DllImport("user32")]
    private static extern IntPtr GetWindowDC(IntPtr hwnd);
    private const int WM_NCPAINT = 0x85;
    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);
        if (m.Msg == WM_NCPAINT && this.Focused)
        {
            var dc = GetWindowDC(Handle);
            using (Graphics g = Graphics.FromHdc(dc))
            {
                g.DrawRectangle(Pens.Red, 0, 0, Width - 1, Height - 1);
            }
        }
    }
}
结果

控件聚焦时的边框绘制完全不闪烁:

文本框的边框颜色属性

在当前帖子中,我只是更改了焦点上的边框颜色。您还可以向控件添加
BorderColor
属性。然后,您可以在设计时或运行时根据需要更改边框颜色。我发布了一个更完整的
TextBox
版本,它具有
BorderColor
属性: 在以下职位:


难道不是这么简单吗


txtbox1.BorderColor=System.Drawing.Color.Red

谢谢,伙计,它确实有效,但它也改变了表单中所有其他文本框的边框颜色,你能解释一下它是如何发生的,以及如何使颜色变为红色的,它只有蓝色!!我认为上面的代码不会改变所有文本框的边框。我们正在做的是,我们在文本框1周围画一个矩形,如何在边框上应用不同的颜色,或者在矩形周围我对@PraVn代码做了一些更改,现在我认为它很漂亮:int variance=1;g、 DrawRectangle(p,新矩形(textBox1.Location.X-方差,textBox1.Location.Y-方差,textBox1.Width+方差,textBox1.Height+方差));方差=2;g、 DrawRectangle(p,新矩形(textBox1.Location.X-方差,textBox1.Location.Y-方差,textBox1.Width+variance+1,textBox1.Height+variance+1));在性能方面,当父控件中有更多控件时,在OnPaint中执行任何操作都不是一个好主意。要亲自了解我的意思,请执行以下操作:在窗体上放置10个控件,但在
Form1\u Paint
上放置一个断点。现在,在表单1上绘制的每个控件的
textBox1
周围绘制边框。最好创建一个从TextBox继承的自定义控件,以确保在需要时只绘制一次边框。而不是绘制大量不必要的时间,并大幅增加渲染时间。特别是在有更多自定义的onpaint作业的情况下。我们可以让这个文本框多行吗?如果您正在寻找一个
textbox
具有
BorderColor
属性的文本框,请看一看,您可能应该先测试一下,然后再提出建议。虽然此代码可以回答问题,提供关于如何和/或为什么解决问题的附加上下文将提高答案的长期价值。
With PictureBox1
    .Visible = False
    .Width = TextBox1.Width + 4
    .Height = TextBox1.Height + 4
    .Left = TextBox1.Left - 2
    .Top = TextBox1.Top - 2
    .SendToBack()
    .Visible = True
End With
With PictureBox1
    .Visible = False
    .Width = TextBox1.Width + 4
    .Height = TextBox1.Height + 4
    .Left = TextBox1.Left - 2
    .Top = TextBox1.Top - 2
    .SendToBack()
    .Visible = True
End With