2012-03-19 114 views
17

如何在用戶點擊或關注文本框時更改文本框的BorderColor?更改文本框的borderColor

+0

如果你正在尋找一個'TextBox'有'BorderColor'財產,看看[在文本框更改邊框顏色(HTTP ://stackoverflow.com/a/39420512/3110834) – 2017-04-13 16:41:30

回答

12

試試這個

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

感謝的人,它的工作,但它也改變了表格中的所有其他文本框的邊框顏色也一樣,你能解釋一下它是如何發生的,以及如何使紅色,它只有藍色! – 2012-03-20 04:13:28

+0

我不認爲上面的代碼會改變所有文本框的邊界。我們正在做的是,我們圍繞textBox1畫一個矩形。 – PraveenVenu 2012-03-20 04:17:45

+0

如何在邊框上應用不同的顏色,或圍繞矩形 – 2012-03-20 06:41:02

9

這是一個最終的解決方案設置文本框的邊框顏色:

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); 
    } 
} 
+0

我們可以讓這個文本框多行嗎? – 2015-04-04 11:13:41

5

的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); 
    } 
} 
0

使用OnPaint畫上你控制一個自定義邊框的罰款。但知道如何使用OnPaint來保持效率,並將時間降至最低。如果您在使用自定義繪圖程序時遇到延遲GUI,請閱讀以下內容:What is the right way to use OnPaint in .Net applications?

因爲PraVn的接受答案可能看起來很簡單,但實際上效率不高。使用自定義控件,就像上面答案中發佈的控件一樣好。

也許性能在您的應用程序中不是問題,因爲它很小,但對於具有大量自定義OnPaint例程的較大應用程序,使用PraVn所顯示的方式是錯誤的方法。

0

集文本框的邊框風格,無 然後編寫代碼,以集裝箱形式的「漆」事件

private void Form1_Paint(object sender, PaintEventArgs e) 
     { 
System.Drawing.Rectangle rect = new Rectangle(TextBox1.Location.X, TextBox1.Location.Y, TextBox1.ClientSize.Width, TextBox1.ClientSize.Height); 

       rect.Inflate(1, 1); // border thickness 
       System.Windows.Forms.ControlPaint.DrawBorder(e.Graphics, rect, Color.DeepSkyBlue, ButtonBorderStyle.Solid); 

} 
17

您可以處理的TextBoxWM_NCPAINT信息和控制的,如果非客戶區繪製一個邊框控制有重點。你可以使用任何顏色來繪製邊框:

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

結果

而控制的重點是邊界的畫是完全無閃爍:

Change TextBox border color on focus

注意

在當前文章中我只是改變焦點上的邊框顏色。您還可以將BorderColor屬性添加到控件。然後,您可以在設計時或運行時根據您的要求更改邊框顏色。 Here我張貼的TextBox一個更完整的版本具有BorderColor屬性:

Change Textbox border color