How can I change the BorderColor of the Textbox when a user Clicks on it or focuses on it?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Carriage Return (ASCII chr 13) is missing from tex
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
You can handle
WM_NCPAINT
message ofTextBox
and draw a border on the non-client area of control if the control has focus. You can use any color to draw border:Result
The painting of borders while the control is focused is completely flicker-free:
Note
In the current post I just change the border color on focus. You can also add a
BorderColor
property to the control. Then you can change border-color based on your requirement at design-time or run-time. Here I posted a more completed version ofTextBox
which hasBorderColor
property:WinForms was never good at this and it's a bit of a pain.
One way you can try is by embedding a TextBox in a Panel and then manage the drawing based on focus from there:
This is an ultimate solution to set the border color of a TextBox:
Isn't it Simple as this,
txtbox1.BorderColor = System.Drawing.Color.Red;
try this
Using
OnPaint
to draw a custom border on your controls is fine. But know how to useOnPaint
to keep efficiency up, and render time to a minimum. Read this if you are experiencing a laggy GUI while using custom paint routines: What is the right way to use OnPaint in .Net applications?Because the accepted answer of PraVn may seem simple, but is actually inefficient. Using a custom control, like the ones posted in the answers above is way better.
Maybe the performance is not an issue in your application, because it is small, but for larger applications with a lot of custom OnPaint routines it is a wrong approach to use the way PraVn showed.