كيفية القيام بما يلي: إضافة إلى أو إزالتها من مجموعة من عناصر التحكم في وقت التشغيل

المهام عام في تطوير التطبيقات تقوم بإضافة عناصر تحكم إلى و إزالة عناصر التحكم من أي عنصر تحكم الحاوية تشغيل النماذج الخاصة بك (مثل Panelأو GroupBoxعنصر التحكم أو النموذج نفسه حتى). عند تصميم الوقت، عناصر التحكم التي يمكن سحبه مباشرة على صندوق مجموعة أو لوحة. الاحتفاظ بعناصر التحكم هذه في وقت التشغيل، Controlsمجموعة، يحتفظ بتعقب لعناصر التحكم التي توضع عليها.

ملاحظة

مثال التعليمة البرمجية التالية تنطبق إلى أي عنصر تحكم التي تحتفظ بها مجموعة من عناصر التحكم الموجودة بداخله.

لإضافة عنصر تحكم إلى مجموعة برمجياً

  1. إنشاء مثيل عنصر تحكم لتتم إضافته.

  2. قم بتعيين خصائص الجديدة عنصر تحكم.

  3. إضافة عنصر تحكم إلى Controlsمجموعة من الأصل عنصر تحكم.

    يظهر المثال تعليمات برمجية التالي كيفية إنشاء مثيل ل Buttonعنصر تحكم. يتطلب نموذج يحتوي على Panelوالذي تم تاريخ الإنشاء أسلوب معالجة حدث للزر، NewPanelButton_Click، بالفعل.

    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. Substite the appropriate event
       // handler for your application.
       this.newPanelButton.Click += new System.EventHandler(this. NewPanelButton_Click);
    }
    

لإزالة عناصر التحكم من مجموعة برمجياً

  1. قم بإزالة معالج حدث من حدث. فيVisual Basic, استخدم theبيان RemoveHandlerكلمة أساسية; في#Visual C, استخدم the-= عامل التشغيل (C# مرجع).

  2. استخدام Removeالأسلوب لحذف المطلوب عنصر تحكم من لوحة إلى Controlsمجموعة.

  3. باستدعاء Disposeأسلوب إلى قم بتحرير الجميع موارد المستخدمة من قبل عنصر تحكم.

    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();
       }
    }
    

راجع أيضًا:

المرجع

Panel

موارد أخرى

لوحة عنصر تحكم (Windows Forms)