更改TextBox的borderColor

Rag*_*tha 21 c# textbox border winforms

当用户点击文本框或关注它时,如何更改文本框的BorderColor?

Rez*_*aei 30

如果控件具有焦点,您可以处理非客户控制区域的WM_NCPAINT消息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);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

结果

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

在焦点上更改TextBox边框颜色

注意

在当前帖子中,我只是更改焦点上的边框颜色.您还可以BorderColor向控件添加属性.然后,您可以在设计时或运行时根据您的要求更改边框颜色.这里我发布了一个更完整的版本,TextBox其中包含以下BorderColor属性:

更改文本框边框颜色


Pra*_*enu 15

试试这个

        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();
        }
Run Code Online (Sandbox Code Playgroud)

  • 关于性能,在OnPaint中完成任何操作,在父控件中有更多控件时,这是一个坏主意.要亲自了解我的意思,请执行以下操作:在表单上放置10个控件,但在"Form1_Paint"上放置一个断点.现在,对于在Form1上绘制的每个控件,边框都围绕`textBox1`绘制.最好创建一个继承自TextBox的自定义控件,以确保在需要时仅绘制一次边框.而不是大量抽取不必要的时间,并且大大增加了渲染时间.特别是有更多定制的onpaint工作喜欢这个. (3认同)

Bal*_*nyi 10

这是设置TextBox边框颜色的终极解决方案:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)


Lar*_*ech 5

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

您可以尝试的一种方法是在Panel中嵌入TextBox,然后根据焦点管理绘图:

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);
  }
}
Run Code Online (Sandbox Code Playgroud)