How to: Access Keyed Collections in Windows Forms

The key associated with an item in a collection is typically the name of the item. The following procedures show you how to use collection classes to perform common tasks.

To find and give focus to a nested control in a control collection

  • Use the Find and Focus methods to specify the name of the control to find and give focus to.

    OrderForm OrderForm1 = new OrderForm();
    OrderForm1.Show();
    OrderForm1.Controls.Find("textBox1", true)[0].Focus();
    
    Dim OrderForm1 As New OrderForm()
    OrderForm1.Show()
    OrderForm1.Controls.Find("textBox1", True)(0).Focus()
    

To access an image in an image collection

  • Use the Item[Int32] property to specify the name of the image you want to access.

    this.BackgroundImage = imageList1.Images["logo.gif"];
    
    Me.BackgroundImage = imageList1.Images("logo.gif")
    

To set a tab page as the selected tab

  • Use the SelectedTab property together with the Item[String] property to specify the name of the tab page to set as the selected tab.

    tabControl1.SelectedTab = tabControl1.TabPages["shippingOptions"];
    
    tabControl1.SelectedTab = tabControl1.TabPages("shippingOptions")
    

See Also

Getting Started with Windows Forms
How to: Add or Remove Images with the Windows Forms ImageList Component