0

I am working on a GUI using Windows Forms.

When the value entered in a TextBox does not fall into a specified range, it is treated as an invalid parameter and a red border should appear around the corresponding TextBox.

I have many such TextBoxes in the Form. Is there any straightforward way of doing this?

Jimi
  • 29,621
  • 8
  • 43
  • 61
  • Did you have a look at the official Microsoft documentation: https://learn.microsoft.com/en-us/dotnet/desktop/winforms/input-keyboard/validation?view=netdesktop-5.0 – DeMaki Jul 29 '21 at 15:06
  • Where is this requirement coming from? In WinForms the traditional way of indicating validation errors is the [`ErrorProvider`](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.errorprovider) component, which is a blinking icon with a tool tip (supports data binding, too). Additionally, you can use the `BackColor` to indicate ([validation severity](https://github.com/koszeggy/KGySoft.ComponentModelDemo)). But unlike in WPF, WinForms does not have an adorner layer so drawing anything around/above the controls without affecting the layout is a way too complicated issue. – György Kőszeg Jul 29 '21 at 15:45
  • Changing the border color of a TextBox is pretty simple; you need to derive a Custom Control from TextBox, trap WM_PAINT messages and draw your border. You can do that in different ways. This is pretty simple: [Change Border Color of NumericUpDown](https://stackoverflow.com/a/65880818/7444103). Replace `public class NumericUpDownEx : NumericUpDown` with, e.g., `public class TextBoxEx : TextBox` and that's it. Set different colors and styles using the PropertyGrid as usual. -- You can add custom validation logic to change the border color as needed. – Jimi Jul 30 '21 at 04:31

0 Answers0