Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

Your profile

Search

November 2008
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

XML Feeds

« Foundations of Programming EbookSolution folders are not being sorted alphabeticaly in Visual studio 2008 »
The Desktop Developers Journal

C# Sorting winforms controls using linq

by chrissie1


Permalink 24 Jun 2008 06:48 , Categories: Microsoft Technologies, C#

Use the power of linq

Create a form with some controls on it and a textbox called textbox1.

then but this in the load event of the form

  1. textBox1.Clear();
  2.             IEnumerable<Control> query = from p in this.Controls.OfType<Control>() orderby p.TabIndex select p;
  3.             foreach (Control c in query)
  4.             {
  5.                 textBox1.AppendText(c.TabIndex + " " + c.Name + Environment.NewLine);    
  6.             }

and you probably need these usings.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Forms;

At first I couldn’t get it to work until I found this LINQ - Query Windows Forms Controls
by Sam Allen
and then the light came. I was missing the TypeOf method. This is really cool. No more reflection needed to get that control you realy need.

Leave a comment »Send a trackback » 1414 views

Trackback address for this post

Trackback URL (right click and copy shortcut/link location)

No feedback yet

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
PoorExcellent
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)