簡體   English   中英

如何動態更改復選框的檢查狀態

[英]How do I dynamically change check box check state

我正在運行Visual Studio 2015的社區版本。

我正在嘗試使用Me.Controls...CheckState = CheckState.Unchecked動態更改動態創建的復選框的CheckState屬性,但出現編譯時錯誤,提示CheckState不是控件的成員。

我在下面顯示了我用來創建復選框的代碼,並在底部顯示了我試圖用來更改值的代碼。 我將不勝感激任何建議。

        cbPDF.Location = New Point(710, tvposition)
    cbPDF.Size = New Size(80, 20)
    cbPDF.Name = "cbPDF" + panposition.ToString
    cbPDF.Text = "PDF Conv"
    cbPDF.CheckState = CheckState.Unchecked
    Controls.Add(cbPDF)
    AddHandler cbPDF.CheckedChanged, AddressOf Me.CommonCheck
    arrTextVals(10, panposition) = "cbPDF" + panposition.ToString
    arrTextVals(11, panposition) = "unchecked"

    If arrTextVals(11, bottomLine) = "unchecked" Then
        Me.Controls(arrTextVals(10, bottomLine)).CheckState = CheckState.Unchecked
    Else
        Me.Controls(arrTextVals(10, bottomLine)).CheckState = CheckState.Checked
    End If

該行試圖在不具有該屬性的通用控件對象上設置CheckState。

Me.Controls(arrTextVals(10, bottomLine)).CheckState = CheckState.Unchecked

您需要將其強制轉換為復選框以設置此屬性(您需要確保它實際上是一個復選框,否則會產生運行時錯誤):

DirectCast(Me.Controls(arrTextVals(10, bottomLine)), CheckBox).CheckState = CheckState.Unchecked

或長手以便於閱讀:

Dim ctl As Control = Me.Controls(arrTextVals(10, bottomLine))
Dim chk As CheckBox = DirectCast(ctl, CheckBox)
chk.CheckState = CheckState.Unchecked

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM