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 9190583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:29:45+00:00 2026-06-17T20:29:45+00:00

My question is simply: how can I shorten this code even more? List<Button> buttonList

  • 0

My question is simply: how can I shorten this code even more?

 List<Button> buttonList = new List<Button>();

        buttonList.Add(button1);
        buttonList.Add(button2);
        buttonList.Add(button3);
        buttonList.Add(button4);
        buttonList.Add(button5);
        buttonList.Add(button6);
        buttonList.Add(button7);
        buttonList.Add(button8);
        buttonList.Add(button9);
  • 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-06-17T20:29:46+00:00Added an answer on June 17, 2026 at 8:29 pm

    If you’ve got your buttons stored in some control’s Controls collection (say, a Form or a Panel), then you could use:

    //here `this` is supposed to be a `Form`
    List<Button> buttonList = this.Controls.OfType<Button>() 
                                           .OrderBy(b=>b.Name)
                                           .ToList();
    

    In this case you may have your method, returning an enumerable of buttons, stored within some container control:

    public IEnumerable<Button> GetButtons()
    {
        return this.Controls.OfType<Button>().OrderBy(b => b.Name);
        //return this.panel1.Controls.OfType<Button>().OrderBy(b => b.Name);
        //actually any container control you're having your buttons within
    }
    

    and you may use it:

    foreach(Button b in GetButtons())
    {
        //...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Simple syntactic c# question is this. Given this code: List<string> Columns = new List<string>();
My question can be simply illustrated by this code: def proceed(self, *args): myname =
I'm new to this whole Template Metaprogramming in C++ mess and I simply can't
I know this is an elementary question but I simply can't find an answer.
This might be an extremely dumb question, but I simply can't understand the problem
This is purely a math question! I simply can't get my head around this
My question is simply how can i do a dialog pop up from other
A quite dumb question but i simply can't find the answer. I have a
Apologies if this is a simple question, I'm still pretty new to this, but
I don't know if this question is already answered on stackoverflow. But I simply

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.