مشاركة عبر


ControlCollection.AddControl أسلوب (Control, Range, String)

قم بإضافة محدد Controlإلى ControlCollectionفي النطاق المحدد.

مساحة الاسم:  Microsoft.Office.Tools.Excel
التجميع:  Microsoft.Office.Tools.Excel (في Microsoft.Office.Tools.Excel.dll)

بناء الجملة

'إقرار
Function AddControl ( _
    control As Control, _
    range As Range, _
    name As String _
) As ControlSite
ControlSite AddControl(
    Control control,
    Range range,
    string name
)

المعلمات

  • name
    النوع: System.String
    اسم عنصر التحكم الذي يمكن أن تستخدم لفهرسة عنصر التحكم في ControlCollectionمثيل.

القيمة المُرجعة

النوع: Microsoft.Office.Tools.Excel.ControlSite
كائن الذي يمثل عنصر تحكم الذي يتضمن الفرعي عنصر تحكم في ورقة عمل.

استثناءات

استثناء: شرط
ArgumentNullException

الوسيطة عنصر تحكم أو اسم النطاق هو nullمرجع خالٍ (لا شيء في Visual Basic)، أو وسيطة "اسم" الطول الصفرية.

ControlNameAlreadyExistsException

عنصر تحكم بنفس الاسم هو موجود بالفعل في ControlCollectionالمثيل.

InvalidRangeException

النطاق الذي تم تحديده هو غير صالح. لا يمكن استخدام نطاقات multi-مساحة. يجب أن يكون النطاق ورقة عمل نفسها ك ControlCollectionمثيل.

ملاحظات

يمكن استخدام هذا الأسلوب إلى إضافة أي عنصر تحكم إلى ControlCollectionفي وقت التشغيل. لمزيد من المعلومات، راجع إضافة عناصر إلى مستندات Office في وقت التشغيل.

أمثلة

يلي تعليمات برمجية في المثال إضافة عنصري تحكم مستخدم المخصصة إلى ورقة عمل استخدام AddControlالأسلوب. عنصر التحكم أول هو تمت الإضافة إلى نطاق من خلايا. عنصر التحكم الثاني هو تمت الإضافة إلى الموقع محدد. تغيير تعليمات برمجية Topالخاصية للمستخدم أول مخصص عنصر تحكم، الانتقال الوحيدة التي عنصر تحكم نسبة إلى ControlSiteالذي يحتوي تشغيل عنصر تحكم تشغيل ورقة عمل. تعليمات برمجية ثم تعيين Topخاصية ControlSiteيتم إرجاعها بواسطة عنصر تحكم مستخدم الثاني إلى توضيح الطريقة الصحيحة إلى تعيين Topالخاصية لعنصر التحكم.

    Private Sub ExcelRangeAddControl()

        Dim CustomUserControl As New UserControl1()
        Dim CustomUserControl2 As New UserControl2()

        Dim DynamicControl As Microsoft.Office.Tools.Excel.ListObject = Me.Controls.AddControl( _
            CustomUserControl, 0, 0, 150, 150, _
            "DynamicControl")

        Dim DynamicControl2 As Microsoft.Office.Tools.Excel. _
            ControlSite = Me.Controls.AddControl( _
            CustomUserControl2, 200, 0, 150, 150, _
            "DynamicControl2")

        CustomUserControl.BackColor = Color.Blue
        CustomUserControl2.BackColor = Color.Green

        CustomUserControl.Top = 100
        DynamicControl2.Top = 100

    End Sub

private void ExcelRangeAddControl()
{

    UserControl1 customUserControl = new UserControl1();
    UserControl2 customUserControl2 = new UserControl2();

    Microsoft.Office.Tools.Excel.ControlSite dynamicControl =
        this.Controls.AddControl(customUserControl,
        0, 0, 150, 150, "dynamicControl");

    Microsoft.Office.Tools.Excel.ControlSite dynamicControl2 =
        this.Controls.AddControl(customUserControl2, 200, 0,
        150, 150, "dynamicControl2");

    customUserControl.BackColor = Color.Blue;
    customUserControl2.BackColor = Color.Green;

    customUserControl.Top = 100;
    dynamicControl2.Top = 100;
}

أمن NET Framework.

راجع أيضًَا

المرجع

ControlCollection واجهة

ControlCollection الأعضاء

AddControl التحميل الزائد

Microsoft.Office.Tools.Excel مساحة الاسم