Keywords: C# | .NET | WinForms | TextBox | border color
Abstract: This article explores a method to change the border color of a TextBox control in WinForms when it gains or loses focus. Based on the best answer, it details code implementation with event handling and custom drawing, supplemented by alternative technical approaches.
In WinForms application development, the default border color of a TextBox control remains unchanged during focus transitions, limiting UI customization. To enhance user interaction, developers often need to dynamically adjust border colors to indicate focus state. This article delves into a widely adopted solution, explaining how to achieve this through event-driven programming and graphical drawing.
Core Implementation Method
The primary approach uses the Enter and Leave events to monitor focus changes in the TextBox, and draws a custom border in the Form_Paint event based on the focus state. This method avoids directly modifying control properties, instead overlaying the original border with drawing operations for greater visual flexibility.
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();
}
In the code above, a boolean variable focus tracks the focus state. When the TextBox gains focus, the Enter event sets focus to true and calls Refresh() to trigger repainting; when focus is lost, the Leave event sets it to false. In the Form_Paint event, if focus is true, the default border is hidden and a red border is drawn using Graphics.DrawRectangle; otherwise, the default border style is restored. This method is straightforward but requires performance considerations, such as minimizing frequent repaints.
Reference to Other Technical Solutions
Beyond the event-based method, two alternative solutions are available for reference. The first involves handling the Windows message WM_NCPAINT by overriding the WndProc method to draw borders in the non-client area, achieving flicker-free effects. Sample code includes platform invocation (P/Invoke) and message handling, suitable for advanced scenarios. The second is creating a custom control BorderedTextBox, inheriting from UserControl and encapsulating a BorderColor property for design-time and runtime flexibility. Both approaches have pros and cons, and developers can choose based on project needs.
Implementation Details and Optimization Tips
During implementation, key points must be noted. First, coordinate calculations for drawing should ensure borders align with the control to avoid offsets or overlaps. Second, color selection should consider user experience, such as using high-contrast colors to highlight focus. Additionally, for performance improvement, repaint frequency can be limited or double-buffering techniques applied. The variance parameter in the code sample adjusts border positioning, which developers can tweak to fit different control sizes.
Conclusion
By combining event handling and graphical drawing, developers can flexibly control the border color of TextBox controls, enhancing user interaction. The method presented here is based on best practices and easily integrable into existing WinForms projects. Future explorations may include supporting more border styles or extending to other control types.