ControlExtensions.AddRadioButton 方法 (ControlCollection, Range, String)

將新的 RadioButton 控制項加入至工作表中指定的範圍。

命名空間:  Microsoft.Office.Tools.Excel
組件:  Microsoft.Office.Tools.Excel.v4.0.Utilities (在 Microsoft.Office.Tools.Excel.v4.0.Utilities.dll 中)

語法

'宣告
<ExtensionAttribute> _
Public Shared Function AddRadioButton ( _
    controls As ControlCollection, _
    range As Range, _
    name As String _
) As RadioButton
public static RadioButton AddRadioButton(
    this ControlCollection controls,
    Range range,
    string name
)

參數

傳回值

型別:Microsoft.Office.Tools.Excel.Controls.RadioButton
加入 ControlCollection 執行個體的 RadioButton 控制項中。

使用注意事項

在 Visual Basic 和 C# 中,您可以在任何 ControlCollection 型別物件中呼叫這個方法以做為執行個體。使用執行個體方法語法呼叫這個方法時,請省略第一個參數。如需詳細資訊,請參閱擴充方法 (Visual Basic)擴充方法 (C# 程式設計手冊)

例外狀況

例外狀況 條件
ArgumentNullException

名稱或範圍引數為 nullNull 參照 (即 Visual Basic 中的 Nothing),或者名稱引數的長度為 0。

ControlNameAlreadyExistsException

具有相同名稱的控制項已經存在於 ControlCollection 執行個體中。

InvalidRangeException

指定的範圍無效。 無法使用多區域範圍。 範圍必須放在和 ControlCollection 執行個體相同的工作表上。

備註

AddRadioButton 方法可以用來將 RadioButton 物件加入至 ControlCollection 的結尾。 若要移除先前以程式設計方式加入的 RadioButton,請使用 Remove 方法。

控制項會在重新調整範圍大小時自動重新調整大小。

當您將多個選項按鈕直接加入文件中時,這些選項按鈕並不會互斥。 您可以撰寫程式碼讓選項按鈕互斥,但最好是能先將選項按鈕加入至使用者控制項,然後再將該使用者控制項加入至文件中。

範例

下列程式碼範例會將兩個 RadioButton 控制項加入至工作表的儲存格 A1A2,然後指派文字給每一個控制項。 若要使用這個範例,請在文件層級專案中的 Sheet1 類別執行。

    Private Sub ExcelRangeAddRadioButton()

        Dim RadioButton1 As Microsoft.Office.Tools. _
            Excel.Controls.RadioButton = Me.Controls. _
            AddRadioButton(Me.Range("A1"), "RadioButton1")
        Dim RadioButton2 As Microsoft.Office.Tools. _
            Excel.Controls.RadioButton = Me.Controls. _
            AddRadioButton(Me.Range("A2"), "RadioButton2")

        RadioButton1.Text = "Bold"
        RadioButton2.Text = "Italic"
        RadioButton2.Checked = False

    End Sub

private void ExcelRangeAddRadioButton()
{


    Microsoft.Office.Tools.Excel.Controls.RadioButton
         radioButton1 = this.Controls.AddRadioButton(
         this.Range["A1", missing], "radioButton1");
    Microsoft.Office.Tools.Excel.Controls.RadioButton
         radioButton2 = this.Controls.AddRadioButton(
         this.Range["A2", missing], "radioButton2");
    radioButton1.Text = "Bold";
    radioButton2.Text = "Italic";
    radioButton2.Checked = false;

}

.NET Framework 安全性

請參閱

參考

ControlExtensions 類別

AddRadioButton 多載

Microsoft.Office.Tools.Excel 命名空間