Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6856771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:55:41+00:00 2026-05-27T01:55:41+00:00

I have a WinForms app that contains many NumericUpDown controls. In a nutshell, if

  • 0

I have a WinForms app that contains many NumericUpDown controls. In a nutshell, if my users enter a value into the control and then delete the text, I want to restore it (the text) when the control loses focus. So I decided that I’d check .Text when the control loses focus and if it’s empty, I set .Text = .Value.ToString().

I’m doing this in the Leave event handler and it works just fine. But as I said, I have many of these controls (18, to be exact). I don’t like creating 18 Leave event handlers that all do the same thing so I created a generic one like this:

private void numericUpDown_GenericLeave(object sender, EventArgs e)
{
    if (string.IsNullOrEmpty(((NumericUpDown)sender).Text))
        ((NumericUpDown)sender).Text = ((NumericUpDown)sender).Value.ToString();
}

I started to hook up all of the controls to this generic event handler but I quickly got tired of doing this:

numericUpDown1.Leave += numericUpDown_GenericLeave;
numericUpDown2.Leave += numericUpDown_GenericLeave;
numericUpDown3.Leave += numericUpDown_GenericLeave;
...
numericUpDown18.Leave += numericUpDown_GenericLeave;

So I thought I’d create a function that would return a list of all the controls of a specified type and then loop through that list and hookup the event handlers. That function looks like this:

public static List<Control> GetControlsOfSpecificType(Control container, Type type)
{
    var controls = new List<Control>();

    foreach (Control ctrl in container.Controls)
    {
        if (ctrl.GetType() == type)
            controls.Add(ctrl);

        controls.AddRange(GetControlsOfSpecificType(ctrl, type));
    }

    return controls;
}

I call the function like this:

var listOfControls = GetControlsOfSpecificType(this, typeof(NumericUpDown));

foreach (var numericUpDownControl in listOfControls)
{
    numericUpDownControl.Leave += numericUpDown_GenericLeave;
}

When I run my app, however, I don’t see the expected behavior that occurs when I manually hookup each control to the generic event handler. This code is currently in the constructor of my form and I’ve tried calling it before as well as after the call to InitializeComponent() but neither one seems to be working. I get no error of any kind, I just don’t see the behavior that I was expecting. I have a breakpoint set inside the generic event handler but the debugger never breaks so it seems like the event handler isn’t being hooked up correctly. Does anyone know why this might be or how I can troubleshoot it further? Thanks!

EDIT

I just realized that the call to:

var listOfControls = GetControlsOfSpecificType(this, typeof(NumericUpDown));

was happening before the call to InitializeComponent() so of course the list of controls being returned was empty. DOH! Thanks for all the replys. I apologize for wasting everyones time. 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:55:42+00:00Added an answer on May 27, 2026 at 1:55 am

    Question answered. See Edit above. Thanks to bsegraves for pointing me in the right direction.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WinForms app that contains several comboboxes, numericupdown controls and checkboxes. I
Please advise me. Winforms app, C#. I have a user control (UC) that contains
We have a WinForms app that runs fine on x86, but has many third-party
I have a DataGridView control in a winforms app that I'm working on. The
i have a c# winform app that contains a listview control. i would like
In my winforms app, I have a list view control that is populated with
I have a Winforms app that has a DataGridView that is databound at runtime.
I have a winforms app that checks user credentials as it starts. If autharization
So we have a winforms app that in the task manager should anywhere from
I have a small WinForms app that utilizes a BackgroundWorker object to perform a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.