Is that you need?
ProcessTextBoxes(this, true, (textbox) =>
{
if ( !textbox.Focused && textbox.Text.IsNumeric() )
textbox.Text = String.Format("{0:n}", textbox.Text);
});
private void ProcessTextBoxes(Control control, bool recurse, Action<TextBox> action)
{
if ( !recurse )
Controls.OfType<TextBox>().ToList().ForEach(c => action?.Invoke(c));
else
foreach ( Control item in control.Controls )
{
if ( item is TextBox )
action.Invoke((TextBox)item);
else
if ( item.Controls.Count > 0 )
ProcessTextBoxes(item, recurse);
}
}
You can adapt this code and pass this
for the form or any container like a panel and use recursivity or not to process all inners.
Also, you can do that on each Leave event and assign one to all needed:
private void TextBox_Leave(object sender, EventArgs e)
{
var textbox = sender as TextBox;
if ( textbox == null ) return;
if ( textbox.Text.IsNumeric() )
textbox.Text = String.Format("{0:n}", textbox.Text);
}
private void InitializeTextBoxes(Control control, bool recurse)
{
if ( !recurse )
Controls.OfType<TextBox>().ToList().ForEach(c => c.Leave += TextBox_Leave);
else
foreach ( Control item in control.Controls )
{
if ( item is TextBox )
item.Leave += TextBox_Leave;
else
if ( item.Controls.Count > 0 )
InitializeTextBoxes(item, recurse);
}
}
public FormTest()
{
InitializeComponent();
InitializeTextBoxes(EditPanel, true);
}