Nasıl yapılır: Çalışma Zamanında bir Denetimler Koleksiyonuna Ekleme veya Kaldırma

Uygulama geliştirmede yaygın görevler, formlar (veya denetimi, hatta formun kendisi gibi) üzerinde denetimler eklemek ve bu denetimlerden denetimleri PanelGroupBox kaldırmaktır. Tasarım zamanında denetimler doğrudan bir panele veya grup kutusuna sürüklenebilirsiniz. Çalışma zamanında, bu denetimler üzerinde hangi denetimlerin yerleştiril olduğunu takip Controls eden bir koleksiyon tutar.

Not

Aşağıdaki kod örneği, içinde bir denetim koleksiyonu bulunduran tüm denetimler için geçerlidir.

Bir koleksiyona program aracılığıyla denetim eklemek için

  1. Eklenecek denetimin bir örneğini oluşturun.

  2. Yeni denetimin özelliklerini ayarlayın.

  3. Denetimi üst Controls denetimin koleksiyonuna ekleyin.

    Aşağıdaki kod örneği, denetimin bir örneğini oluşturmayı Button gösterir. Denetimi olan bir form gerektirir ve oluşturulan düğme için olay işleme Panel yönteminin zaten NewPanelButton_Click mevcut olması gerekir.

    Public NewPanelButton As New Button()  
    
    Public Sub AddNewControl()  
       ' The Add method will accept as a parameter any object that derives  
       ' from the Control class. In this case, it is a Button control.  
       Panel1.Controls.Add(NewPanelButton)  
       ' The event handler indicated for the Click event in the code
       ' below is used as an example. Substite the appropriate event  
       ' handler for your application.  
       AddHandler NewPanelButton.Click, AddressOf NewPanelButton_Click  
    End Sub  
    
    public Button newPanelButton = new Button();  
    
    public void addNewControl()  
    {
       // The Add method will accept as a parameter any object that derives  
       // from the Control class. In this case, it is a Button control.  
       panel1.Controls.Add(newPanelButton);  
       // The event handler indicated for the Click event in the code
       // below is used as an example. Substitute the appropriate event  
       // handler for your application.  
       this.newPanelButton.Click += new System.EventHandler(this. NewPanelButton_Click);  
    }  
    

Koleksiyondan program aracılığıyla denetimleri kaldırmak için

  1. Olay işleyicisini olaydan kaldırın. Bu Visual Basic RemoveHandler Deyimi anahtar sözcüğünü kullanın; C# içinde -= işleci kullanın.

  2. İstenen Remove denetimi panelin koleksiyonundan silmek için yöntemini Controls kullanın.

  3. Denetimin Dispose kullandığı tüm kaynakları serbest bırakmak için yöntemini çağırma.

    Public Sub RemoveControl()  
    ' NOTE: The code below uses the instance of
    ' the button (NewPanelButton) from the previous example.  
       If Panel1.Controls.Contains(NewPanelButton) Then  
          RemoveHandler NewPanelButton.Click, AddressOf _
             NewPanelButton_Click  
          Panel1.Controls.Remove(NewPanelButton)  
          NewPanelButton.Dispose()  
       End If  
    End Sub  
    
    private void removeControl(object sender, System.EventArgs e)  
    {  
    // NOTE: The code below uses the instance of
    // the button (newPanelButton) from the previous example.  
       if(panel1.Controls.Contains(newPanelButton))  
       {  
          this.newPanelButton.Click -= new System.EventHandler(this.
             NewPanelButton_Click);  
          panel1.Controls.Remove(newPanelButton);  
          newPanelButton.Dispose();  
       }  
    }  
    

Ayrıca bkz.