I want to change the properties (Example: ReadOnly, BorderStyle, Forecolor...) of the textbox referenced in "with".
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
The thing is, I need to set these properties to a certain textbox, so I've declared a variable called "Number", and I was thinking if this could work:
With Controls("txt" & Number)
.ForeColor = Color.White
.ReadOnly = True
.BorderStyle = BorderStyle.None
End With
This only works for some properties, and don't work with the "with". Do you have any idea how to make this work?
CodePudding user response:
Retrieve the control and define it as a TextBox. Then utilize it just as you did before. When you get a control from teh Controls
collection, because the control type is not specified, some properties will not be available (example the textbox readonly property).
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