-1

Max length for a textbox input value can be decided from the MaxLength property. But we need to do this for each textbox one by one.

Is there any way to set the value of MaxLength property once for all textboxes on a form?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Rashid Ali
  • 587
  • 3
  • 13

2 Answers2

1

You may do it in code in On_Loaded event. Query all controls on the form and setup MaxLength. You may also create custom control based on TextBox with predefined MaxLength and use it instead of standard TextBox.

How to get ALL child controls of a Windows Forms form of a specific type (Button/Textbox)?

opewix
  • 4,993
  • 1
  • 20
  • 42
0

Did it with recursive calls because Form have panels and textboxes are inside panels.

Here is the extension method to get all TextBoxes on a Form:

public static IEnumerable<TControl> GetChildControls<TControl>(this Control control) where TControl : Control
       {
           var children = (control.Controls != null) ? control.Controls.OfType<TControl>() : Enumerable.Empty<TControl>();
           return children.SelectMany(c => GetChildControls<TControl>(c)).Concat(children);
       }
Rashid Ali
  • 587
  • 3
  • 13