Welcome to MSDN Blogs Sign in | Join | Help

jfo's coding

adventures in windows forms and wpf
A different kind of key altogether: keyed collections

In most of the commonly used collections in Windows Forms 2.0, you'll find that they also take a string in addition to an integer index.  The string corresponds to the "Name" property on the corresponding item. 

    this.button1 = new System.Windows.Forms.Button();
   this.button1.Name = "button1";
   panel1.Controls.Add(button1);
  

   panel1.Controls["button1"].BackColor = Color.Blue;

Note: this lookup is slightly less efficient than using an index directly - so if you have it use it!

Additionally, some collections now have a "Find" method, the string representing the name and the boolean representing whether or not to "search all children".

   panel1.Controls.Find("button1", true)

Or perhaps more interestingly:

       this.menuStrip1.Items.Find("copyToolStripMenuItem", true)

So there you go, perhaps not as interesting as all the fancy new collections in System.Collections.Generic - but occasionally super handy.

 

Posted: Thursday, February 02, 2006 12:18 AM by jfoscoding
Filed under:

Comments

jason kemp .ca said:

# April 14, 2006 3:26 PM
New Comments to this post are disabled
Page view tracker