我想更改“with”中參考的文本框的屬性(例如:ReadOnly、BorderStyle、Forecolor...)。
Private Sub Apply(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txt10.KeyDown, txt0.KeyDown
If txt10.ReadOnly = False Then
If e.KeyCode = Keys.Enter Then
With txt10
.ForeColor = Color.White
.ReadOnly = True
.BorderStyle = BorderStyle.None
End With
End If
End If
End Sub
問題是,我需要將這些屬性設定為某個文本框,所以我宣告了一個名為“Number”的變數,我在想這是否可行:
With Controls("txt" & Number)
.ForeColor = Color.White
.ReadOnly = True
.BorderStyle = BorderStyle.None
End With
這僅適用于某些屬性,不適用于“with”。您知道如何進行這項作業嗎?
uj5u.com熱心網友回復:
檢索控制元件并將其定義為 TextBox。然后像以前一樣使用它。當您從Controls集合中獲取控制元件時,由于未指定控制元件型別,某些屬性將不可用(例如文本框只讀屬性)。
Dim textBox As TextBox = DirectCast(Controls("txt" & number), TextBox)
If textBox.ReadOnly = False AndAlso e.KeyCode = Keys.Enter Then
With textBox
.ForeColor = Color.White
.ReadOnly = True
.BorderStyle = BorderStyle.None
End With
End If
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/442642.html
