Share via


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.

Comments