+92 332 4229 857 99ProjectIdeas@Gmail.com

Counting Buttons In Your Form (C#.net)

Counting Buttons In Your Form (C#.net):
The following code shows you counting all the buttons in your form and displaying their text written on them in listbox.


using System.Linq;

    IEnumerable<Control> query =
         from Control buttons in this.Controls
         where (buttons is Button)
         orderby buttons.Text
         select buttons;

         int buttonCount = query.Count(); // counting buttons
         label1.Text = "The following " + buttonCount.ToString() + " buttons are on this form and text written on them is :";
         foreach (Button b in query)
            listBox1.Items.Add(b.Text); // adding buttonText in ListBox