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

  • SEARCH
  • Home
  • 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 6601189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:43:14+00:00 2026-05-25T18:43:14+00:00

I will tell my requirement. I need to have a keydown event for each

  • 0

I will tell my requirement. I need to have a keydown event for each control in the Windows Forms form. It’s better to do so rather than manually doing it for all controls if what I have to do for all keydown events is the same.

So I could basically do this:

foreach (Control c in this.Controls)
    c.KeyDown+= new KeyEventHandler(c_KeyDown);

But here, the foreach doesn’t loop inside those controls which reside inside a groupBox or a tabControl. I mean if the form (this) contains a groupBox or some other container control, then I can get a keydown event for that particular container control. And the foreach doesn’t loop through controls that reside inside that container control.

Question 1: How do I get a keydown event for "all" the controls in a form?

If the above puzzle is solved, then my problem is over.

This is what I can otherwise do:

Mainly pseudo code

foreach (Control c in this.Controls)
{
     c.KeyDown += new KeyEventHandler(c_KeyDown);

     if (c is Container control)
           FunctionWhichGeneratesKeyDownForAllItsChildControls(c)
}

I know I will have to go through FunctionWhichGeneratesKeyDownForAllItsChildControls(c) many times over to get keydown for all controls if there are groupboxes inside a groupbox or so. I can do it. My question is,

Question 2: How do I check if c is a container control?

  • 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-25T18:43:15+00:00Added an answer on May 25, 2026 at 6:43 pm

    A simple recursive function should do it.

    private void AddEvent(Control parentCtrl)
    {
      foreach (Control c in parentCtrl.Controls)
      {
        c.KeyDown += new KeyEventHandler(c_KeyDown);
        AddEvent(c);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to write a java program which will tell me whether I have internet
In my web-application I have a the following requirement. I need to have a
Basically I will tell you what I want to do. 1: If a user
The TLDR version: Bulk Insert will tell you how many rows it affected. It
Does anyone know of a resource that will tell me the sequence of locks
I'm looking for a tool that will tell/resolve for every function all the call
How can I make a helper that will tell me how many weeks ago
I am looking for a function that will tell me, for a list of
I'm looking for a very simple script that will tell me if one twitter
Could somebody please explain or link to a resource that will tell me why

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.